In this tutorial, we’ll discuss how to use the ESP32 AT commands (ESP-AT) with STM32 microcontrollers, using an ESP32-C3 as a WiFi co-processor. You’ll learn how the ESP-AT firmware works, which ESP-AT version to use, how to flash it on an ESP32-C3, how to wire the ESP32-C3 to an STM32 Blue Pill, and how to configure the STM32 UART with DMA in CubeMX to talk to it.
Then, we’ll build four STM32 ESP32-C3 example projects with our own non-blocking ESP_AT driver: AT commands, connecting to WiFi with automatic reconnection, a TCP client that sends and receives data, and network time (SNTP) with ping. We’ll also compare the ESP32-C3 and the ESP8266 on the bench, and finish with a complete ESP32 AT commands list with the real replies from our module. This is the third 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), WeAct ESP32-C3 Core Board with ESP-AT firmware.
STM32 ESP32-C3 WiFi: How It Works
ESP-AT is Espressif’s official AT command firmware for its ESP32 chips. It turns an ESP32 into a WiFi co-processor: the STM32 sends text commands over a UART ( AT+CWJAP="MyWiFi","password" to join a network, AT+CIPSTART to open a TCP connection, AT+CIPSEND to send data), and the ESP32 answers with text replies ( OK, ERROR, +IPD,... for received data). The ESP32 runs the TCP/IP stack, TLS, DNS, and even an HTTP and MQTT client, so the STM32 needs none of them.
ESP-AT supports the ESP32, ESP32-C2, ESP32-C3, ESP32-C5, ESP32-C6, ESP32-C61, and ESP32-S2. The ESP32-C3 is the best fit for an STM32 project: it’s one of the cheapest chips in the family, its AT port is a separate UART from its boot log, and it gets every ESP-AT release. The ESP8266 runs an older AT firmware, which we covered in the ESP8266 AT commands with STM32 tutorial. The commands are almost the same, so the same STM32 driver works with both.

ESP-AT Firmware: Versions and Download
Espressif publishes ready-made ESP-AT binaries for each chip and module. For the ESP32-C3, the binary for the ESP32-C3-MINI-1 module works on most ESP32-C3 boards with 4MB of flash, including the WeAct ESP32-C3 Core Board and the ESP32-C3 SuperMini. Its AT port is UART1:
| Function | ESP32-C3 Pin | Connects To |
|---|---|---|
| AT commands in (UART1 RX) | GPIO6 | STM32 TX |
| AT replies out (UART1 TX) | GPIO7 | STM32 RX |
| Flow control (optional) | GPIO5 (CTS), GPIO4 (RTS) | Not used in this tutorial |
| Firmware download and boot log (UART0) | GPIO20 (RX), GPIO21 (TX), or the USB port | Your PC |
The pins come from Espressif’s ESP32-C3 AT hardware connection guide. Because the AT port is not the boot-log port, the STM32 never receives the garbage characters that an ESP8266 prints at power-up.
| ESP-AT Version | Released | How to Get It |
|---|---|---|
| v4.1.1.0 (the one we use) | July 2025 | Direct download from Espressif’s AT binary list (ESP32-C3-MINI-1-AT-V4.1.1.0.zip) |
| v4.2.0.0 | Newer | Espressif asks you to fill in a request form first |
Our WeAct ESP32-C3 Core Board answered AT+GMR with:
|
1 2 3 4 5 6 |
AT version:4.1.1.0(ba4dd0e - ESP32C3 - Jul 31 2025 08:38:07) SDK version:v5.4.1-643-g8ad0d3d8f2-dirty compile time(7c092f9a):Aug 1 2025 05:35:19 Bin version:v4.1.1.0(MINI-1) OK |
Note: An ESP32-C3 board is usually sold with a demo program, not with the AT firmware. If AT gets no answer on GPIO6/GPIO7 at 115200 baud, flash the firmware first (next section).
Flashing the ESP-AT Firmware on the ESP32-C3
Download the ESP32-C3-MINI-1 AT zip and unzip it. The factory folder has one image, factory_MINI-1.bin, that holds everything (bootloader, partition table, AT application, and default settings). Connect the ESP32-C3 board to your PC with its USB cable and write the image to address 0x0 with esptool:
|
1 |
esptool --chip esp32c3 --port COM5 --baud 460800 write-flash 0x0 factory/factory_MINI-1.bin |
Replace COM5 with your board’s port. If you have the Arduino ESP32 core installed, esptool is already on your PC (in the core’s tools/esptool_py folder), or install it with pip install esptool. If the board doesn’t connect, hold its BOOT button (GPIO9) while you press RESET, and try again. Espressif’s Flash Download Tool for Windows does the same with a GUI.
STM32 ESP32-C3 Wiring
Required Parts
| QTY. | Component Name | Amazon.com | AliExpress |
|---|---|---|---|
| 1 | STM32 Blue Pill (STM32F103C8T6) | Amazon | AliExpress |
| 1 | ST-Link V2 Debugger | Amazon | AliExpress |
| 1 | ESP32-C3 board (ESP32-C3 SuperMini; we use the WeAct ESP32-C3 Core Board) | Amazon | AliExpress |
| 1 | USB-TTL (UART) Converter, 3.3V | Amazon | AliExpress |
| 1 | BreadBoard | Amazon | AliExpress |
| 1 | Jumper Wires Pack | Amazon | AliExpress |
STM32 Blue Pill to ESP32-C3 Wiring
Connect the STM32’s USART1 to the ESP32-C3’s AT port (UART1), and USART2’s TX pin to your USB-TTL converter for the log (see the Blue Pill pinout for the pin locations). TX goes to RX, RX goes to TX, and all the grounds are connected. The ESP32-C3 board is powered from its own USB port, so only the ground is shared:
PA9 USART1 TX, goes to the ESP32-C3 GPIO6 (AT RX): the AT commands.
PA10 USART1 RX, goes to the ESP32-C3 GPIO7 (AT TX): the replies, events, and received data.
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.


The ST-Link stays on the Blue Pill’s SWD header (SWDIO, SWCLK, GND) as usual and isn’t shown in the diagram.
Note: Power the ESP32-C3 board from its own USB port (or 5V on its 5V pin), and power the Blue Pill from its micro-USB port, not from the ST-Link’s 3.3V pin. The ESP32-C3’s on-board regulator then supplies its WiFi transmit peaks, and the 3.3V supply sag that we measured with the ESP8266 doesn’t happen.
Testing ESP32 AT Commands With a USB-TTL Converter
Before connecting the ESP32-C3 to the STM32, it’s worth a quick test from your PC. Connect the ESP32-C3’s GPIO7 to the converter’s RX, GPIO6 to its TX, and GND to GND, open a serial terminal at 115200 baud with CR+LF line endings, and type AT. The module answers OK. Then try AT+GMR and AT+CWMODE?. If nothing comes back, the AT firmware isn’t on the board yet, or TX and RX are swapped.
STM32 CubeMX Configuration Steps
All four examples use the same CubeMX configuration as the ESP8266 tutorial: USART1 with DMA for the ESP32-C3, USART2 for the log, and the on-board LED on PC13.
#1
Open STM32CubeMX, create a new project, and select the STM32F103C8T6 in the MCU selector
#2
Set SYS > Debug to Serial Wire

#3
Set RCC > High Speed Clock (HSE) to Crystal/Ceramic Resonator, and set HCLK to 72MHz in the Clock Configuration tab


#4
Enable USART1 in Asynchronous mode (115200 baud, 8N1)

#5
In USART1’s DMA Settings tab, add USART1_RX with Mode set to Circular, and add USART1_TX (Mode Normal)

#6
In USART1’s NVIC Settings tab, enable the USART1 global interrupt (the two DMA channel interrupts are enabled by CubeMX)

#7
Enable USART2 in Asynchronous mode (115200 baud, 8N1) for the log

#8
Set PC13 as GPIO_Output with the user label LED (the Blue Pill’s on-board LED)

Here’s the finished pin configuration:

#9
In Project Manager, name the project, set the Toolchain/IDE to STM32CubeIDE, and click Generate Code

STM32 ESP-AT Commands Driver (ESP_AT)
All the examples use our ESP_AT driver from the ESP8266 tutorial: plain C, HAL called directly, two files ( ESP_AT.c and ESP_AT.h), no dynamic memory, about 9.1KB of flash and 3.9KB of RAM. The STM32 receives every byte from the ESP32-C3 with DMA in circular mode, and a line parser turns the stream into replies, events, and received data, so nothing is lost while the main loop is busy:

