STM32 Web Server Over WiFi: Control GPIO, Read Sensors (Examples)

In this tutorial, we’ll discuss how to build an STM32 web server over WiFi that you can open from any browser on your network to control the STM32’s GPIO pins and read its sensors, using an ESP32-C3 as a WiFi co-processor. You’ll learn how an STM32 can serve web pages through the ESP32-C3’s TCP server (ESP-AT), how to parse HTTP requests and send responses on the STM32, how to update a page live with JSON, and how to open the page by name at http://stm32.local.

Then, we’ll build two STM32 web server example projects with our ESP_AT driver: a web page that turns the Blue Pill’s LED on and off, and a live dashboard that charts the STM32’s internal temperature sensor. We’ll also measure how fast the pages load and what happens when several browsers connect at once. This is the fifth part of our STM32 WiFi tutorial series. Without further ado, let’s get right into it!

Note: The STM32 code examples in this tutorial are tested with: STM32 Blue Pill (STM32F103C8T6), ESP32-C3 Core Board with ESP-AT firmware, Chrome, and a phone browser.


STM32 Web Server: How It Works

A web server waits for TCP connections on port 80, reads the request a browser sends (for example, GET /led/on HTTP/1.1), and sends back a response: a status line, a few headers, and the page or data. With an ESP32-C3 running ESP-AT, the ESP32-C3 holds the WiFi connection and the TCP server, and the STM32 handles the HTTP part:

StepWho Does ItAT Command or Event
Enable multiple connections, start the TCP server on port 80STM32 (once)AT+CIPMUX=1, AT+CIPSERVER=1,80
A browser connectsESP32-C3 tells the STM32<link>,CONNECT
The browser’s request arrivesESP32-C3 passes it to the STM32+IPD,<link>,<len>:GET / HTTP/1.1...
Read the request line, choose the responseSTM32–
Send the response, close the connectionSTM32AT+CIPSEND=<link>,<len>, AT+CIPCLOSE=<link>

The ESP32-C3 can keep up to 5 connections (links 0 to 4) open at a time, and every message carries its link number, so the STM32 knows which browser it’s answering. Our ESP_AT driver from the ESP32 AT commands with STM32 tutorial does this routing: it delivers each request as an event with its link number, and ESP_AT_SendClose() sends a response on a link and then closes it.

STM32 web server architecture: the ESP32-C3 runs the WiFi and TCP server with ESP-AT, and the STM32 parses the HTTP requests and sends the pages


STM32 Web Server Wiring and CubeMX Configuration

Required Parts

QTY.Component NameAmazon.comAliExpress
1STM32 Blue Pill (STM32F103C8T6)AmazonAliExpress
1ST-Link V2 DebuggerAmazonAliExpress
1ESP32-C3 board (ESP32-C3 SuperMini; we use the WeAct ESP32-C3 Core Board)AmazonAliExpress
1USB-TTL (UART) Converter, 3.3VAmazonAliExpress
1BreadBoardAmazonAliExpress
1Jumper Wires PackAmazonAliExpress

STM32 ESP32-C3 Wiring

The wiring is the same as in the previous parts of this series: the STM32’s USART1 goes to the ESP32-C3’s AT port, and USART2’s TX pin goes to a USB-TTL converter for the log. The ESP32-C3 needs the ESP-AT firmware (the ESP32 AT commands tutorial shows how to flash it).

PA9 USART1 TX, goes to the ESP32-C3 GPIO6 (AT RX).

PA10 USART1 RX, goes to the ESP32-C3 GPIO7 (AT TX).

PA2 USART2 TX, goes to the USB-TTL converter’s RX: the log to your PC at 115200 baud.

GND Goes to the ESP32-C3 GND and the USB-TTL GND: common ground.

STM32 web server wiring: Blue Pill PA9 to ESP32-C3 GPIO6, PA10 to GPIO7, and PA2 to a USB-TTL converter

