Welcome to ESP32 Core Board repository
3. WIFI AP Mode
WIFI AP Mode
WIFI AP Mode1. WiFi AP Overview2. Core Concepts2.1 AP Mode Network Topology2.2 AP Configuration Parameters2.3 WPA2 Security Authentication3. Project Architecture and FlowEvent Handling Flow4. Hardware Dependencies5. Source Code Analysis5.1 Header Files and Macro Configuration5.2 wifi_event_handler() — Event Callback5.3 wifi_init_ap() — AP Initialization5.4 app_main() — Main Entry6. Operation Flow6.1 Compilation and Flashing Flow6.2 Normal Startup6.3 Device Connection and Disconnection7. Common Problems
1. WiFi AP Overview
WiFi AP (Access Point, Hotspot Mode) allows ESP32-S3 to become its own wireless router/hotspot, other devices (phones, computers, another ESP32) can search for and connect to this hotspot, enabling LAN communication.
AP mode has irreplaceable application value in IoT. First, it is the foundation of provisioning technology - when device is first deployed without available WiFi network, device runs in AP mode first, user's phone connects to hotspot then sends router password through webpage or App, device then switches to STA mode to connect (first step of SmartConfig/Web provisioning flow). Second, in scenarios without router (field operations, site inspection, temporary exhibitions), AP mode allows users to directly connect point-to-point to device for parameter configuration, status viewing and firmware upgrade, without relying on any network infrastructure. Additionally, AP mode is widely used in LAN multi-person collaboration scenarios - multiple phones or computers connect to the same ESP32-S3 hotspot for local chat, file sharing, multi-player games and other LAN applications.
| Comparison | AP Mode | STA Mode |
|---|---|---|
| Role | Hotspot (others connect to it) | Client (connect to others) |
| IP Address | Fixed 192.168.4.1 | DHCP dynamic assignment |
| DHCP Service | ESP32 provides (assigns IP to connectors) | Router provides |
| Max Connections | Configurable (default 4) | N/A |
| Typical Scenario | Provisioning, direct control, offline LAN | IoT internet access, data reporting |
This project implements WiFi AP mode, creating a WPA2 encrypted hotspot named
ESP32_APwith password12345678, other devices can access ESP32 via192.168.4.1after connecting.
2. Core Concepts
2.1 AP Mode Network Topology
┌──────────────────┐│ ESP32-S3 (AP) ││ 192.168.4.1 ││ SSID: ESP32_AP │└────────┬─────────┘│ WiFi┌───────────┼───────────┐│ │ │┌────┴────┐ ┌────┴────┐ ┌────┴────┐│ Phone │ │ PC │ │ Another ││ Client1 │ │ Client2 │ │ ESP32 ││ .2 │ │ .3 │ │ .4 │└─────────┘ └─────────┘ └─────────┘
2.2 AP Configuration Parameters
xxxxxxxxxxtypedef struct { char ssid[32]; // Hotspot name (max 32 bytes) char password[64]; // Hotspot password (WPA2 requires ≥ 8 chars) uint8_t ssid_len; // Actual SSID length (0=auto, default) uint8_t channel; // WiFi channel (1~13, select idle channel to avoid interference) uint8_t authmode; // Encryption mode (Open/WPA/WPA2/WPA3) // Max simultaneous connections (default 4, max 10) uint8_t max_connection;} wifi_ap_config_t;2.3 WPA2 Security Authentication
| Auth Mode | Description |
|---|---|
WIFI_AUTH_OPEN | No password (not recommended) |
WIFI_AUTH_WEP | Deprecated, insecure |
WIFI_AUTH_WPA2_PSK | Recommended — Personal pre-shared key mode |
WIFI_AUTH_WPA3_PSK | More secure, but some older devices incompatible |
WIFI_AUTH_WPA2_Enterprise | Enterprise-grade |
3. Project Architecture and Flow
xxxxxxxxxxapp_main()├─ nvs_flash_init() ← Initialize NVS flash│└─ wifi_init_ap() ← WiFi AP initialization + start├─ esp_netif_init() → Initialize TCP/IP network stack├─ esp_event_loop_create_default() → Create default event loop├─ esp_netif_create_default_wifi_ap() → Create AP network interface├─ esp_wifi_init() → Initialize WiFi driver├─ Register event callbacks (WIFI_EVENT / ESP_EVENT_ANY_ID)├─ Configure AP parameters (SSID/PASS/CHANNEL/MAX_CONN/AUTH)├─ esp_wifi_set_mode(WIFI_MODE_AP) → Set AP mode├─ esp_wifi_set_config() → Write config├─ esp_wifi_start() → Start hotspot└─ Print SSID + password + IP info
Event Handling Flow
xWIFI_EVENT_AP_START→ ESP_LOGI "WiFi AP started" // Hotspot createdWIFI_EVENT_AP_STACONNECTED→ ESP_LOGI "Station connected" // Device connectedWIFI_EVENT_AP_STADISCONNECTED→ ESP_LOGI "Station disconnected" // Device disconnected
4. Hardware Dependencies
Hardware Connection: No external wiring, using on-board WiFi antenna
Dependent Components:
esp_wifi,esp_netif,esp_event,nvs_flash,FreeRTOS,esp_log
5. Source Code Analysis
Complete source code: Source Code → ESP-IDF → 5.Network_Protocol → 2.WiFi_AP
5.1 Header Files and Macro Configuration
Introduce string header for calculating SSID length - AP mode must explicitly tell protocol stack the byte count of hotspot name. Remaining headers are standard combination of WiFi driver, event system, NVS flash. Macros centralize four core parameters: hotspot name, password, max connections and channel at file top for easy modification.
xxxxxxxxxx
// ==================== AP Hotspot Config ====================// Hotspot name// Hotspot password (WPA2 ≥ 8 chars)// Max connections// WiFi channel5.2 wifi_event_handler() — Event Callback
AP mode event handling is much simpler than STA mode - callback only needs to manage three cases: hotspot created, device connected, device disconnected. AP doesn't need to get dynamic IP from router like STA - AP's own IP is fixed at 192.168.4.1, therefore no need to register IP event callback. Each event just logs it, upper layer HTTP/TCP service depends on these events to determine device connection status.
xxxxxxxxxxstatic void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){ // Hotspot created successfully if (event_id == WIFI_EVENT_AP_START) { ESP_LOGI(TAG, "WiFi AP started"); } // Device disconnected else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) { ESP_LOGI(TAG, "Station disconnected"); } // Device connected else if (event_id == WIFI_EVENT_AP_STACONNECTED) { ESP_LOGI(TAG, "Station connected"); }}5.3 wifi_init_ap() — AP Initialization
WiFi AP initialization flow is the same as STA - network stack initialization, event loop creation, network interface creation, WiFi driver initialization, fixed order. But there are three key differences:
Network interface uses AP version creation function, not STA version, this determines ESP32 runs as hotspot not client.
Event callback only registers WiFi events, no IP events - AP's IP is fixed at 192.168.4.1, doesn't depend on DHCP assignment.
Configuration struct fills AP members. SSID length field must explicitly tell protocol stack the byte count of the string, different from STA mode where system calculates automatically.
Note that there's no need to call connection function - hotspot takes effect automatically after starting, just wait for other devices to connect.
xxxxxxxxxxvoid wifi_init_ap(void){ esp_netif_init(); esp_event_loop_create_default(); esp_netif_create_default_wifi_ap();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); esp_wifi_init(&cfg);
// Register WiFi event callback esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL);
wifi_config_t wifi_config = { .ap = { .ssid = AP_SSID, // Hotspot name .password = AP_PASS, // Password .ssid_len = strlen(AP_SSID), .channel = AP_CHANNEL, // Channel // Max connections .max_connection = AP_MAX_CONN, .authmode = WIFI_AUTH_WPA2_PSK } };
// Set hotspot mode esp_wifi_set_mode(WIFI_MODE_AP); esp_wifi_set_config(WIFI_IF_AP, &wifi_config); // Apply config esp_wifi_start(); // Start hotspot // Log SSID and password ESP_LOGI(TAG, "SSID: %s, PASS: %s", AP_SSID, AP_PASS); ESP_LOGI(TAG, "ESP32 IP: 192.168.4.1"); // AP fixed IP}5.4 app_main() — Main Entry
Main entry function does two things - first initialize NVS flash (required prerequisite for WiFi driver), then call initialization function to start hotspot. Final infinite loop ensures program doesn't exit, actual hotspot management and device connection are all handled asynchronously in event callback.
xxxxxxxxxxvoid app_main(void){ nvs_flash_init(); // Initialize NVS flash wifi_init_ap(); // Start AP mode
while (1) { // Keep program running vTaskDelay(pdMS_TO_TICKS(1000)); }}6. Operation Flow
6.1 Compilation and Flashing Flow
ESP-IDF flashing flow: See ESP-IDF → 1. Before Development → 3. Build and Flash.
6.2 Normal Startup
After flashing, reset development board, serial assistant will output the following - SSID/PASS confirms hotspot started, DHCP server started means DHCP server ready to assign IPs to clients, WiFi AP started is user log from event callback, last line emphasizes AP fixed IP:
Log field meanings:
| Field | Example | Meaning |
|---|---|---|
I | I | INFO level log |
WIFI_AP | WIFI_AP | Log tag |
esp_netif_lwip | esp_netif_lwip | Network interface DHCP service log |
SSID: | ESP32_AP | Hotspot name |
PASS: | 12345678 | Hotspot password |
DHCP server started | DHCP server started | DHCP server started, begins assigning IP to connected devices |
WiFi AP started | WiFi AP started | WiFi AP hotspot successfully created |
ESP32 IP: | 192.168.4.1 | AP fixed IP address |
xxxxxxxxxxI (551) WIFI_AP: SSID: ESP32_AP, PASS: 12345678I (551) esp_netif_lwip: DHCP server started on interface WIFI_AP_DEF with IP: 192.168.4.1I (571) WIFI_AP: WiFi AP startedI (571) WIFI_AP: ESP32 IP: 192.168.4.1

6.3 Device Connection and Disconnection
Search WiFi with phone, find ESP32_AP, enter password 12345678 to connect. Connection process outputs in order - Station connected is association success log from user code callback, then wifi:new:<1,1> indicates driver internal state changed from "no associated devices" to "1 associated device", two wifi:<ba-add> are link layer Block Ack negotiation complete, finally DHCP server assigned IP means DHCP has assigned IP to client, at which point device can communicate normally:
Log field meanings:
| Field | Example | Meaning |
|---|---|---|
Station connected | Station connected | Device associated to hotspot (user code event callback output) |
wifi:new:<1,1> | new:<1,1> | WiFi driver state transition: associated devices count from 0 to 1 (IDF underlying log) |
wifi:<ba-add> | ba-add | WiFi driver Block Ack negotiation successful, link layer ready (IDF underlying log) |
DHCP server assigned IP | 192.168.4.2 | DHCP server assigned IP address to client, device can communicate normally |
xxxxxxxxxxI (11061) WIFI_AP: Station connectedI (11091) wifi:new:<1,1>, old:<1,0>, ap:<1,1>, sta:<255,255>, prof:1I (11111) wifi:<ba-add>idx:2 (ifx:1, e6:d1:24:85:df:49), tid:0, ssn:0, winSize:64I (11121) wifi:<ba-add>idx:3 (ifx:1, e6:d1:24:85:df:49), tid:7, ssn:0, winSize:64I (11151) esp_netif_lwip: DHCP server assigned IP to a client, IP is: 192.168.4.2

After disconnection, log shows complete link teardown process - three wifi:<ba-del> are driver deleting Block Ack sessions one by one, releasing link layer resources, wifi:new:<1,0> indicates driver internal state from "1 associated device" to "0 devices", finally Station disconnected is user code event callback confirming disconnect:
| Field | Example | Meaning |
|---|---|---|
wifi:<ba-del> | ba-del | WiFi driver deleted Block Ack session, link layer released resources (IDF underlying log) |
wifi:new:<1,0> | new:<1,0> | WiFi driver state transition: associated devices from 1 to 0 (IDF underlying log) |
Station disconnected | Station disconnected | Client disconnected from hotspot (user code event callback output) |
xxxxxxxxxxI (256871) wifi:<ba-del>idx:2, tid:0I (256871) wifi:<ba-del>idx:4, tid:5I (256871) wifi:new:<1,0>, old:<1,1>, ap:<1,1>, sta:<255,255>, prof:1, snd_ch_cfg:0x0I (256881) wifi:<ba-del>idx:3, tid:7I (256891) WIFI_AP: Station disconnected

7. Common Problems
| Symptom | Cause | Solution |
|---|---|---|
| Phone can't find hotspot | WiFi not started normally / channel occupied | Check serial logs confirm WiFi AP started; try changing AP_CHANNEL (e.g., 6, 11) |
| "Password incorrect" when connecting | Password less than 8 chars or SSID abnormal | WPA2 requires password ≥ 8 chars; confirm AP_PASS length is sufficient |
Can't access 192.168.4.1 after connecting | No HTTP/TCP server running | AP is just network layer, need upper layer service listening on port to respond |
| New device can't connect after multiple devices connected | AP_MAX_CONN too small | Increase AP_MAX_CONN (max 10), but note memory consumption |
| Hotspot unstable, frequent disconnections | 2.4G channel interference | Use WiFi channel scan tool to select idle channel, modify AP_CHANNEL |
WIFI_EVENT_AP_START no log | NVS not initialized | Ensure nvs_flash_init() called at beginning of app_main() |