Some links in this article are affiliate links. If you buy through them, LabprojectsBD may earn a small commission at no extra cost to you.
In this guide, you will learn how to connect the ESP8266 Wi-Fi module to an STM32 microcontroller. We cover the wiring, the AT commands and a working example that sends data to a server over Wi-Fi.
⚠️Disclaimer:
Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.
All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.
If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.
Need help or have questions? Leave a comment below — the author is always happy to assist!
Table of Contents
About STM32
STM32 is a family of 32-bit ARM Cortex-M microcontrollers from STMicroelectronics. They offer low power use, good performance and a rich set of peripherals. However, most STM32 chips have no built-in Wi-Fi. Therefore, to connect one to a wireless network, you add an external Wi-Fi module.
About WiFi modules
Wi-Fi sends and receives data over a local network using radio waves. It follows the IEEE 802.11 standard, and almost every modern device supports it. Several low-cost Wi-Fi modules work well with microcontrollers.
Among these, the ESP8266 is the most popular. Its smallest board version is called the ESP-01.
Interfacing ESP8266 with STM32
The ESP8266 is easy to add to an STM32 project. In its default firmware, it accepts simple AT commands over a serial (UART) link. Follow these steps:
- Wire the two boards: connect the STM32 UART TX pin to the ESP8266 RX pin, and the STM32 RX pin to the ESP8266 TX pin. Also join the grounds. Both parts use 3.3 V logic, so no level shifter is needed. Power the ESP-01 from a 3.3 V regulator that can supply at least 300 mA, because it draws current peaks while transmitting.
- Set up the UART: in STM32CubeMX, enable a USART (for example USART2) at 115200 baud, 8 data bits, no parity and 1 stop bit. This is the default speed of most ESP8266 AT firmware.
- Initialise the module: send AT commands to reset it, select station mode and join your Wi-Fi network. After each command, wait for the “OK” reply before you send the next one.
- Send and receive data: open a TCP connection with
AT+CIPSTART, then send data withAT+CIPSEND. The ESP8266 handles all the Wi-Fi work for you. - Debug: if something fails, connect the ESP8266 to a USB-to-serial adapter and type the AT commands by hand in a terminal. In this way, you can see each reply and find the problem quickly.
In short, connecting an ESP8266 to an STM32 needs only a few wires and some simple code. As a result, you can add Wi-Fi to almost any embedded product.
Example code
Here is a working example that uses the STM32 HAL library. It joins a Wi-Fi network and sends a message to a TCP server every 5 seconds:
/* main.c (excerpt). SystemClock_Config(), MX_GPIO_Init() and MX_USART2_UART_Init()
are generated by STM32CubeMX. USART2: 115200 baud, 8N1.
Wiring: PA2 (USART2_TX) -> ESP8266 RX, PA3 (USART2_RX) <- ESP8266 TX, common GND.
Power the ESP-01 from a 3.3 V regulator that can supply at least 300 mA. */
#include "main.h"
#include <stdio.h>
#include <string.h>
extern UART_HandleTypeDef huart2;
#define WIFI_SSID "your-ssid"
#define WIFI_PASS "your-password"
#define SERVER_IP "192.168.1.100" /* PC or server that listens on TCP port 5000 */
#define SERVER_PORT 5000
/* Send an AT command and wait until 'expect' appears in the reply.
Returns 1 on success, 0 on timeout, "ERROR" or "FAIL". */
static int ESP_Cmd(const char *cmd, const char *expect, uint32_t timeout_ms)
{
static char rx[256];
uint16_t n = 0;
uint8_t ch;
uint32_t start = HAL_GetTick();
memset(rx, 0, sizeof(rx));
if (cmd) HAL_UART_Transmit(&huart2, (const uint8_t *)cmd, (uint16_t)strlen(cmd), 1000);
while ((HAL_GetTick() - start) < timeout_ms) {
if (HAL_UART_Receive(&huart2, &ch, 1, 10) == HAL_OK) {
if (n < sizeof(rx) - 1) rx[n++] = (char)ch;
if (strstr(rx, expect)) return 1;
if (strstr(rx, "ERROR") || strstr(rx, "FAIL")) return 0;
}
}
return 0;
}
static int ESP8266_Init(void)
{
char cmd[96];
ESP_Cmd("AT+RST\r\n", "ready", 5000); /* restart the module */
if (!ESP_Cmd("AT\r\n", "OK", 1000)) return 0; /* is it answering? */
if (!ESP_Cmd("ATE0\r\n", "OK", 1000)) return 0; /* turn echo off */
if (!ESP_Cmd("AT+CWMODE=1\r\n", "OK", 1000)) return 0; /* station mode */
snprintf(cmd, sizeof(cmd), "AT+CWJAP=\"%s\",\"%s\"\r\n", WIFI_SSID, WIFI_PASS);
return ESP_Cmd(cmd, "OK", 20000); /* joining can take 10 s or more */
}
static int ESP8266_SendTCP(const char *data)
{
char cmd[80];
snprintf(cmd, sizeof(cmd), "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", SERVER_IP, SERVER_PORT);
if (!ESP_Cmd(cmd, "OK", 5000)) return 0; /* open the connection */
snprintf(cmd, sizeof(cmd), "AT+CIPSEND=%u\r\n", (unsigned)strlen(data));
if (!ESP_Cmd(cmd, ">", 2000)) return 0; /* wait for the '>' prompt */
if (!ESP_Cmd(data, "SEND OK", 5000)) return 0; /* send the payload */
ESP_Cmd("AT+CIPCLOSE\r\n", "OK", 2000); /* close the connection */
return 1;
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
HAL_Delay(1000); /* give the ESP8266 time to boot */
while (!ESP8266_Init()) {
HAL_Delay(5000); /* retry until it joins the Wi-Fi network */
}
while (1) {
ESP8266_SendTCP("Hello World!\r\n");
HAL_Delay(5000);
}
}
The code has three main parts. First, ESP_Cmd() sends one AT command and waits for the expected reply, with a timeout. Next, ESP8266_Init() resets the module, switches it to station mode and joins your network. Finally, ESP8266_SendTCP() opens a TCP connection, sends the data and closes the connection.
In main(), the program sets up the clock, GPIO and UART. Then it keeps trying to join the Wi-Fi network until it succeeds. After that, it sends “Hello World!” to the server every 5 seconds.
Note: this example reads replies one byte at a time, which is fine for learning. For a product, receive the UART data with interrupts or DMA into a ring buffer instead.
Before you run the code, check the wiring. The ESP8266 TX pin goes to the STM32 RX pin, and the ESP8266 RX pin goes to the STM32 TX pin.
Next, change WIFI_SSID and WIFI_PASS to match your network. Also set SERVER_IP and SERVER_PORT to a computer that listens for TCP connections. For a quick test, a free TCP server app on your PC works well.
Finally, build the project and flash it to your STM32 with STM32CubeIDE or your preferred toolchain.
Conclusion
To conclude, an ESP8266 is a simple and cheap way to add Wi-Fi to an STM32 project. With the example code above, you can start sending data from your own devices today.
Read more on
- How to interface DHT11 with STM32
- Designing a BMS with STM32
- STM32 Bluetooth Low Energy & its application
- STM32 Development Boards: A Guide for Beginners
- How to interface the EM18 RFID module with STM32
Liked this article? Subscribe to our newsletter:
or,
Visit LabProjectsBD.com for more inspiring projects and tutorials.
Thank you!
Have a project in mind? LabProjectsBD designs power electronics, embedded firmware, IoT devices and production-ready PCBs for clients. Message us on WhatsApp, use the contact form, or see our services and portfolio.



4 Comments
Dewo · 11/06/2023 at 10:48 pm
How to debug ESP8266 with HAL lib ?
MKDas · 14/06/2023 at 12:00 pm
may be not possible.
Karlsson · 10/07/2023 at 1:57 am
Hello!
Is it possible to build ESP8266 device without STM32 or external mictrocontroller is always needed for ESP8266 or ESP32-S?
MKDas · 15/07/2023 at 8:11 pm
not impossible