Skip to main content
Visitor II
July 31, 2024
Question

Implementing MQTT in STM32F407G

  • July 31, 2024
  • 3 replies
  • 11631 views

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

    This topic has been closed for replies.

    3 replies

    ST Employee
    August 2, 2024

    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

    ggowd.1Author
    Visitor II
    August 6, 2024

    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

    ST Employee
    August 14, 2024

    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

    ST Employee
    September 3, 2024

    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

    ggowd.1Author
    Visitor II
    September 10, 2024

    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."

    Super User
    September 3, 2024

    @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?