STM32 Blue Pill ESP32-C3 / USB-TTL PA9 (USART1 TX) GPIO6 (AT RX) PA10 (USART1 RX) GPIO7 (AT TX) GND GND PA2 (USART2 TX) USB-TTL RX GND USB-TTL GND

STM32 CubeMX Configuration

Start from the CubeMX configuration of the ESP32 AT commands tutorial (every step has a screenshot there): USART1 at 115200 baud with RX DMA in Circular mode and the USART1 global interrupt for the ESP32-C3, USART2 for the log, PC13 as the LED output, and the 72MHz clock.

STM32CubeMX USART1 DMA Settings: USART1_RX in Circular mode and USART1_TX in Normal mode for the ESP_AT driver

For the temperature examples, also enable ADC1 with the Temperature Sensor Channel and the Vrefint Channel, and set the sampling time to 239.5 cycles (the sensor needs a long sampling time). Set the ADC clock to 12MHz (PCLK2 / 6) in the Clock Configuration tab. The STM32 internal temperature sensor tutorial walks through these settings.


STM32 Web Server Functions (ESP_AT Driver)

Function or EventWhat It Does
cfg.multiLink = 1Lets the ESP32-C3 keep up to 5 connections (needed for a server)
ESP_AT_ServerStart(&esp, 80)Starts the TCP server on port 80 (AT+CIPSERVER=1,80)
ESP_AT_Mdns(&esp, "stm32", "_http", 80)Announces the page as http://stm32.local with AT+MDNS
ESP_AT_EVT_LINK_OPENED / ESP_AT_EVT_LINK_CLOSEDA browser connected or disconnected, d->link says which one
ESP_AT_EVT_DATARequest bytes from a browser, with d->link
ESP_AT_SendClose(&esp, link, data, len)Sends a response on one link, then closes that link

STM32 Web Server Example 1: Control LED From a Web Page

In the first example project, the STM32 serves a web page with two buttons, LED ON and LED OFF, and a status box. In other words, any phone or computer on your WiFi can turn the Blue Pill’s on-board LED (PC13) on and off:

  • /: the web page itself (1.2KB of HTML, CSS, and JavaScript stored in the STM32’s flash).
  • /led/on, /led/off, /led/toggle: change the LED and answer with the new state as JSON.
  • /status: the LED state, uptime, chip temperature, and the number of requests as JSON. The page asks for it every second, so it stays up to date even if someone else changes the LED.
  • Anything else gets a 404 Not Found.

Wiring: PA9 to ESP32-C3 GPIO6, PA10 to GPIO7, PA2 to the USB-TTL RX, and GND to both.

Write your WiFi name and password in Core/Inc/wifi_secrets.h:

Here is The Application Code For This Example Project (main.c). Only the USER CODE sections are shown; the rest of the file is generated by CubeMX:

STM32 Web Server Example Code Explanation

The page is one C string, pageResponse[], that already starts with the HTTP response headers ( HTTP/1.1 200 OK, Content-Type: text/html, Connection: close). Because the STM32 closes the connection after the page, the browser knows where the page ends, and no Content-Length header is needed.

OnEvent() keeps one slot per ESP link. When a browser connects ( ESP_AT_EVT_LINK_OPENED), its slot is cleared. The request bytes ( ESP_AT_EVT_DATA) are collected until the end of the first line, which is all the server needs: GET /led/on HTTP/1.1. The browser’s other headers are ignored.

In the main loop, the code looks for a slot with a complete request line, calls Route() to pick the response (and switch the LED), and sends it with ESP_AT_SendClose(). The JSON replies are built with snprintf() into a static buffer, which must stay unchanged until the driver has sent it.

The script in the page does the rest in the browser: the buttons call led('on') or led('off'), which fetch /led/on or /led/off, and poll() asks for /status every second and updates the status box.

Tip: If your router supports mDNS (most home routers, Windows 10/11, macOS, iOS, and Android 12 or newer do), open http://stm32.local instead of the IP address. The example announces that name with ESP_AT_Mdns().

