Running an MQTT Client on STM32L4 with ES-WiFi and Eclipse Paho
Introduction
This project demonstrates how to run an MQTT client on an STM32L4 development board (B‑L475E‑IOT01A1) using the integrated ES‑WiFi module. The objective was to connect the board to a public Mosquitto MQTT broker, subscribe to a topic, and publish messages periodically. For MQTT functionality, the Eclipse Paho Embedded MQTT Client library was integrated with the STM32’s networking interface.
This post outlines the integration process, implementation highlights, and testing setup. It concludes with an optional configuration to connect the board to a cloud-hosted MQTT broker using an AWS EC2 instance.
Features
- MQTT client implementation using Paho Embedded C library
- Wi‑Fi connectivity via ES‑WiFi module
- Hostname resolution and TCP socket communication
- Topic subscription and periodic message publishing
- Real-time debugging and verification via command-line tools
Implementation Details
1. Integrating the MQTT Client Library
We used the Eclipse Paho Embedded C client, requiring the MQTTClient
and MQTTPacket
source files. These were placed under the project’s middleware directory.
To resolve symbol conflicts with ST HAL headers (e.g., SUCCESS
), we renamed the MQTT return code SUCCESS
to MQTT_SUCCESS
.
2. Network Interface Wrappers
The Paho library requires a custom Network
structure that defines how data is read/written. We implemented:
mqtt_network_read()
mqtt_network_write()
mqtt_network_disconnect()
These functions wrap calls to the ES‑WiFi interface provided by ST’s BSP libraries.
3. Timer Abstraction
Paho’s timer interface is used for keep-alive and timeouts. Our implementation leverages HAL_GetTick()
to define:
TimerInit()
TimerCountdownMS()
TimerCountdown()
TimerIsExpired()
TimerLeftMS()
4. Application Logic in main.c
Wi‑Fi Connection
The wifi_connect()
function initializes the Wi‑Fi module, connects to an access point, and retrieves an IP address.
Broker Communication
- Use
WIFI_GetHostAddress()
to resolve the broker hostname. - Connect using
WIFI_OpenClientConnection()
. - Initialize the MQTT client with the custom
Network
struct. - Configure the MQTT connection (client ID, keep-alive, clean session).
- Subscribe to
sensor/data
and publish messages every second usingHAL_Delay(1000)
or FreeRTOS’sosDelay(1000)
.
Testing and Debugging
Tools and Environment
- IDE: Keil uVision5 (Arm Compiler 5)
- Network: Android Hotspot
- Board: B-L745E-IOT01A1
- Terminal: Tera Term @ 115200 baud
- Verification Tool:
mosquitto_sub
Example Command
mosquitto_sub -h test.mosquitto.org -p 1883 -t "sensor/data"
Broker Setup (Optional)
To self-host a broker (non-TLS) on AWS EC2:
- Launch a Ubuntu EC2 instance.
Install Mosquitto:
sudo apt-get update sudo apt-get install mosquitto mosquitto-clients
- Allow port 1883 in the EC2 security group.
- Set your board’s broker hostname to the EC2 public DNS.
Key Modifications in Code
- Update Wi‑Fi SSID and password in
main.c
- Change broker hostname if using a custom server
- Ensure Arm Compiler 5 is installed (Guide)
Conclusion
This project successfully showcases how to integrate an MQTT client on STM32 using the Paho library and ES-WiFi interface. It provides a foundational example for IoT applications where real-time data publishing is essential. While this setup doesn’t use TLS, it lays the groundwork for more secure implementations in the future.
License
The Eclipse Paho Embedded MQTT library is used under the Eclipse Public License. Ensure you review the original license terms when reusing the code.
GitHub Repository
You can find the full source code and setup instructions on GitHub.