STM32F411CEU6_W5500_BME280_Modbus
This project demonstrates a robust, production-ready Modbus TCP Sensor Node utilizing the STM32F411CEU6 (BlackPill) as the primary MCU and the W5500 Hardwired T
Implementation of an Industrial Modbus TCP Environment Monitoring Node: STM32F411 & W5500
This project demonstrates a robust, production-ready Modbus TCP Sensor Node utilizing the STM32F411CEU6 (BlackPill) as the primary MCU and the W5500 Hardwired TCP/IP controller for reliable wired Ethernet connectivity.
Moving beyond simple data streaming, this implementation provides a structured Modbus TCP Server architecture, enabling seamless integration with industrial PLC, HMI, and SCADA systems for real-time environmental monitoring (Temperature, Humidity, Pressure via BME280).
🛠 Hardware Architecture & Interface Design
The system is engineered for high reliability by offloading the networking overhead from the MCU to the W5500 hardware stack, ensuring deterministic performance for the application layer.
Core Logic: STM32F411CEU6 (Cortex-M4, 100MHz) managing the sensor interface and Modbus register mapping.
Network Engine: W5500 Ethernet Controller via SPI, featuring a hardwired TCP/IP stack that minimizes CPU utilization.
Sensor Interface: BME280 via I2C1 (configured at 100kHz Standard Mode).
Pin Configuration:
SPI1 (Comm): PA5 (SCK), PA6 (MISO), PA7 (MOSI) — Configured with Prescaler 2 for high-speed W5500 interaction.
W5500 Control: PA4 (CS), PA3 (RST), PA2 (INT) using Software NSS.
Debug UART: PA15 (TX), PB3 (RX) @ 115,200bps for real-time telemetry and diagnostics.
📡 Modbus TCP Protocol Implementation
The software is not merely a wrapper but a custom-implemented Modbus TCP stack designed for stability and standards compliance.
Protocol Core: Operates on the standard TCP Port 502 using Socket 0.
Data Modeling: Maintains a 10-slot Holding Register array for sensor data and system parameters.
Supported Function Codes:
FC03 (Read Holding Registers): For high-precision data retrieval by the master.
FC06 (Write Single Register): For remote configuration and threshold setting.
Validation Logic: The engine performs rigorous frame validation, including Protocol ID verification, MBAP header length consistency, and register boundary checks.
Exception Handling: In compliance with the Modbus specification, the system returns standard exception codes:
0x01 (Illegal Function)
0x02 (Illegal Data Address)
0x03 (Illegal Data Value)
⚙️ W5500 Networking Strategy: Hardware Offloading
The integration of the W5500 allows the STM32 to treat the network as a simple hardware socket rather than managing a complex software TCP/IP stack (like LwIP).
State Machine Management: The Modbus_Loop() function continuously monitors socket states (SOCK_ESTABLISHED, SOCK_CLOSE_WAIT, SOCK_CLOSED). It includes robust recovery logic to re-initialize the server state automatically upon connection drops.
Buffer Management: 512-byte dedicated RX/TX buffers ensure that even during burst traffic, Modbus frames are captured and processed without packet loss.
Efficiency: By utilizing the W5500's internal buffer and hardware logic, the STM32 remains free to handle high-frequency sensor sampling and local logic processing.
🚀 Industrial Applications
This architecture is ideal for deployment in environments where wireless reliability is insufficient and industrial protocol compatibility is mandatory:
Industrial IoT Gateways: Bridging I2C/UART sensors to a factory-wide SCADA network.
Server Room/Data Center Monitoring: Reliable, wired environmental sensing for critical infrastructure.
Smart Building Automation: Integrating environmental data into Building Management Systems (BMS) via Modbus TCP.
Remote I/O Modules: Utilizing the Holding Registers for remote setpoint delivery and status signaling.
📑 Summary
This project serves as a comprehensive reference for developers looking to implement professional-grade Ethernet connectivity on STM32 platforms. It bridges the gap between simple hobbyist "Weather Stations" and robust industrial communication nodes by emphasizing protocol integrity, hardware offloading, and structured error handling.
