Home / Learn Chip Programming / ESP32 Wi-Fi: Station, AP and Provisioning
ESP32 Wi-Fi: Station, AP and Provisioning
Connect to your router, create your own network, and let users enter credentials safely.
Theory in plain words
What it is
In station mode the ESP32 joins a router. In SoftAP mode it creates its own network. Provisioning (BLE, SoftAP, WPS) lets users enter Wi-Fi details without hard-coding.
Why we use it
Hard-coded passwords make products unusable. Understanding events, reconnect and IP lifecycle prevents most "device is offline" bugs.
How it works
Initialise NVS, netif and event loop, register WIFI_EVENT and IP_EVENT handlers, call esp_wifi_start, on disconnect retry with backoff, on GOT_IP start services.
Where it is used
- Smart plug, sensor node
- Captive portal for setup
- Mesh and repeater
Connection state machine
Reconnect with growing delays so a router reboot does not cause a retry storm.
static void on_event(void *arg, esp_event_base_t base, int32_t id, void *data) {
if (base == WIFI_EVENT && id == WIFI_EVENT_STA_START) esp_wifi_connect();
else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) { vTaskDelay(pdMS_TO_TICKS(backoff_ms())); esp_wifi_connect(); }
else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t *e = data; ESP_LOGI("wifi", "IP " IPSTR, IP2STR(&e->ip_info.ip)); }
}
esp_netif_init(); esp_event_loop_create_default(); esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); esp_wifi_init(&cfg);
wifi_config_t wc = { .sta = { .ssid = "MyWiFi", .password = "pass1234" } };
esp_wifi_set_mode(WIFI_MODE_STA); esp_wifi_set_config(WIFI_IF_STA, &wc); esp_wifi_start();Quick quiz
1. Which event tells you an IP address was received?
Only then is the network usable.
2. Why backoff on reconnect?
Gradual retries are polite and robust.
Common questions
Which event tells you an IP address was received?
IP_EVENT_STA_GOT_IP. Only then is the network usable.
Why backoff on reconnect?
Avoid hammering the router. Gradual retries are polite and robust.