Joining Wi-Fi from an ESP32
About this lesson
Put an ESP32-DevKitC-32E on a home network with six lines of Arduino code, and see what really happens behind WiFi.begin(). The call only starts the join: in the background the Wi-Fi driver scans for the network, authenticates and associates, proves the password with the WPA2 4-way handshake, and leases an IP address from DHCP. Only then does WiFi.status() report WL_CONNECTED. Then keep the device online when the router reboots, and size its power supply for the radio's 240 mA transmit peaks. The Networking lessons “How Wi-Fi works”, “DHCP: getting an address” and “Defence: WPA3 and enterprise Wi-Fi (802.1X)” explain the protocols; here you use them from the chip.
Station and access point
Wi-Fi has two roles. An access point (AP), like a home router, runs a network; a station joins one. Line 1 makes our ESP32 a station. Its radio is inside the chip, wired to a small antenna on the module.
line 1 WiFi.mode(WIFI_STA); AP runs the network: the home router station joins a network: our ESP32 (STA) board ESP32-DevKitC-32E, module ESP32-WROOM-32E radio 802.11 b/g/n, 2.4 GHz, PCB antenna
Read the lesson step by step
Station and access point
Wi-Fi has two roles. An access point (AP), like a home router, runs a network; a station joins one. Line 1 makes our ESP32 a station. Its radio is inside the chip, wired to a small antenna on the module.
line 1 WiFi.mode(WIFI_STA); AP runs the network: the home router station joins a network: our ESP32 (STA) board ESP32-DevKitC-32E, module ESP32-WROOM-32E radio 802.11 b/g/n, 2.4 GHz, PCB antennabegin() only starts the join
WiFi.begin hands the network name, HomeNet, and the password to the Wi-Fi driver, then returns at once. The driver does the joining in the background, in four stages: find, join, prove, get an address.
line 2 WiFi.begin(ssid, pass); ssid "HomeNet" pass the WPA2 password (8 to 63 characters) returns at once: it does not wait for the link driver find -> join -> prove -> address // ESP32-DevKitC-32E (ESP32-WROOM-32E), Arduino core for ESP32 #include <WiFi.h> const char* ssid = "HomeNet"; const char* pass = "your-password"; // 8 to 63 characters void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) delay(500); Serial.println(WiFi.localIP()); } void loop() { if (WiFi.status() == WL_CONNECTED) { // online: safe to open a connection and send } // else the core is reconnecting delay(1000); }Your code waits by asking
Lines 3 to 5 ask WiFi.status() every 500 ms. While the driver works the answer is not WL_CONNECTED, so delay(500) pauses and the loop asks again. Asking over and over like this is called polling.
lines 3-5 while (WiFi.status() != WL_CONNECTED) delay(500); status WL_CONNECTED = 3, WL_DISCONNECTED = 6, WL_IDLE_STATUS = 0 each 500 ms: not 3 yet -> wait, ask againFind: scan for HomeNet
The radio hops from channel to channel, 1 to 11 by default. On each it sends a probe request, “who is out there?”, and listens. On channel 6 the access point answers: HomeNet is here.
scan active: a probe request on each channel channels 1..11 (ESP-IDF default country "01") answer probe response from HomeNet, channel 6 the radio tunes to channel 6 and stays thereJoin: authenticate and associate
Two short exchanges follow. An authentication request and reply, then an association request and reply: the ESP32 asks to join and the access point says yes. The link exists now, but nothing is encrypted yet.
ESP32 -> AP authentication request AP -> ESP32 authentication response ESP32 -> AP association request AP -> ESP32 association response linked, not yet secure: no keys installedProve: the 4-way handshake
Both sides already turned password and SSID into the same 256-bit key. Four messages swap random numbers and check codes that prove it, without sending the key. Fresh encryption keys result, and WIFI_EVENT_STA_CONNECTED fires.
PMK = PBKDF2(HMAC-SHA1, password, salt = SSID, 4096 rounds) -> 256 bits M1 AP -> ESP32 ANonce, a random number from the AP M2 ESP32 -> AP SNonce from the ESP32, with a check code (MIC) M3 AP -> ESP32 the group key (GTK), with a MIC M4 ESP32 -> AP confirmation: keys installed PTK = from PMK, both nonces, both MAC addresses the password and the PMK never cross the air event WIFI_EVENT_STA_CONNECTEDAddress: ask DHCP
The link is up but the ESP32 has no IP address. Its DHCP client broadcasts Discover; the router’s DHCP server makes an Offer; Request; Ack. 192.168.1.42 is now lent to the ESP32 for a set time, a lease.
DHCPDISCOVER ESP32 -> all broadcast: is there a DHCP server? DHCPOFFER server -> ESP32 offer: 192.168.1.42 DHCPREQUEST ESP32 -> all request: 192.168.1.42, please DHCPACK server -> ESP32 ack: 192.168.1.42 is yours for the lease lease 192.168.1.42 for a set time, renewed while in use router 192.168.1.1Got IP: WL_CONNECTED
IP_EVENT_STA_GOT_IP fires and the Arduino core sets status() to WL_CONNECTED. The loop on line 3 ends and line 6 prints 192.168.1.42. Only now may the sketch open network connections.
event IP_EVENT_STA_GOT_IP status WL_CONNECTED (3) -> the while loop ends line 6 Serial.println(WiFi.localIP()); -> 192.168.1.42 link up (STA_CONNECTED) is not enough: wait for the IPThe link drops, and returns
The router reboots. WIFI_EVENT_STA_DISCONNECTED fires with a reason code and status() is no longer WL_CONNECTED. Auto-reconnect is on by default, so the core joins again. Check status() before sending, and never wait forever.
event WIFI_EVENT_STA_DISCONNECTED (reason code) status not WL_CONNECTED core auto-reconnect on by default: joins again loop() if (WiFi.status() == WL_CONNECTED) send... setup() add a timeout to the while loop in real codePower: the radio is hungry
Sending, the radio draws about 240 mA; listening, 95-100 mA. Espressif recommends a 3.3 V supply giving 500 mA or more. To save energy the radio sleeps between the AP’s DTIM beacons (often every beacon): modem sleep, on by default.
send 240 mA (802.11b, 1 Mbps, +19.5 dBm) listen 95-100 mA modem sleep 20-68 mA for the whole chip (CPU on, radio off) supply 3.3 V, 500 mA or more recommended default WIFI_PS_MIN_MODEM: wake at every DTIM beacon
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Right after WiFi.begin(ssid, pass) returns, what is true?
WiFi.begin() hands the settings to the driver, starts the connection and returns without waiting. That is why the sketch then polls WiFi.status() in a loop.
02In what order does the driver work?
It must first find the network, then join it, then prove the password and set up encryption keys, and only on a working, secure link can it ask DHCP for an address.
03During the WPA2 4-way handshake, what crosses the air?
Both sides derive the same 256-bit key from the password and SSID beforehand (PBKDF2, 4096 rounds). The four messages carry nonces and message integrity codes, so each side proves it has the key without ever sending it.
04In the Arduino core for ESP32, when does WiFi.status() become WL_CONNECTED?
The core sets WL_CONNECTED on the got-IP event. A link without an address cannot carry your data yet, so this is the right moment for the loop to end and for network code to start.
05The radio draws about 240 mA while sending. What does Espressif recommend for the supply?
The ESP32 datasheet recommends a 3.3 V supply rated 500 mA or more. A weak supply can dip at each transmit peak and reset the chip.
Glossary
- Station (STA)
- A Wi-Fi device that joins a network run by an access point, like a phone or our ESP32.
- Access point (AP)
- The device that runs a Wi-Fi network and that stations join; in a home it is part of the router.
- SSID
- The name of a Wi-Fi network, such as HomeNet.
- Probe request
- A frame a station sends on a channel to ask which networks are there; access points answer with a probe response.
- Association
- The step where a station asks an access point to join its network and the access point accepts.
- 4-way handshake
- Four WPA2 messages that prove both sides know the same key, made from the password, and set up fresh encryption keys.
- DHCP lease
- An IP address that a DHCP server lends to a device for a set time.
- Modem sleep
- A power-saving mode where the radio turns off between the access point's DTIM beacons while the CPU keeps running. A DTIM beacon tells sleeping stations whether data is waiting for them; it comes every Nth beacon, often every one.
Notes and sources
- The card shows only the join inside setup(). The full sketch includes <WiFi.h>, defines ssid and pass, starts Serial at 115200 baud, and in loop() checks WiFi.status() before using the network. Real firmware also gives the while loop a timeout, so a wrong password cannot hang it forever.
- With WPA2-Personal the 802.11 authentication exchange is the simple "open system" kind; the real proof of the password is the 4-way handshake that follows. WPA3-Personal replaces the password step with SAE; the Networking lesson “Defence: WPA3 and enterprise Wi-Fi (802.1X)” covers it.
- In the Arduino core for ESP32, status() becomes WL_CONNECTED only on the got-IP event; the link-up event (STA_CONNECTED) alone does not set it. In ESP-IDF itself it is the application’s job to call esp_wifi_connect() again after a disconnect; the Arduino core does that for you while auto-reconnect is on.
- The channel list depends on the country setting. With ESP-IDF’s default ("01") the ESP32 scans channels 1 to 11 actively and 12 to 14 passively (listening only); the figure shows the active ones. The Arduino core’s default fast scan stops at the first channel where the network answers, so it rarely needs every channel.
- Currents are from the “RF Current Consumption” table and the power-mode table of the ESP32 Series Datasheet v5.3 (3.3 V, 25 °C, transmit at 50% duty). Modem sleep (20-68 mA) is the whole chip with the CPU running at 80 to 240 MHz and the radio off. A weak USB cable or regulator can sag at each transmit peak and reset the board.
- ESP-IDF keeps the station settings (SSID and password) in NVS, a small key-value store in flash, so they survive a reboot. The Embedded lesson “Saving data in EEPROM and flash” explains how flash storage like this works.
- ESP-IDF Programming Guide: Wi-Fi driver, Station Scenarios (scan, connect, got IP, disconnect)
- ESP-IDF Programming Guide: Wi-Fi driver, Overview (country and channels, configuration)
- ESP-IDF Programming Guide: Wi-Fi Performance and Power Save
- Espressif ESP32 Series Datasheet v5.3 (RF current, power modes, power supply)
- Espressif ESP32-DevKitC V4 user guide
- Arduino core for ESP32: Wi-Fi API
- Arduino core for ESP32 source: WiFiType.h (wl_status_t)
- Arduino core for ESP32 source: STA.cpp (status, auto-reconnect)
- IEEE 802.11i-2004 and the 4-way handshake (Wikipedia)
- Wi-Fi Protected Access: WPA-Personal key derivation (Wikipedia)
- RFC 8018: PKCS #5 v2.1 (PBKDF2)
- RFC 2131: Dynamic Host Configuration Protocol
- RFC 1918: Address Allocation for Private Internets
Quiz Unlocks after the last step.