The driver reads AT+GMR and picks the right command form for each firmware. On ESP-AT, it uses AT+CWJAP and AT+CIPSTA? (the ESP8266 NonOS firmware uses AT+CWJAP_CUR and AT+CIPSTA_CUR?), and it parses +PING:<ms> replies. Your application code doesn’t change between the two modules.
Testing on the ESP32-C3 also showed why the driver’s ESP_AT_Begin() syncs with the module first. When the STM32 resets, the ESP32-C3 keeps running: an unfinished AT+CIPSEND left late replies in the line, and a TCP link left open made AT+CIPMUX answer ERROR. So Begin now waits for a clean AT / OK exchange, closes any open link, and only then sets the connection mode.
| Function | What It Does |
|---|---|
ESP_AT_Init(&esp, &cfg) | Checks the UART and DMA setup and starts the reception |
ESP_AT_Process(&esp) | Runs the parser, the jobs, and the timers. Call it in the main loop as often as possible |
ESP_AT_Begin(&esp) | Auto-baud, sync, ATE0, AT+GMR (firmware detection), closes old links, optional baud switch |
ESP_AT_Command(&esp, cmd, timeout) | Sends any AT command and collects its reply |
ESP_AT_WiFiJoin(&esp, ssid, pwd) | Station mode, joins the network, and reads the IP address |
ESP_AT_Connect(&esp, link, type, host, port) | Opens a TCP, UDP, or SSL connection |
ESP_AT_Send(&esp, link, data, len) | Sends data with AT+CIPSEND |
ESP_AT_Ping(&esp, host) | Pings a host, the round trip goes to esp.pingMs |
ESP_AT_..._Blocking() | The same jobs, waiting until they finish (for simple programs) |
STM32 ESP32-C3 WiFi Example 1: AT Commands
In the first example project, the STM32 finds the ESP32-C3, reads its firmware version, and sends ten common AT commands, printing each reply and how long it took.
Wiring: PA9 to ESP32-C3 GPIO6, PA10 to GPIO7, PA2 to the USB-TTL RX, and GND to both.
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
/* USER CODE BEGIN Includes */ #include <stdio.h> #include <string.h> #include <stdarg.h> #include "ESP_AT.h" #include "wifi_secrets.h" /* USER CODE END Includes */ /* USER CODE BEGIN 0 */ /* One handle per ESP module */ static ESP_AT_HandleType esp; /* printf-style log on USART2 (the USB-UART adapter to the PC) */ static void Log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); static void Log(const char *fmt, ...) { char buf[200]; va_list ap; va_start(ap, fmt); int n = vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); if (n > (int)sizeof(buf) - 1) n = (int)sizeof(buf) - 1; if (n > 0) HAL_UART_Transmit(&huart2, (uint8_t *)buf, (uint16_t)n, 100); } /* Driver traffic log: ">>" = sent to the module, "<<" = received, "--" = driver note */ static void OnTrace(ESP_AT_HandleType *h, ESP_AT_TraceType type, const char *text) { (void)h; Log("%s %s\r\n", (type == ESP_AT_TRACE_TX) ? ">>" : (type == ESP_AT_TRACE_RX) ? "<<" : "--", text); } /* The HAL stops the DMA reception after a UART error; the driver restarts it */ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { ESP_AT_UART_ErrorCallback(&esp, huart); } /* The commands this demo sends, with their timeouts. Each reply is printed by OnTrace. */ static const struct { const char *cmd; uint32_t timeout_ms; } demoCommands[] = { { "AT", 1000 }, /* is the module there? */ { "AT+GMR", 1000 }, /* firmware versions */ { "AT+CWMODE?", 1000 }, /* WiFi mode: 1 station, 2 SoftAP, 3 both */ { "AT+CWMODE=1", 1000 }, /* station mode (join a router) */ { "AT+CWLAP", 10000 }, /* scan for WiFi networks */ { "AT+CIPMUX?", 1000 }, /* single (0) or multiple (1) connections */ { "AT+CIFSR", 1000 }, /* IP and MAC addresses */ { "AT+CIPSTATUS", 1000 }, /* connection status */ { "AT+UART_CUR?", 1000 }, /* current UART settings (not on AT 0.2x) */ { "AT+SYSRAM?", 1000 }, /* free heap (ESP-AT only) */ }; /* USER CODE END 0 */ /* Inside main(), after the MX_..._Init() calls */ /* USER CODE BEGIN 2 */ ESP_AT_ConfigType cfg = {0}; ESP_AT_StatusType s; cfg.huart = &huart1; /* USART1: PA9 TX, PA10 RX, RX DMA circular */ cfg.onTrace = OnTrace; /* print every line sent and received */ Log("\r\n=== ESP_AT Demo 1: talk to the ESP module ===\r\n"); if (ESP_AT_Init(&esp, &cfg) != ESP_AT_OK) { Log("Init failed: check that USART1 RX DMA is in circular mode\r\n"); Error_Handler(); } /* Auto-baud, ATE0, AT+GMR, AT+CIPMUX=0 */ s = ESP_AT_Begin_Blocking(&esp); if (s != ESP_AT_OK) { Log("Begin failed: %s (check wiring, power, EN pin)\r\n", ESP_AT_StatusText(s)); } else { Log("\r\nModule found at %lu baud\r\n", (unsigned long)esp.baudRate); Log("Firmware: %s -> %s, feature flags 0x%04X\r\n\r\n", esp.version, ESP_AT_FirmwareText(esp.firmware), (unsigned)esp.features); for (uint32_t i = 0; i < sizeof(demoCommands) / sizeof(demoCommands[0]); i++) { s = ESP_AT_Command_Blocking(&esp, demoCommands[i].cmd, demoCommands[i].timeout_ms); Log("[%s in %lu ms]\r\n\r\n", ESP_AT_StatusText(s), (unsigned long)esp.lastCmdMs); } Log("Demo 1 done. Lines the module sends by itself are still printed below.\r\n"); } uint32_t ledTime = HAL_GetTick(); /* USER CODE END 2 */ /* Inside while (1) */ /* USER CODE BEGIN 3 */ ESP_AT_Process(&esp); /* keeps parsing what the module sends */ if (HAL_GetTick() - ledTime >= 500) { ledTime = HAL_GetTick(); HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); } } /* USER CODE END 3 */ |
STM32 ESP32-C3 Example 1 Code Explanation
OnTrace() prints every line the driver sends ( >>) and receives ( <<) on USART2, so you see the whole AT exchange. ESP_AT_Begin_Blocking() finds the module’s baud rate, syncs with it, turns the echo off, and reads AT+GMR. Then the loop sends each command in demoCommands[] with ESP_AT_Command_Blocking() and prints the result and its time.
STM32 ESP32-C3 Example 1 Testing
This is the real hardware testing result for this example project on my Blue Pill and ESP32-C3 (network names and access point MAC addresses are hidden):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
=== ESP_AT Demo 1: talk to the ESP module === >> AT << OK -- module answers at 115200 baud >> AT << OK >> ATE0 << OK >> AT+GMR << AT version:4.1.1.0(ba4dd0e - ESP32C3 - Jul 31 2025 08:38:07) << SDK version:v5.4.1-643-g8ad0d3d8f2-dirty << compile time(7c092f9a):Aug 1 2025 05:35:19 << Bin version:v4.1.1.0(MINI-1) << OK >> AT+CIPMUX? << +CIPMUX:0 << OK >> AT+CIPCLOSE << ERROR >> AT+CIPMUX=0 << OK Module found at 115200 baud Firmware: AT version:4.1.1.0(ba4dd0e - ESP32C3 - Jul 31 2025 08:38:07) -> ESP-AT 2.x+, feature flags 0x01F7 >> AT << OK [OK in 3 ms] >> AT+GMR << AT version:4.1.1.0(ba4dd0e - ESP32C3 - Jul 31 2025 08:38:07) << SDK version:v5.4.1-643-g8ad0d3d8f2-dirty << compile time(7c092f9a):Aug 1 2025 05:35:19 << Bin version:v4.1.1.0(MINI-1) << OK [OK in 27 ms] >> AT+CWMODE? << +CWMODE:1 << OK [OK in 6 ms] >> AT+CWMODE=1 << OK [OK in 4 ms] >> AT+CWLAP << +CWLAP:(3,"",-51,"xx:xx:xx:xx:xx:xx",9,-1,-1,5,3,7,0) << +CWLAP:(3,"MyWiFi",-52,"xx:xx:xx:xx:xx:xx",9,-1,-1,4,4,7,1) << +CWLAP:(3,"MyWiFi",-66,"xx:xx:xx:xx:xx:xx",9,-1,-1,4,4,7,0) << +CWLAP:(4,"",-66,"xx:xx:xx:xx:xx:xx",9,-1,-1,4,4,7,0) << +CWLAP:(3,"Network1",-73,"xx:xx:xx:xx:xx:xx",3,-1,-1,4,4,7,1) << +CWLAP:(4,"",-75,"xx:xx:xx:xx:xx:xx",1,-1,-1,5,3,7,0) << +CWLAP:(4,"",-76,"xx:xx:xx:xx:xx:xx",1,-1,-1,5,3,7,0) << +CWLAP:(4,"",-76,"xx:xx:xx:xx:xx:xx",4,-1,-1,5,3,7,0) << +CWLAP:(4,"",-78,"xx:xx:xx:xx:xx:xx",4,-1,-1,5,3,7,0) << +CWLAP:(4,"",-79,"xx:xx:xx:xx:xx:xx",11,-1,-1,5,3,7,0) << +CWLAP:(4,"Network2",-82,"xx:xx:xx:xx:xx:xx",9,-1,-1,5,3,7,1) << +CWLAP:(3,"",-84,"xx:xx:xx:xx:xx:xx",2,-1,-1,4,4,7,0) << +CWLAP:(4,"Network3",-86,"xx:xx:xx:xx:xx:xx",5,-1,-1,5,3,7,0) << +CWLAP:(3,"",-89,"xx:xx:xx:xx:xx:xx",3,-1,-1,4,4,7,1) << +CWLAP:(0,"",-94,"xx:xx:xx:xx:xx:xx",5,-1,-1,0,0,7,0) << +CWLAP:(3,"",-95,"xx:xx:xx:xx:xx:xx",1,-1,-1,5,3,7,1) << +CWLAP:(5,"",-95,"xx:xx:xx:xx:xx:xx",5,-1,-1,5,3,7,0) << OK [OK in 2107 ms] >> AT+CIPMUX? << +CIPMUX:0 << OK [OK in 7 ms] >> AT+CIFSR << +CIFSR:STAIP,"192.168.1.106" << +CIFSR:STAMAC,"60:55:f9:af:ae:90" << OK [OK in 13 ms] >> AT+CIPSTATUS << STATUS:2 << OK [OK in 7 ms] >> AT+UART_CUR? << +UART_CUR:115211,8,1,0,0 << OK [OK in 9 ms] >> AT+SYSRAM? << +SYSRAM:133892,128424 << OK [OK in 8 ms] |
Most commands answered in 3 to 27ms. The WiFi scan ( AT+CWLAP) took 2.1 seconds and found 18 networks, and AT+SYSRAM? shows about 134KB of free RAM inside the ESP32-C3, more than twice the ESP8266’s 51KB.
STM32 ESP32-C3 Example 2: Connect to WiFi and Get IP Address
In this example project, the STM32 joins your WiFi network, prints the IP address, gateway, and netmask, and then runs a non-blocking loop that keeps the connection alive:
- The on-board LED is on while the ESP32-C3 is connected, and blinks fast while it isn’t.
- When the module reports WIFI DISCONNECT, the driver joins the network again by itself.
- To show this without touching the router, the example drops the WiFi once with AT+CWQAP 30 seconds after the start.
Write your WiFi name and password in Core/Inc/wifi_secrets.h:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* * wifi_secrets.h * Your WiFi network name and password. Keep this file out of any project you share. */ #ifndef WIFI_SECRETS_H_ #define WIFI_SECRETS_H_ #define WIFI_SSID "YOUR_WIFI_NAME" #define WIFI_PASSWORD "YOUR_WIFI_PASSWORD" /* Example 3 only: the PC that runs tcp_echo_server.py (same network as the ESP module) */ #define SERVER_IP "192.168.1.100" #define SERVER_PORT 5000 #endif /* 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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
/* USER CODE BEGIN Includes */ #include <stdio.h> #include <string.h> #include <stdarg.h> #include "ESP_AT.h" #include "wifi_secrets.h" /* USER CODE END Includes */ /* USER CODE BEGIN 0 */ /* One handle per ESP module */ static ESP_AT_HandleType esp; /* printf-style log on USART2 (the USB-UART adapter to the PC) */ static void Log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); static void Log(const char *fmt, ...) { char buf[200]; va_list ap; va_start(ap, fmt); int n = vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); if (n > (int)sizeof(buf) - 1) n = (int)sizeof(buf) - 1; if (n > 0) HAL_UART_Transmit(&huart2, (uint8_t *)buf, (uint16_t)n, 100); } /* Driver traffic log: ">>" = sent to the module, "<<" = received, "--" = driver note */ static void OnTrace(ESP_AT_HandleType *h, ESP_AT_TraceType type, const char *text) { (void)h; Log("%s %s\r\n", (type == ESP_AT_TRACE_TX) ? ">>" : (type == ESP_AT_TRACE_RX) ? "<<" : "--", text); } /* The HAL stops the DMA reception after a UART error; the driver restarts it */ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { ESP_AT_UART_ErrorCallback(&esp, huart); } /* 1: drop the WiFi once, 30 s after the start, with a raw AT+CWQAP (like a router going away), * to show the auto reconnect without touching the router. 0: off */ #define DEMO_SIMULATE_DROP 1 /* Events from the driver: WiFi state changes and the end of each job */ static void OnEvent(ESP_AT_HandleType *h, ESP_AT_EventType ev, const ESP_AT_EventDataType *d) { switch (ev) { case ESP_AT_EVT_WIFI_CONNECTED: Log("[event] WiFi connected (t = %lu ms)\r\n", (unsigned long)HAL_GetTick()); break; case ESP_AT_EVT_WIFI_GOT_IP: Log("[event] WiFi got an IP (t = %lu ms)\r\n", (unsigned long)HAL_GetTick()); break; case ESP_AT_EVT_WIFI_DISCONNECTED: Log("[event] WiFi disconnected (t = %lu ms), auto reconnect is on\r\n", (unsigned long)HAL_GetTick()); break; case ESP_AT_EVT_READY: Log("[event] the module restarted\r\n"); break; case ESP_AT_EVT_JOB_DONE: Log("[event] job %s done: %s\r\n", ESP_AT_JobText(d->job), ESP_AT_StatusText(d->status)); if ((d->job == ESP_AT_JOB_JOIN || d->job == ESP_AT_JOB_GET_IP) && d->status == ESP_AT_OK) Log(" IP %s, gateway %s, netmask %s\r\n", h->ip, h->gateway, h->netmask); break; case ESP_AT_EVT_URC: Log("[event] module says: %s\r\n", d->line); break; default: break; } } /* USER CODE END 0 */ /* Inside main(), after the MX_..._Init() calls */ /* USER CODE BEGIN 2 */ ESP_AT_ConfigType cfg = {0}; ESP_AT_StatusType s; cfg.huart = &huart1; cfg.autoReconnect = 1; /* rejoin by itself after WIFI DISCONNECT */ cfg.onEvent = OnEvent; cfg.onTrace = OnTrace; Log("\r\n=== ESP_AT Demo 2: WiFi join, IP, auto reconnect ===\r\n"); if (ESP_AT_Init(&esp, &cfg) != ESP_AT_OK) Error_Handler(); s = ESP_AT_Begin_Blocking(&esp); Log("Begin: %s, %s at %lu baud\r\n", ESP_AT_StatusText(s), ESP_AT_FirmwareText(esp.firmware), (unsigned long)esp.baudRate); /* Old AT 0.2x firmware sends no WIFI DISCONNECT message: poll the WiFi state instead */ if (!(esp.features & ESP_AT_FEAT_WIFI_EVENTS)) esp.cfg.wifiCheckMs = 5000; uint32_t t0 = HAL_GetTick(); s = ESP_AT_WiFiJoin_Blocking(&esp, WIFI_SSID, WIFI_PASSWORD); Log("Join: %s in %lu ms", ESP_AT_StatusText(s), (unsigned long)(HAL_GetTick() - t0)); if (s == ESP_AT_OK) Log(", IP %s\r\n", esp.ip); else Log(", +CWJAP error code %d (1 timeout, 2 password, 3 no AP, 4 failed)\r\n", esp.joinError); uint32_t ledTime = HAL_GetTick(), statusTime = HAL_GetTick(); uint8_t dropped = !DEMO_SIMULATE_DROP; /* USER CODE END 2 */ /* Inside while (1) */ /* USER CODE BEGIN 3 */ ESP_AT_Process(&esp); /* parser, events, auto reconnect: all in here */ /* LED: steady on while connected, fast blink while not (PC13 LED is active low) */ if (ESP_AT_IsWiFiConnected(&esp)) HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); else if (HAL_GetTick() - ledTime >= 100) { ledTime = HAL_GetTick(); HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); } /* Status line every 10 s */ if (HAL_GetTick() - statusTime >= 10000) { statusTime = HAL_GetTick(); Log("[status] t = %lu s, WiFi %s, IP %s\r\n", (unsigned long)(statusTime / 1000), ESP_AT_IsWiFiConnected(&esp) ? "connected" : "NOT connected", esp.ip[0] ? esp.ip : "-"); } /* Simulated router drop: a raw command, so the driver still has the network and rejoins it */ if (!dropped && HAL_GetTick() > 30000 && !ESP_AT_IsBusy(&esp)) { dropped = 1; Log("[demo] dropping the WiFi with AT+CWQAP to test the auto reconnect\r\n"); (void)ESP_AT_Command(&esp, "AT+CWQAP", 2000); } } /* USER CODE END 3 */ |
STM32 ESP32-C3 Example 2 Code Explanation
Setting cfg.autoReconnect = 1 keeps the connection: the driver remembers the network and joins it again after every WIFI DISCONNECT. OnEvent() receives the WiFi events and the end of each job, and the main loop only calls ESP_AT_Process() and updates the LED.
Note: On ESP-AT, AT+CWJAP also saves the network in the ESP32-C3’s flash ( AT+SYSSTORE is 1 by default), and the module joins it again by itself after a reset. Use AT+SYSSTORE=0 first if you don’t want it saved.
STM32 ESP32-C3 Example 2 Testing
This is the real hardware testing result for this example project:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
=== ESP_AT Demo 2: WiFi join, IP, auto reconnect === >> AT << OK -- module answers at 115200 baud >> AT << OK >> ATE0 << OK >> AT+GMR << AT version:4.1.1.0(ba4dd0e - ESP32C3 - Jul 31 2025 08:38:07) << SDK version:v5.4.1-643-g8ad0d3d8f2-dirty << compile time(7c092f9a):Aug 1 2025 05:35:19 << Bin version:v4.1.1.0(MINI-1) << OK >> AT+CIPMUX? << +CIPMUX:0 << OK >> AT+CIPCLOSE << ERROR >> AT+CIPMUX=0 << OK [event] job BEGIN done: OK Begin: OK, ESP-AT 2.x+ at 115200 baud >> AT+CWMODE=1 << OK >> AT+CWJAP="MyWiFi","********" << WIFI DISCONNECT << WIFI CONNECTED [event] WiFi connected (t = 310 ms) << WIFI GOT IP [event] WiFi got an IP (t = 1334 ms) << OK >> AT+CIPSTA? << +CIPSTA:ip:"192.168.1.201" << +CIPSTA:gateway:"192.168.1.1" << +CIPSTA:netmask:"255.255.255.0" << OK [event] job JOIN done: OK IP 192.168.1.201, gateway 192.168.1.1, netmask 255.255.255.0 Join: OK in 1162 ms, IP 192.168.1.201 [status] t = 11 s, WiFi connected, IP 192.168.1.201 [status] t = 21 s, WiFi connected, IP 192.168.1.201 [demo] dropping the WiFi with AT+CWQAP to test the auto reconnect >> AT+CWQAP << WIFI DISCONNECT [event] WiFi disconnected (t = 30025 ms), auto reconnect is on << OK [event] job COMMAND done: OK [status] t = 31 s, WiFi NOT connected, IP - -- auto reconnect: joining again >> AT+CWMODE=1 << OK >> AT+CWJAP="MyWiFi","********" << WIFI CONNECTED [event] WiFi connected (t = 32134 ms) << WIFI GOT IP [event] WiFi got an IP (t = 33159 ms) << OK >> AT+CIPSTA? << +CIPSTA:ip:"192.168.1.201" << +CIPSTA:gateway:"192.168.1.1" << +CIPSTA:netmask:"255.255.255.0" << OK [event] job JOIN done: OK IP 192.168.1.201, gateway 192.168.1.1, netmask 255.255.255.0 |
The ESP32-C3 joined the network in 1.2 seconds, with WIFI GOT IP (the router’s DHCP answer) 1.0 second after WIFI CONNECTED. After the simulated drop at 30 seconds, it was back with an IP address 3.1 seconds later. The ESP8266 needed 5 to 6 seconds for the same join on the same router.
STM32 ESP32-C3 WiFi Example 3: TCP Client (Send and Receive Data)
In this example project, the STM32 connects to a TCP server on your PC through the ESP32-C3 and exchanges data with it in both directions:
- The STM32 sends a text line every 2 seconds.
- The PC server sends each line back ( echo: ...), and the STM32 measures how long the echo takes.
- Every 6 seconds, the server sends LED TOGGLE, and the STM32 toggles its on-board LED.
- If the server stops, the STM32 tries to reconnect every 3 seconds.
Data goes out with AT+CIPSEND=<length>: the STM32 waits for the > prompt, sends exactly that many bytes, and the module answers Recv <n> bytes and SEND OK. Data comes in at any time as +IPD,<length>: followed by the data, and the driver reads it by its length. The ESP8266 tutorial shows this exchange step by step.
The PC TCP Server
Run this Python script on your PC (Python 3, no extra packages), and set SERVER_IP in wifi_secrets.h to your PC’s IP address. Allow Python through the Windows firewall when it asks.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
"""TCP test server for ESP_AT Demo 3 (STM32 + ESP module as a TCP client). - Greets each client, echoes every line back as "echo: <line>", - sends an LED command every 6 seconds (LED TOGGLE), - prints everything with a timestamp. Stop with Ctrl+C. python tcp_echo_server.py [port] [--seconds N] --seconds N stops the server after N seconds (for scripted bench runs). """ import socket, sys, time, threading args = [a for a in sys.argv[1:] if not a.startswith("--")] port = int(args[0]) if args else 5000 stop_after = None if "--seconds" in sys.argv: stop_after = float(sys.argv[sys.argv.index("--seconds") + 1]) t_start = time.time() def log(msg): print(f"[{time.time() - t_start:8.3f}] {msg}", flush=True) def client(conn, addr): log(f"client {addr[0]}:{addr[1]} connected") conn.settimeout(0.2) buf = b"" next_cmd = time.time() + 6 try: conn.sendall(b"Hello STM32, this is the PC\n") while True: if time.time() >= next_cmd: conn.sendall(b"LED TOGGLE\n") log("sent: LED TOGGLE") next_cmd = time.time() + 6 try: d = conn.recv(4096) except socket.timeout: continue if not d: break buf += d while b"\n" in buf: line, buf = buf.split(b"\n", 1) text = line.decode(errors="replace").rstrip("\r") log(f"recv: {text}") conn.sendall(b"echo: " + line + b"\n") except OSError as e: log(f"connection error: {e}") finally: conn.close() log(f"client {addr[0]} closed") srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srv.bind(("0.0.0.0", port)) srv.listen(4) srv.settimeout(0.5) log(f"listening on port {port}") while stop_after is None or time.time() - t_start < stop_after: try: c, a = srv.accept() except socket.timeout: continue threading.Thread(target=client, args=(c, a), daemon=True).start() log("stopped") |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
/* USER CODE BEGIN Includes */ #include <stdio.h> #include <string.h> #include <stdarg.h> #include "ESP_AT.h" #include "wifi_secrets.h" /* USER CODE END Includes */ /* USER CODE BEGIN 0 */ /* One handle per ESP module */ static ESP_AT_HandleType esp; /* printf-style log on USART2 (the USB-UART adapter to the PC) */ static void Log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); static void Log(const char *fmt, ...) { char buf[200]; va_list ap; va_start(ap, fmt); int n = vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); if (n > (int)sizeof(buf) - 1) n = (int)sizeof(buf) - 1; if (n > 0) HAL_UART_Transmit(&huart2, (uint8_t *)buf, (uint16_t)n, 100); } /* Driver traffic log: ">>" = sent to the module, "<<" = received, "--" = driver note */ static void OnTrace(ESP_AT_HandleType *h, ESP_AT_TraceType type, const char *text) { (void)h; Log("%s %s\r\n", (type == ESP_AT_TRACE_TX) ? ">>" : (type == ESP_AT_TRACE_RX) ? "<<" : "--", text); } /* The HAL stops the DMA reception after a UART error; the driver restarts it */ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { ESP_AT_UART_ErrorCallback(&esp, huart); } /* The application: a small state machine, so the main loop never waits */ typedef enum { APP_BEGIN, APP_WAIT_BEGIN, APP_JOIN, APP_WAIT_JOIN, APP_ONLINE } AppStateType; static char txMsg[96]; /* must stay unchanged until the send job ends */ static uint32_t txCount = 0; static uint32_t txTime = 0; /* when the last message was handed to the driver */ static char rxLine[96]; /* received text, assembled into lines */ static uint16_t rxLen = 0; static uint32_t rxBytes = 0; /* A complete line from the PC: commands for the LED, everything else is printed */ static void HandleServerLine(const char *line) { if (strcmp(line, "LED ON") == 0) HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); else if (strcmp(line, "LED OFF") == 0) HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET); else if (strcmp(line, "LED TOGGLE") == 0) HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); else if (strncmp(line, "echo: ", 6) == 0) { Log("[app] echo came back after %lu ms\r\n", (unsigned long)(HAL_GetTick() - txTime)); return; } else return; Log("[app] LED command: %s\r\n", line); } static void OnEvent(ESP_AT_HandleType *h, ESP_AT_EventType ev, const ESP_AT_EventDataType *d) { (void)h; switch (ev) { case ESP_AT_EVT_DATA: /* +IPD data: may arrive in several chunks (d->remaining = bytes still to come) */ rxBytes += d->len; Log("[event] +IPD link %u: %u bytes%s\r\n", d->link, d->len, d->remaining ? " (more follows)" : ""); for (uint16_t i = 0; i < d->len; i++) { char c = (char)d->data[i]; if (c == '\n' || rxLen >= sizeof(rxLine) - 1) { rxLine[rxLen] = '\0'; if (rxLen > 0 && rxLine[rxLen - 1] == '\r') rxLine[rxLen - 1] = '\0'; Log("[app] from PC: \"%s\"\r\n", rxLine); HandleServerLine(rxLine); rxLen = 0; } else rxLine[rxLen++] = c; } break; case ESP_AT_EVT_LINK_OPENED: Log("[event] link %u open\r\n", d->link); break; case ESP_AT_EVT_LINK_CLOSED: Log("[event] link %u closed\r\n", d->link); rxLen = 0; break; case ESP_AT_EVT_WIFI_DISCONNECTED: Log("[event] WiFi disconnected\r\n"); break; case ESP_AT_EVT_WIFI_GOT_IP: Log("[event] WiFi got an IP\r\n"); break; case ESP_AT_EVT_JOB_DONE: if (d->status != ESP_AT_OK || d->job != ESP_AT_JOB_SEND) Log("[event] job %s done: %s\r\n", ESP_AT_JobText(d->job), ESP_AT_StatusText(d->status)); break; default: break; } } /* USER CODE END 0 */ /* Inside main(), after the MX_..._Init() calls */ /* USER CODE BEGIN 2 */ ESP_AT_ConfigType cfg = {0}; AppStateType app = APP_BEGIN; uint32_t nextConnect = 0, nextSend = 0; cfg.huart = &huart1; cfg.autoReconnect = 1; cfg.onEvent = OnEvent; cfg.onTrace = OnTrace; Log("\r\n=== ESP_AT Demo 3: TCP client, send and receive (+IPD) ===\r\n"); Log("Server: %s:%u (run tools\\tcp_echo_server.py on the PC)\r\n", SERVER_IP, (unsigned)SERVER_PORT); if (ESP_AT_Init(&esp, &cfg) != ESP_AT_OK) Error_Handler(); /* USER CODE END 2 */ /* Inside while (1) */ /* USER CODE BEGIN 3 */ ESP_AT_Process(&esp); uint32_t now = HAL_GetTick(); switch (app) { case APP_BEGIN: /* start the module check */ if (ESP_AT_Begin(&esp) == ESP_AT_OK) app = APP_WAIT_BEGIN; break; case APP_WAIT_BEGIN: if (ESP_AT_GetJobStatus(&esp) == ESP_AT_RUNNING) break; if (ESP_AT_GetJobStatus(&esp) == ESP_AT_OK) { Log("[app] module ready: %s\r\n", esp.version); app = APP_JOIN; } else app = APP_BEGIN; /* try again */ break; case APP_JOIN: if (ESP_AT_WiFiJoin(&esp, WIFI_SSID, WIFI_PASSWORD) == ESP_AT_OK) app = APP_WAIT_JOIN; break; case APP_WAIT_JOIN: if (ESP_AT_GetJobStatus(&esp) == ESP_AT_RUNNING) break; if (ESP_AT_GetJobStatus(&esp) == ESP_AT_OK) { Log("[app] joined, IP %s\r\n", esp.ip); app = APP_ONLINE; } else app = APP_JOIN; break; case APP_ONLINE: if (ESP_AT_IsBusy(&esp) || !ESP_AT_IsWiFiConnected(&esp)) break; /* auto reconnect handles WiFi */ if (!ESP_AT_IsLinkOpen(&esp, 0)) { if ((int32_t)(now - nextConnect) >= 0) { nextConnect = now + 3000; /* retry every 3 s while the server is down */ (void)ESP_AT_Connect(&esp, 0, ESP_AT_LINK_TCP, SERVER_IP, SERVER_PORT); } } else if ((int32_t)(now - nextSend) >= 0) { nextSend = now + 2000; /* one line every 2 s */ int n = snprintf(txMsg, sizeof(txMsg), "Hello from STM32 #%lu, uptime %lu ms, received %lu bytes\n", (unsigned long)++txCount, (unsigned long)now, (unsigned long)rxBytes); txTime = now; if (ESP_AT_Send(&esp, 0, txMsg, (uint32_t)n) == ESP_AT_OK) Log("[app] sending #%lu (%d bytes)\r\n", (unsigned long)txCount, n); } break; } } /* USER CODE END 3 */ |
STM32 ESP32-C3 Example 3 Code Explanation
The application is a non-blocking state machine: begin, join, and then online, where it opens link 0 with ESP_AT_Connect() and sends a line every 2 seconds with ESP_AT_Send(). Received data comes as ESP_AT_EVT_DATA events in chunks of up to 256 bytes, which the example collects into lines and checks for the LED commands.
STM32 ESP32-C3 Example 3 Testing
This is the real hardware testing result for this example project (the first lines):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
=== ESP_AT Demo 3: TCP client, send and receive (+IPD) === Server: 192.168.1.101:5000 (run tools\tcp_echo_server.py on the PC) >> AT << OK -- module answers at 115200 baud >> AT << OK >> ATE0 << OK >> AT+GMR << AT version:4.1.1.0(ba4dd0e - ESP32C3 - Jul 31 2025 08:38:07) << SDK version:v5.4.1-643-g8ad0d3d8f2-dirty << compile time(7c092f9a):Aug 1 2025 05:35:19 << Bin version:v4.1.1.0(MINI-1) << OK >> AT+CIPMUX? << +CIPMUX:0 << OK >> AT+CIPCLOSE << ERROR >> AT+CIPMUX=0 << OK [event] job BEGIN done: OK [app] module ready: AT version:4.1.1.0(ba4dd0e - ESP32C3 - Jul 31 2025 08:38:07) >> AT+CWMODE=1 << OK >> AT+CWJAP="MyWiFi","********" << WIFI DISCONNECT << WIFI CONNECTED << WIFI GOT IP [event] WiFi got an IP << OK >> AT+CIPSTA? << +CIPSTA:ip:"192.168.1.201" << +CIPSTA:gateway:"192.168.1.1" << +CIPSTA:netmask:"255.255.255.0" << OK [event] job JOIN done: OK [app] joined, IP 192.168.1.201 >> AT+CIPSTART="TCP","192.168.1.101",5000 << CONNECT [event] link 0 open << OK [event] job CONNECT done: OK >> AT+CIPSEND=54 [app] sending #1 (54 bytes) << +IPD,28: [event] +IPD link 0: 28 bytes [app] from PC: "Hello STM32, this is the PC" << OK << > << Recv 54 bytes << SEND OK << +IPD,60: [event] +IPD link 0: 60 bytes [app] from PC: "echo: Hello from STM32 #1, uptime 1417 ms, received 0 bytes" [app] echo came back after 49 ms >> AT+CIPSEND=55 [app] sending #2 (55 bytes) << OK << > << Recv 55 bytes << SEND OK << +IPD,61: [event] +IPD link 0: 61 bytes [app] from PC: "echo: Hello from STM32 #2, uptime 3417 ms, received 88 bytes" [app] echo came back after 41 ms >> AT+CIPSEND=56 [app] sending #3 (56 bytes) << OK |
All 18 lines came back, with an echo round trip of 39 to 49ms, and all 5 LED TOGGLE commands toggled the LED.
STM32 ESP32-C3 Example 4: Network Time (SNTP) and Ping
ESP-AT has an SNTP client built in, so the STM32 can get the date and time from the internet with two commands, with no RTC setup and no NTP code. In this example project, the STM32 joins WiFi, sets the time zone and the NTP server, pings the router and www.google.com, and prints the local time every 10 seconds.
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
/* USER CODE BEGIN Includes */ #include <stdio.h> #include <string.h> #include <stdarg.h> #include "ESP_AT.h" #include "wifi_secrets.h" /* USER CODE END Includes */ /* USER CODE BEGIN 0 */ /* One handle per ESP module */ static ESP_AT_HandleType esp; /* printf-style log on USART2 (the USB-UART adapter to the PC) */ static void Log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); static void Log(const char *fmt, ...) { char buf[200]; va_list ap; va_start(ap, fmt); int n = vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); if (n > (int)sizeof(buf) - 1) n = (int)sizeof(buf) - 1; if (n > 0) HAL_UART_Transmit(&huart2, (uint8_t *)buf, (uint16_t)n, 100); } /* Driver traffic log: ">>" = sent to the module, "<<" = received, "--" = driver note */ static void OnTrace(ESP_AT_HandleType *h, ESP_AT_TraceType type, const char *text) { (void)h; Log("%s %s\r\n", (type == ESP_AT_TRACE_TX) ? ">>" : (type == ESP_AT_TRACE_RX) ? "<<" : "--", text); } /* The HAL stops the DMA reception after a UART error; the driver restarts it */ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { ESP_AT_UART_ErrorCallback(&esp, huart); } /* Settings for this demo */ #define TIME_ZONE 2 /* hours from UTC for the printed local time (-12 to 14) */ #define NTP_SERVER "pool.ntp.org" #define PING_HOST "www.google.com" /* a name: the module also does the DNS lookup */ #define PRINT_EVERY_MS 10000U /* The application: one step at a time, never waiting inside the main loop */ typedef enum { APP_BEGIN, APP_JOIN, APP_SNTP, APP_PING_GW, APP_PING_NET, APP_TIME, APP_IDLE } AppStepType; static AppStepType step = APP_BEGIN; static uint8_t started = 0; /* 1 while the job of this step runs */ static uint8_t finished = 0; /* set by the JOB_DONE event of this step's job */ static ESP_AT_StatusType result; static uint32_t nextTime = 0; /* The driver job each step starts (the driver may also run short jobs of its own) */ static ESP_AT_JobType StepJob(void) { static const ESP_AT_JobType jobs[] = { ESP_AT_JOB_BEGIN, ESP_AT_JOB_JOIN, ESP_AT_JOB_COMMAND, ESP_AT_JOB_PING, ESP_AT_JOB_PING, ESP_AT_JOB_COMMAND, ESP_AT_JOB_NONE }; return jobs[step]; } /* Start the job for the current step. Returns ESP_AT_BUSY while the driver is still busy. */ static ESP_AT_StatusType StartStep(void) { char cmd[64]; switch (step) { case APP_BEGIN: return ESP_AT_Begin(&esp); case APP_JOIN: return ESP_AT_WiFiJoin(&esp, WIFI_SSID, WIFI_PASSWORD); case APP_SNTP: snprintf(cmd, sizeof(cmd), "AT+CIPSNTPCFG=1,%d,\"%s\"", TIME_ZONE, NTP_SERVER); return ESP_AT_Command(&esp, cmd, 2000); case APP_PING_GW: return ESP_AT_Ping(&esp, esp.gateway[0] ? esp.gateway : "192.168.1.1"); case APP_PING_NET: return ESP_AT_Ping(&esp, PING_HOST); case APP_TIME: return ESP_AT_Command(&esp, "AT+CIPSNTPTIME?", 2000); default: return ESP_AT_OK; } } /* The job of the current step ended with status s: print the result and pick the next step */ static void StepDone(ESP_AT_StatusType s) { char t[48]; switch (step) { case APP_BEGIN: Log("[app] module: %s (%s)\r\n", esp.version, ESP_AT_FirmwareText(esp.firmware)); step = (s == ESP_AT_OK) ? APP_JOIN : APP_BEGIN; break; case APP_JOIN: if (s == ESP_AT_OK) Log("[app] joined: IP %s, gateway %s\r\n", esp.ip, esp.gateway); else Log("[app] join failed (+CWJAP error %d), trying again\r\n", esp.joinError); step = (s == ESP_AT_OK) ? APP_SNTP : APP_JOIN; break; case APP_SNTP: Log("[app] SNTP set to %s, UTC%+d: %s\r\n", NTP_SERVER, TIME_ZONE, ESP_AT_StatusText(s)); step = APP_PING_GW; break; case APP_PING_GW: case APP_PING_NET: if (s == ESP_AT_OK) Log("[app] ping %s: %lu ms\r\n", (step == APP_PING_GW) ? esp.gateway : PING_HOST, (unsigned long)esp.pingMs); else Log("[app] ping %s: %s\r\n", (step == APP_PING_GW) ? esp.gateway : PING_HOST, ESP_AT_StatusText(s)); step = (step == APP_PING_GW) ? APP_PING_NET : APP_TIME; break; case APP_TIME: /* Reply: +CIPSNTPTIME:Thu Nov 26 07:41:13 2026. Year 1970 = not synchronised yet */ if (s == ESP_AT_OK && ESP_AT_FindValue(&esp, "+CIPSNTPTIME:", t, sizeof(t))) { if (strstr(t, "1970") != NULL) Log("[app] time not synchronised yet (%s)\r\n", t); else Log("[app] local time: %s\r\n", t); } else Log("[app] time read: %s\r\n", ESP_AT_StatusText(s)); /* Read again 1 s later until the first real time arrives, then every 10 s */ nextTime = HAL_GetTick() + ((s == ESP_AT_OK && strstr(t, "1970") == NULL) ? PRINT_EVERY_MS : 1000U); step = APP_IDLE; break; default: break; } } static void OnEvent(ESP_AT_HandleType *h, ESP_AT_EventType ev, const ESP_AT_EventDataType *d) { (void)h; if (ev == ESP_AT_EVT_JOB_DONE && started && d->job == StepJob()) { result = d->status; finished = 1; } else if (ev == ESP_AT_EVT_WIFI_DISCONNECTED) Log("[event] WiFi disconnected, auto reconnect is on\r\n"); else if (ev == ESP_AT_EVT_WIFI_GOT_IP) Log("[event] WiFi got an IP\r\n"); else if (ev == ESP_AT_EVT_URC) Log("[event] module says: %s\r\n", d->line); /* e.g. +TIME_UPDATED */ } /* USER CODE END 0 */ /* Inside main(), after the MX_..._Init() calls */ /* USER CODE BEGIN 2 */ ESP_AT_ConfigType cfg = {0}; cfg.huart = &huart1; cfg.autoReconnect = 1; cfg.onEvent = OnEvent; cfg.onTrace = OnTrace; Log("\r\n=== ESP_AT Demo 4: SNTP time and ping ===\r\n"); if (ESP_AT_Init(&esp, &cfg) != ESP_AT_OK) Error_Handler(); /* USER CODE END 2 */ /* Inside while (1) */ /* USER CODE BEGIN 3 */ ESP_AT_Process(&esp); if (step == APP_IDLE) { if ((int32_t)(HAL_GetTick() - nextTime) >= 0 && ESP_AT_IsWiFiConnected(&esp)) step = APP_TIME; } else if (!started) { finished = 0; started = 1; ESP_AT_StatusType r = StartStep(); if (r == ESP_AT_BUSY) started = 0; /* the driver is busy: try on the next pass */ else if (r != ESP_AT_OK) { started = 0; StepDone(r); } } else if (finished) { started = 0; finished = 0; StepDone(result); } } /* USER CODE END 3 */ |
STM32 ESP32-C3 Example 4 Code Explanation
AT+CIPSNTPCFG=1,2,"pool.ntp.org" turns SNTP on with a time zone of UTC+2 (set TIME_ZONE to yours) and the pool.ntp.org servers. The module prints +TIME_UPDATED by itself when it has the time, and AT+CIPSNTPTIME? then returns the local date and time as text. ESP_AT_Ping() sends AT+PING and the driver reads the round trip from the +PING: reply.
STM32 ESP32-C3 Example 4 Testing
This is the real hardware testing result for this example project:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
=== ESP_AT Demo 4: SNTP time and ping === >> AT << OK -- module answers at 115200 baud >> AT << OK >> ATE0 << OK >> AT+GMR << AT version:4.1.1.0(ba4dd0e - ESP32C3 - Jul 31 2025 08:38:07) << SDK version:v5.4.1-643-g8ad0d3d8f2-dirty << compile time(7c092f9a):Aug 1 2025 05:35:19 << Bin version:v4.1.1.0(MINI-1) << OK >> AT+CIPMUX? << +CIPMUX:0 << OK >> AT+CIPCLOSE << CLOSED << OK >> AT+CIPMUX=0 << OK [app] module: AT version:4.1.1.0(ba4dd0e - ESP32C3 - Jul 31 2025 08:38:07) (ESP-AT 2.x+) >> AT+CWMODE=1 << OK >> AT+CWJAP="MyWiFi","********" << WIFI DISCONNECT << WIFI CONNECTED << WIFI GOT IP [event] WiFi got an IP << OK >> AT+CIPSTA? << +CIPSTA:ip:"192.168.1.106" << +CIPSTA:gateway:"192.168.1.1" << +CIPSTA:netmask:"255.255.255.0" << OK [app] joined: IP 192.168.1.106, gateway 192.168.1.1 >> AT+CIPSNTPCFG=1,2,"pool.ntp.org" << OK [app] SNTP set to pool.ntp.org, UTC+2: OK >> AT+PING="192.168.1.1" << +TIME_UPDATED [event] module says: +TIME_UPDATED << +PING:4 << OK [app] ping 192.168.1.1: 4 ms >> AT+PING="www.google.com" << +PING:47 << OK [app] ping www.google.com: 47 ms >> AT+CIPSNTPTIME? << +CIPSNTPTIME:Thu Nov 26 10:14:02 2026 << OK [app] local time: Thu Nov 26 10:14:02 2026 >> AT+CIPSNTPTIME? << +CIPSNTPTIME:Thu Nov 26 10:14:12 2026 << OK [app] local time: Thu Nov 26 10:14:12 2026 >> AT+CIPSNTPTIME? << +CIPSNTPTIME:Thu Nov 26 10:14:22 2026 << OK [app] local time: Thu Nov 26 10:14:22 2026 >> AT+CIPSNTPTIME? << +CIPSNTPTIME:Thu Nov 26 10:14:32 2026 << OK [app] local time: Thu Nov 26 10:14:32 2026 |
The router answered in 3ms and www.google.com in 45ms. Note that AT+PING itself took about 1 second to return each time, even though the reported round trip was a few milliseconds. ESP-AT waits out a fixed window before it answers, so don’t use AT+PING to time anything.
Measured: ESP32-C3 vs ESP8266 With STM32
We ran the same driver and the same tests on the ESP32-C3 (ESP-AT v4.1.1.0) and on the ESP8266 from the previous tutorial (NonOS AT v1.7.6), both driven by the same Blue Pill and joining the same router:
| Test | ESP32-C3 (ESP-AT v4.1.1.0) | ESP8266 (NonOS AT v1.7.6) |
|---|---|---|
| WiFi join time | 1.2 to 1.9s | 5.0 to 6.1s |
| IP back after a WiFi drop | 3.1s | 5.8s |
| TCP echo round trip (STM32 to PC and back) | 39 to 49ms | 32 to 89ms |
| TCP upload at 115200 baud | about 10KB/s | 7.2KB/s |
| TCP upload at 921600 baud | 35 to 37KB/s | 31.8KB/s |
| TCP upload at 2,000,000 baud | 56.2KB/s | Not tested |
| Reply delay after a short command (scope) | 1.95ms | 0.38ms |
AT+RST to ready | 1.2s | 0.26s |
| Free RAM inside the module | about 134KB | about 51KB |
| Boot log on the AT port | No (separate UART0) | Yes, at 74880 baud |
| Built-in HTTP client, MQTT, SNTP event, BLE | Yes | SNTP only |
The ESP32-C3 joins the network 3 to 4 times faster and moves data faster at every baud rate. At 2,000,000 baud, the 64KB upload ran at 56.2KB/s, and all 71,719 bytes echoed back by the PC arrived with no byte lost. The ESP8266 answers short commands faster (0.38ms vs 1.95ms), and it restarts faster, but for a WiFi application the join time and the throughput matter much more.
ESP32 AT Commands on The Oscilloscope
Here’s what an AT command and its reply look like on my oscilloscope captures. CH1 (yellow) is PA9, the STM32’s TX line to the ESP32-C3’s GPIO6, and CH3 (blue) is PA10, the ESP32-C3’s GPIO7 back to the STM32.
The first command of Example 2 is AT plus CR LF: 4 bytes in 0.34ms at 115200 baud. The cursors show that the ESP32-C3’s OK starts 2.29ms after the command starts, so the module answered 1.95ms after it received the last byte. That’s 5 times slower than the ESP8266, but still far below any timeout:
The WiFi join: the cursors run from the AT+CWJAP command (X1) to the module’s WIFI GOT IP line (X2), 1.93 seconds in this run. WIFI CONNECTED came 0.42 seconds after the start. Unlike the ESP8266 capture, there are no supply dips on the lines, because the ESP32-C3 board has its own regulator fed from USB:
ESP32 AT Commands List (With Real Replies)
These are the ESP32 AT commands (ESP-AT) you’ll use most with an STM32, grouped by function, with the real replies from our ESP32-C3 running ESP-AT v4.1.1.0. Every command ends with \r\n. The full reference is Espressif’s ESP-AT command set.
Note: The NonOS-only _CUR and _DEF forms of the ESP8266 ( AT+CWJAP_CUR, AT+CIPSTA_CUR?…) return ERROR on ESP-AT. Use the plain forms, and AT+SYSSTORE to choose whether settings are saved.
ESP32 AT Commands List: Basic, WiFi, and TCP/IP (click to expand)
Basic and System AT Commands
| Command | What It Does | Reply From Our ESP32-C3 | Time (ms) |
|---|---|---|---|
AT | Tests that the module is alive | OK | 4 |
ATE0 / ATE1 | Turns the command echo off / on | OK | 3 |
AT+GMR | AT, SDK, and bin versions | AT version:4.1.1.0(ba4dd0e - ESP32C3 - Jul 31 2025 08:38:07)SDK version:v5.4.1-643-g8ad0d3d8f2-dirtycompile time(7c092f9a):Aug 1 2025 05:35:19Bin version:v4.1.1.0(MINI-1)OK | 26 |
AT+RST | Restarts the module | OK, then the boot log on UART0, then ready | 1225 to ready |
AT+UART_CUR? | Current UART settings: baud, data bits, stop bits, parity, flow control | +UART_CUR:115211,8,1,0,1 | 10 |
AT+UART_DEF? | UART settings saved in flash | +UART_DEF:115200,8,1,0,1 | 11 |
AT+SYSSTORE? | Whether settings are saved in flash (1 = yes) | +SYSSTORE:1 | 7 |
AT+SYSRAM? | Free RAM and the minimum ever free, in bytes | +SYSRAM:134144,128140 | 8 |
AT+SYSFLASH? | The user flash partitions | +SYSFLASH:"mfg_nvs",1,2,0x1f000,0x1f000+SYSFLASH:"fatfs",1,129,0x47000,0x19000 | 16 |
AT+SLEEP? | Sleep mode (0 off, 1 modem sleep DTIM, 2 light, 3 modem sleep interval) | +SLEEP:1 | 7 |
AT+RFPOWER=82 | Maximum TX power | OK | 7 |
AT+BLEINIT? | Bluetooth LE role (0 = off) | +BLEINIT:0 | 7 |
WiFi AT Commands
| Command | What It Does | Reply From Our ESP32-C3 | Time (ms) |
|---|---|---|---|
AT+CWMODE? | WiFi mode: 1 station, 2 SoftAP, 3 both | +CWMODE:1 | 7 |
AT+CWJAP="ssid","pwd" | Joins a network (saved in flash when AT+SYSSTORE=1) | WIFI CONNECTEDWIFI GOT IPOK | 1182 |
AT+CWJAP? | The current network: SSID, BSSID, channel, RSSI, and more | +CWJAP:"MyWiFi","xx:xx:xx:xx:xx:xx",9,-66,0,1,3,0,1 | 16 |
AT+CWSTATE? | WiFi state (2 = connected with an IPv4 address) and SSID | +CWSTATE:2,"MyWiFi" | 10 |
AT+CWLAP | Scans for WiFi networks | +CWLAP:(3,"MyWiFi",-55,"xx:xx:xx:xx:xx:xx",9,-1,-1,4,4,7,1)... one line per networkOK | 2117 |
AT+CWQAP | Disconnects from the network | WIFI DISCONNECTOK | 17 |
AT+CWAUTOCONN? | Joins the saved network after power-up (1 = on) | +CWAUTOCONN:1 | 7 |
AT+CWDHCP? | DHCP state (bit 0 station, bit 1 SoftAP) | +CWDHCP:3 | 6 |
AT+CWHOSTNAME? | The module’s host name on your network | +CWHOSTNAME:espressif | 9 |
AT+CIPSTAMAC? | Station MAC address | +CIPSTAMAC:"60:55:f9:af:ae:90" | 10 |
AT+CIPSTA? | IP address, gateway, and netmask | +CIPSTA:ip:"192.168.1.201"+CIPSTA:gateway:"192.168.1.1"+CIPSTA:netmask:"255.255.255.0" | 16 |
AT+CIFSR | IP and MAC address | +CIFSR:STAIP,"192.168.1.201"+CIFSR:STAMAC,"60:55:f9:af:ae:90" | 13 |
TCP/IP AT Commands
| Command | What It Does | Reply From Our ESP32-C3 | Time (ms) |
|---|---|---|---|
AT+CIPSTATUS | Connection status (2 = got IP) | STATUS:2 | 6 |
AT+CIPMUX? | Single (0) or multiple (1) connections | +CIPMUX:0 | 6 |
AT+CIPMODE? | 0 normal mode, 1 transparent (passthrough) mode | +CIPMODE:0 | 6 |
AT+CIPRECVTYPE? | Receive mode per link (0 = active, data pushed as +IPD) | +CIPRECVTYPE:0,0 | 9 |
AT+CIPSTO? | TCP server timeout, in seconds | +CIPSTO:0 | 6 |
AT+CIPDOMAIN="www.google.com" | DNS lookup | +CIPDOMAIN:"142.251.156.119" | 264 |
AT+PING="192.168.1.1" | Pings a host, the reply is the time in ms | +PING:4 | 1010 |
AT+CIPSNTPCFG=1,0,"pool.ntp.org" | Enables SNTP time (time zone 0 = UTC) | OK, then +TIME_UPDATED | 13 |
AT+CIPSNTPTIME? | Date and time from the SNTP server | +CIPSNTPTIME:Thu Nov 26 07:56:45 2026 | 13 |
AT+CIPSTART="TCP","192.168.1.101",5000 | Opens a TCP connection (“UDP” and “SSL” also work) | CONNECTOK | 29 |
AT+CIPSEND=5 | Sends 5 bytes: wait for the > prompt, then send the data | OK>Recv 5 bytesSEND OK | 19 to SEND OK |
| (received data) | Data from the server, sent by the module at any time | +IPD,28:Hello STM32, this is the PC | – |
AT+CIPCLOSE | Closes the connection | CLOSEDOK | 15 |
AT+HTTPCLIENT=2,0,"http://httpbin.org/get",,,1 | HTTP GET (built-in HTTP client) | +HTTPCLIENT:246,{ "args": {}, ... }OK | 551 |
STM32 ESP32-C3 Troubleshooting
The ESP32-C3 doesn’t answer AT commands
Check that the AT firmware is flashed (a new board usually runs a demo program), that you are on the AT port (GPIO6 and GPIO7, not the USB port or GPIO20/GPIO21), that TX and RX are crossed, and that the grounds are connected. Test the module alone with a USB-TTL converter as shown above.
Replies look shifted by one, or AT+CIPMUX returns ERROR after an STM32 reset
The ESP32-C3 kept running while the STM32 restarted, so an old reply or an open TCP link is still pending. Send AT until you get a clean OK, then AT+CIPCLOSE before AT+CIPMUX. Our driver’s Begin does this.
AT+CWJAP_CUR or AT+CIPSTA_CUR? returns ERROR
Those are ESP8266 NonOS commands. On ESP-AT, use AT+CWJAP and AT+CIPSTA?.
AT+CWJAP returns FAIL or ERROR
The reply +CWJAP:<code> tells you why: 1 = timeout, 2 = wrong password, 3 = network not found, 4 = connection failed. The ESP32-C3 is 2.4GHz only. Commas, double quotes, and backslashes in the SSID or password must be escaped with a backslash (our driver does it).
The module joins WiFi by itself after a reset
ESP-AT saves the network when you join ( AT+SYSSTORE=1 by default) and reconnects after power-up. Send AT+CWAUTOCONN=0 or AT+SYSSTORE=0 if you don’t want that.
The reply is busy p…
It’s short for (busy processing…): the module is still working on the previous command. Wait for its OK or ERROR before sending the next command. Our driver resends the command after 200ms.
STM32 ESP32-C3 FAQ
Which ESP32 boards can run the ESP-AT firmware?
Boards with an ESP32, ESP32-C2, ESP32-C3, ESP32-C5, ESP32-C6, ESP32-C61, or ESP32-S2. For the ESP32-C3, the ESP32-C3-MINI-1 binary works on most boards with 4MB of flash, like the WeAct ESP32-C3 Core Board and the ESP32-C3 SuperMini. The ESP32-S3 isn’t on Espressif’s ESP-AT list.
Can I use an ESP32 DevKit instead of an ESP32-C3?
Yes. Flash the ESP32 (WROOM-32) AT binary; its AT port is UART1 on GPIO16 (RX) and GPIO17 (TX). The STM32 code doesn’t change.
Do I need a level shifter between the STM32 and the ESP32-C3?
No. Both run at 3.3V, so their UART pins connect directly.
How fast can the UART between the STM32 and the ESP32-C3 run?
We ran it at 2,000,000 baud with AT+UART_CUR and got 56.2KB/s of TCP upload with no byte lost. Start at 115200 and switch up once the link works.
Can the ESP32-C3 do HTTPS and MQTT for the STM32?
Yes. ESP-AT has SSL connections, an HTTP client ( AT+HTTPCLIENT), and an MQTT client built in, so TLS runs on the ESP32-C3 and not on the STM32. We use the HTTP client in the STM32 HTTP client over WiFi tutorial, and MQTT in a later part of this series.
ESP32-C3 or ESP8266 for a new STM32 design?
The ESP32-C3: it costs about the same, joins WiFi 3 to 4 times faster in our tests, has a separate AT port with no boot garbage, and gets the current ESP-AT releases.
STM32 WiFi Tutorial Series
This tutorial is the third part of our STM32 WiFi tutorial series. Each part builds complete, tested projects with an STM32 Blue Pill and an ESP WiFi module, using our ESP_AT driver:
| Part | Tutorial | What You’ll Build |
|---|---|---|
| 1 | STM32 WiFi | Choosing a WiFi module, quick start, ESP32-C3 vs ESP8266 measured |
| 2 | ESP8266 AT Commands With STM32 | ESP8266 AT firmware, commands, and our ESP_AT driver |
| 3 | ESP32 AT Commands (ESP-AT) With STM32 (this tutorial) | ESP-AT firmware, commands, SNTP time, and our ESP_AT driver |
| 4 | STM32 HTTP Client Over WiFi | GET, POST, HTTPS, and JSON parsing |
| 5 | STM32 Web Server | Control GPIOs and read sensors from a browser |
| 6 | STM32 MQTT | Publish and subscribe, Home Assistant |
| 7 | STM32 OTA Firmware Update | Update the STM32’s firmware over WiFi |
| 8 | STM32 WebSocket | Real-time data with an ESP32 smart bridge |
Download STM32 ESP32-C3 (ESP-AT) Example Projects + ESP_AT Driver
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 used the ESP32 AT commands (ESP-AT) with an STM32 Blue Pill and an ESP32-C3 to connect the STM32 to WiFi. We flashed the ESP-AT firmware, configured the STM32 UART with circular DMA, and built four example projects with our ESP_AT driver: AT commands, joining WiFi with automatic reconnection, a TCP client, and network time with ping. The measurements showed that the ESP32-C3 joins WiFi 3 to 4 times faster than the ESP8266 and reaches 56.2KB/s at 2,000,000 baud.
In the next part of this series, we’ll use the ESP32-C3’s built-in HTTP client to send GET and POST requests and parse JSON from the STM32. If you’re new to the STM32 UART, the STM32 UART tutorial and the STM32 UART IDLE line detection tutorial are a good place to start.