cancel
Showing results for 
Search instead for 
Did you mean: 

Implementing MQTT in STM32F407G

ggowd.1
Associate III

Hi Team,

I am currently working on a project involving the STM32F407G microcontroller, where I have configured MQTT to connect to a server. However, I am encountering persistent issues and have been unable to establish a successful connection.

The specific error messages I receive are:

  • mqtt_connect return -10
  • Publish err: -1
  • mqtt_connection_cb: Disconnected, reason: 256

My setup includes:

  • No RTOS
  • Ethernet communication through the ENC28J60 controller

Despite spending over a week troubleshooting, I have been unable to resolve these issues. I have attached the relevant sections of my code for your reference. Could someone please provide guidance or suggestions on how to overcome these errors? 

Best Regards,
Gagan Gowda

12 REPLIES 12
STea
ST Employee

Hello @ggowd.1 ,

Your attachment was not included in the post can you add it. also is it possible to test that your Hardware setup is working by trying to run a simple TCP echo example to make sure that you are establishing a connection correctly with your external PHY .
Regards

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hello @STea ,

Thank you for your suggestion. I was able to ping the IP address in the command prompt, which indicates that the hardware setup is working to some extent. However, I am encountering an issue where I cannot subscribe to the MQTT server using Mosquitto.
Could you please provide guidance or documentation, procedures, or links to MQTT protocol.

Best Regards,
Gagan Gowda

Hello @ggowd.1 ,

You can see the following eziya/STM32F4_HAL_ETH_MQTT_CLIENT: STM32 + FREERTOS + LWIP + MQTT example (github.com)
which implement an MQTT client on STM32F4 using Lwip and FreeRtos which is very similar to your usecase
see also How to Implement Embedded Mqtt Client Using W5500 IC. : 5 Steps - Instructables
you can see also an implementation of pubsubclient MQTT using STM32duino which is a very simple and quick way to establish a client with very little time and effort STM32Examples/examples/Communication/MQTT/PubSubClient/mqtt_STM32Ethernet/mqtt_STM32Ethernet.ino at main · stm32duino/STM32Examples (github.com)

Regards

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hi @STea ,

Thank you for sharing links about the MQTT protocol. I am currently working on Ethernet and MQTT protocol without using RTOS. If it's not feasible to implement them without RTOS, I will transition to working with RTOS on the ENC28J60. Once Ethernet is functioning with RTOS, I will then proceed to implement MQTT.Is there a procedure to configure the ENC28J60 with RTOS on the STM32F407G board?

Best Regards,
Gagan Gowda

Hi @STea,
 
I hope this message finds you well. I wanted to follow up on my previous inquiry regarding the configuration of the ENC28J60 Ethernet module with RTOS on the STM32F407G board.

I’m currently working on integrating Ethernet and MQTT protocols without RTOS. However, if RTOS is necessary, I plan to transition to using RTOS with the ENC28J60. Once Ethernet is successfully configured with RTOS, I’ll proceed with the MQTT implementation.

Could you please provide guidance or share any relevant resources or documentation that might assist with this setup?

Best Regards
Gagan Gowda

STea
ST Employee

Hello @ggowd.1 ,

To verify basic connectivity, ensure that the ENC28J60 Ethernet controller is correctly configured and that there are no physical connection issues. Since you can ping the IP address, the basic Ethernet setup seems to be working. Next, check your MQTT client configuration, including the server address, port, client ID, username, and password if required. Verify that the MQTT broker is running and accessible from your network. The error codes you mentioned indicate various issues: mqtt_connect return -10 suggests a connection failure, Publish err: -1 points to a publishing failure, and mqtt_connection_cb: Disconnected, reason: 256indicates a lost connection. Consult your MQTT library documentation for more details on these codes. Before diving into MQTT, test your Ethernet setup with a simple TCP echo server/client example to ensure basic communication works and insure you have access to the network. While you can implement Ethernet and MQTT without an RTOS, using one can simplify task management and timing control. For configuring ENC28J60 with RTOS on STM32F407G, set up FreeRTOS, integrate the ENC28J60 driver, use the LwIP stack for TCP/IP communication, and implement an MQTT library compatible with LwIP and FreeRTOS. Use Wireshark for network traffic analysis and add debug prints to trace errors. Ensure the ENC28J60 and STM32F407G are correctly powered and free of hardware issues.
Regards

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

@ggowd.1 wrote:

The specific error messages I receive are:

  • mqtt_connect return -10
  • Publish err: -1
  • mqtt_connection_cb: Disconnected, reason: 256

So have you looked-up what those messages/codes mean?

Hi @STea ,

"Thanks for your update. I have now checked and confirmed that I can successfully ping the IP address of the ENC28J60 board, and I am able to open the HTTP server using the IP address. I also tested the TCP echo server, and it is working fine.

However, when I check the MQTT functionality, I am encountering an error. I will post all the screenshots of the working setup, along with the main.c and other files. Please review and correct any mistakes you may find."

Hi @STea ,

Please find attached the screenshots of everything working.