STM32 Blue Pill TCP Server with W5500 and MQ-2 Gas Sensor
TCP server on STM32F103C8T6 with W5500 for remote LED control and gas sensor monitoring over Ethernet
Software Apps and online services
A Beginner-Friendly W5500 TCP Server
Nepalese maker Rubin Khadka Chhetri has built a clean, well-documented TCP server project using the STM32 Blue Pill and a WIZnet W5500 Ethernet module. The server listens on port 5000 and lets you control an LED and monitor an MQ-2 gas sensor remotely over a wired Ethernet connection.
What makes this project worth highlighting is not its complexity, but its clarity. The README includes a full schematic, demo videos, pin configuration tables, and a command reference. It is a solid starting point for anyone looking to learn how to use the W5500 with STM32.
How It Works
The system is straightforward. The STM32 initializes the W5500 over SPI, assigns a static IP address (192.168.1.10), and opens a TCP socket on port 5000. A client (such as Hercules or PuTTY) connects and sends text commands. The STM32 processes each command and responds immediately.
| Command | What it does |
|---|---|
ON / OFF / TOGGLE | Control the LED |
STATUS | Check current LED state |
GAS | Get a single gas sensor reading (voltage, PPM, level) |
START / STOP | Enable or disable periodic sensor data every 5 seconds |
HELP | List all available commands |
The command loop is non-blocking, so LED commands execute immediately even while the sensor is streaming periodic data. UART debug output mirrors all activity to a serial console.
Project Structure
The repository is organized as a standard STM32CubeIDE project. The key source files are:
| File | Role |
|---|---|
Core/Src/tcp_server.c | TCP server logic with command parser (227 lines) |
Core/Src/mq2_sensor.c | MQ-2 ADC driver with voltage, PPM, and level classification |
Drivers/Ethernet_W5500/wizchip_port.c | W5500 SPI init, reset sequence, static IP configuration |
Drivers/Ethernet_W5500/ | WIZnet ioLibrary (socket, wizchip_conf, DHCP, DNS) |
The W5500 port layer (wizchip_port.c) handles the SPI callback registration, hardware reset sequence, link status check with retry, and static IP assignment. It verifies communication by reading the W5500 version register (0x04) before proceeding.
W5500 TOE in Action
This project uses the W5500's hardware TCP/IP stack directly through the WIZnet ioLibrary. The TCP socket is created with socket(0, Sn_MR_TCP, 5000, 0), and the server enters listening mode with listen(0). When a client connects, the STM32 reads incoming data with recv() and sends responses with send().
All TCP/IP protocol handling happens inside the W5500 chip. The STM32 only deals with application-level logic: parsing commands and formatting responses. This is the W5500's TCP/IP Offload Engine (TOE) at work, and it is especially useful on a resource-constrained MCU like the STM32F103 with just 20KB of RAM.
Related Project
Rubin has also published a FreeRTOS version of this project, which separates the TCP server and sensor reading into independent RTOS tasks. If you are interested in a multitasking approach, that repository is worth a look.
FAQ
Q: What TCP client can I use to test this server?
A: Any TCP client works. The README demonstrates Hercules (a free Windows tool), but PuTTY, netcat (nc), or a custom Python script will also connect to 192.168.1.10:5000.
Q: Does this project use DHCP?
A: No. The IP address is statically configured to 192.168.1.10 in wizchip_port.c. You need to set your PC to a compatible static IP (e.g., 192.168.1.20) on the same subnet. The README includes a screenshot of the Windows static IP configuration.
Q: How accurate is the MQ-2 gas sensor reading?
A: The driver uses a fixed linear formula tuned for this specific sensor setup. It is not calibrated for precise PPM measurement. The gas level classification (NORMAL, LOW, MEDIUM, HIGH, CRITICAL) based on voltage thresholds is more useful for detecting relative changes.
Q: Can this run on other STM32 boards?
A: Yes. The project uses STM32 HAL, so porting to another STM32F1xx board requires updating the SPI and GPIO pin definitions. The W5500 driver and TCP server code remain unchanged.