STM32 Web Server Example Testing

After a reset, the STM32 joins the WiFi, starts the server, and prints the address to open:

STM32 web server serial log after a reset: module version, server and mDNS started, and the address to open
STM32 web server serial log after a reset: module version, server and mDNS started, and the address to open

This is the real hardware testing result for this example project. On the left is the web page in Chrome, and on the right is my webcam on the Blue Pill: every click on LED ON or LED OFF switches the PC13 LED a moment later.

STM32 web server demo: clicking LED ON and LED OFF on the web page switches the Blue Pill’s PC13 LED

The STM32’s log shows every request, the link it came on, and the time from the connection to the end of the response:

STM32 web server serial log: every request, its link, and the time from connect to close
STM32 web server serial log: every request, its link, and the time from connect to close


STM32 Web Server Example 2: Live Temperature Dashboard

In this example project, the STM32 reads its internal temperature sensor once a second, keeps the last 60 readings, and serves a dashboard page with the current temperature in large digits and a live line chart of the last minute. The page draws the chart itself with a <canvas>, so it needs no chart library and works on a WiFi network without internet access. The LED toggle button is on the same page.

  • /: the dashboard page (2.6KB).
  • /data: the current temperature, the supply voltage (VDDA), the LED state, and the 60-reading history as JSON. The page asks for it every second.
  • /led/toggle: toggles the LED.

Here is The Application Code For This Example Project (main.c). Only the USER CODE sections are shown; the rest of the file is generated by CubeMX:

STM32 Internal Temperature Sensor Reading

The STM32F103’s temperature sensor is an analog voltage on ADC1 channel 16: 1.43V at 25°C (V25), falling by 4.3mV per °C (Avg_Slope), so the temperature is:

The example averages 16 conversions per reading and measures the real supply voltage with the internal reference (Vrefint, channel 17), so Vsense stays right even if the 3.3V supply is a bit off. The STM32F103 has no factory calibration values for the sensor, so V25 and Avg_Slope are the datasheet’s typical values, and the result can be a few degrees off from chip to chip. It also measures the chip, not the room: our reading was about 32°C in a room around 25°C. The STM32 internal temperature sensor tutorial explains the sensor and its calibration in detail.

STM32 Web Server Dashboard Testing

This is the real dashboard served by the Blue Pill, after one minute of readings:

STM32 web server dashboard: the internal temperature sensor reading in large digits and a live chart of the last 60 seconds, served by the Blue Pill through an ESP32-C3


STM32 Web Server Measured: Page Load Times

We loaded each address 10 times from a PC on the same WiFi and measured the time from the request to the end of the response (median):

RequestSize115200 baud921600 baud
Example 1 page (/)1.2KB162ms56ms
Example 1 /status JSON47 bytes64ms42ms
Example 1 /led/toggleJSON69 to 73ms48 to 53ms
Example 2 dashboard page (/)2.6KB311ms–
Example 2 /data JSON (60 readings)324 bytes93ms–

At 115200 baud, the UART between the STM32 and the ESP32-C3 is the limit for the page itself: 1.2KB takes about 110ms on the wire. At 921600 baud, the same page loads in 56ms. Set SERVER_BAUD in the example to switch the speed.

How Many Browsers at Once?

The ESP32-C3 holds up to 5 connections at a time. We tested what happens when several clients connect at the same moment:

TestResult
3 clients at onceAll 3 got the page. The connections were accepted at 0, 1, and 3 seconds
7 clients at once4 got the page, 3 timed out after 10 seconds. The server recovered by itself afterwards
5 idle connections held openA 6th connection is refused (the ESP-AT link limit)

The 1 and 3 second steps come from the ESP32-C3, not the STM32: when several connection requests arrive at the same instant, it accepts one and drops the others, and the PC retries them after 1, 3, and 7 seconds. A browser opens only 1 or 2 connections, so one page in a few tabs or phones works fine. Because the server closes each connection after its response, links are freed quickly.


