STM32 + W5500 MQTT Client — Industrial MODBUS IoT Gateway (WNR-MB-SEDE1)
STM32F103C8T6 Blue Pill + WIZnet W5500 Ethernet: MQTT communication test for an industrial MODBUS IoT gateway project built with STM32CubeIDE and HAL.
Overview
This project is an MQTT communication test firmware for an industrial IoT gateway called WNR-MB-SEDE1. It runs on a STM32F103C8T6 (Blue Pill) microcontroller paired with a WIZnet W5500 Ethernet module, enabling MODBUS field data collected at Site 1 (SEDE 1) to be published to an MQTT broker over wired Ethernet. The project is developed using STM32CubeIDE and the HAL library, with the MCU configuration managed through STM32CubeMX.
Hardware connection
The STM32F103C8T6 communicates with the W5500 via SPI1, providing reliable wired Ethernet connectivity. USART2 is used for serial debug output. CS and RST lines for the W5500 are driven by GPIO output pins.
System architecture
The gateway collects data from MODBUS field devices at SEDE 1 via RS-485/UART, processes it in the STM32 firmware, and publishes the data to an MQTT broker over TCP/IP through the W5500 hardware Ethernet stack.
Hardware Used
- MCU: STM32F103C8T6 (Blue Pill) — ARM Cortex-M3, 48MHz, LQFP48
- Flash: 64KB / RAM: 20KB
- Ethernet: WIZnet W5500 module (SPI, 10/100 Mbps, HW TCP/IP stack)
- SPI1 pins: SCK=PA5, MISO=PA6, MOSI=PA7
- W5500 control: CS=PB13 (GPIO Output), RST=PB14 (GPIO Output)
- Serial debug: USART2 — TX=PA2, RX=PA3 (Async)
- Clock: HSE external oscillator + PLL × 6 = 48MHz
- SWD debug: PA13=SWDIO, PA14=SWCLK
Pin configuration (STM32CubeMX)
All peripherals were configured using STM32CubeMX 6.8.0 with STM32Cube FW_F1 V1.8.5. The .ioc project file is included in the repository for easy regeneration.
Software & Tools
- IDE: STM32CubeIDE (Eclipse-based)
- HAL Library: STM32Cube FW_F1 V1.8.5
- CubeMX version: 6.8.0
- Language: C (97.9%), Makefile (1.9%), Assembly (0.2%)
- SPI1 baud rate: Prescaler /4 → 12.0 Mbits/s (Full Duplex Master)
- MQTT protocol: Publish/subscribe over TCP port 1883
- Build: Debug configuration via STM32CubeIDE
- Linker script: STM32F103C8TX_FLASH.ld
How to build & flash
- Clone the repository:
git clone https://github.com/josevasquez123/WNR-MB-SEDE1-CODIGO - Open in STM32CubeIDE (File → Import → Existing Projects into Workspace)
- To modify pin configuration, open
MQTT_TEST.iocin STM32CubeMX and regenerate code - Build the project (Debug configuration)
- Flash via SWD using ST-Link programmer (PA13=SWDIO, PA14=SWCLK)
- Monitor serial output via USART2 at the configured baud rate
FAQ
Q1. What is the purpose of the WNR-MB-SEDE1 project?
WNR-MB-SEDE1 is an industrial IoT gateway project designed to collect data from MODBUS field devices at Site 1 (SEDE 1) and forward it to an MQTT broker over wired Ethernet. The STM32 acts as the bridge between the MODBUS industrial protocol and the modern MQTT IoT protocol.
Q2. Why use the WIZnet W5500 instead of a software TCP/IP stack?
The W5500 includes a hardware TCP/IP stack that handles network processing independently from the STM32 CPU. This offloads TCP/IP processing, reduces firmware complexity, and improves reliability in industrial environments — critical for continuous MQTT connectivity.
Q3. What MQTT operations does this firmware implement?
The firmware tests MQTT publish and subscribe operations over TCP port 1883. It connects to a configurable MQTT broker and publishes field data collected from MODBUS devices, verifying end-to-end connectivity from the field sensors to the IoT platform.
Q4. How is the W5500 connected to the STM32?
The W5500 connects via SPI1 at 12 Mbits/s (Full Duplex Master). Chip Select is driven by PB13 and the hardware reset line by PB14, both configured as GPIO outputs in STM32CubeMX.
Q5. Can I open and edit this project without STM32CubeIDE?
The project uses the standard Eclipse CDT format (.cproject, .project) and can be imported into any Eclipse-based IDE. The MQTT_TEST.ioc file can be opened in STM32CubeMX standalone to modify pin assignments and regenerate HAL code.
Q6. What clock speed does the STM32 run at?
The STM32F103C8T6 is configured at 48MHz using the external HSE oscillator with PLL multiplier × 6. APB1 runs at 24MHz (divided by 2) and APB2 at 48MHz.
Key Takeaway
This project demonstrates a practical pattern for industrial IoT gateways: collect MODBUS data on an STM32 microcontroller and reliably publish it over MQTT using the WIZnet W5500 hardware Ethernet stack. The STM32 + W5500 combination provides a low-cost, reliable wired network solution — ideal for factory floors and industrial sites where Wi-Fi is not suitable.
Source Code: https://github.com/josevasquez123/WNR-MB-SEDE1-CODIGO