STM32 Web Server Troubleshooting

The page doesn’t open

Use the IP address the STM32 prints in its log, from a device on the same WiFi network (not a guest network, which usually blocks devices from each other). Make sure the URL starts with http://, not https://. The STM32 doesn’t serve HTTPS.

http://stm32.local doesn’t work

mDNS needs support on the client and the network. Older Android versions and some routers don’t support it. Use the IP address, or give the ESP32-C3 a fixed IP address in your router’s DHCP settings.

AT+CIPSERVER returns ERROR

The server needs multiple connections enabled first ( AT+CIPMUX=1), and AT+CIPMUX can only change while no connection is open. Our driver’s Begin closes old connections, and cfg.multiLink = 1 sets the mode.

The page loads slowly or partly

At 115200 baud, every KB takes about 90ms on the UART. Keep the page small, or switch the UART to 921600 baud. Don’t send big pictures from the STM32: link to them on another server instead.

The temperature reading is wrong

The STM32F103 sensor has no factory calibration, so expect an offset of a few degrees. Measure the offset once against a thermometer and subtract it. Also use a long ADC sampling time (239.5 cycles).


STM32 Web Server FAQ

Can the STM32 web server be reached from the internet?

Only through your router: you’d need port forwarding to the ESP32-C3’s IP address, and the page has no password or encryption. For remote access, it’s safer to send the data out to a server or an MQTT broker, which we’ll cover in the next part of this series.

How many pages or devices can the STM32 web server handle?

The ESP32-C3 keeps up to 5 connections at once, and each request takes 50 to 300ms, so a few phones and PCs at the same time work well. It isn’t meant for many users.

Can I serve HTML files from an SD card?

Yes. Read the file in chunks and send each chunk with the driver’s send call before closing the link. Keep in mind the UART speed limit for large files.

Do I need FreeRTOS or LwIP for an STM32 web server?

Not with an ESP32-C3 running ESP-AT: the TCP/IP stack runs on the ESP32-C3, and the STM32 only handles the HTTP text. You need LwIP for an STM32 with Ethernet, like an STM32F4 or STM32H7 board.


STM32 WiFi Tutorial Series

This tutorial is the fifth part of our STM32 WiFi tutorial series:

PartTutorialWhat You’ll Build
1STM32 WiFiChoosing a WiFi module, quick start, ESP32-C3 vs ESP8266 measured
2ESP8266 AT Commands With STM32ESP8266 AT firmware, commands, and our ESP_AT driver
3ESP32 AT Commands With STM32ESP-AT firmware, commands, SNTP time, and our ESP_AT driver
4STM32 HTTP Client Over WiFiGET, POST, HTTPS, and JSON parsing
5STM32 Web Server Over WiFi (this tutorial)Control GPIO from a browser, live temperature dashboard
6STM32 MQTT TutorialPublish and subscribe, last will, MQTT over TLS
7STM32 OTA Firmware Update Over WiFiBootloader, dual-slot update, CRC32 check
8STM32 WebSocket With ESP32WebSocket server on the STM32, live dashboard

Download STM32 Web Server Example Projects (LED Control, Temperature Dashboard)

Download all attachment files for this Article/Tutorial (project files, schematics, code, etc.) using the link below. Everything is tested and verified to work on real hardware, as you’ve seen in the demo videos & screenshots. Please consider supporting my work if it helped you.

Wrap Up

To conclude this tutorial, we’ve built an STM32 web server over WiFi with an ESP32-C3 and our ESP_AT driver. The STM32 parses the HTTP requests and serves the pages, while the ESP32-C3 holds the WiFi and the TCP connections. We controlled the Blue Pill’s LED from a browser, served a live dashboard of the STM32’s internal temperature sensor, and measured page loads of 56 to 311ms and the limits with several browsers at once.

In the next part of this series, we’ll connect the STM32 to an MQTT broker to publish sensor data and receive commands, and show it in Home Assistant.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment