ENع
→Joining Wi-Fi
الدرس 48 · Connectivity and the capstone node

Joining Wi-Fi from an ESP32

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 48 · تفاعلي
01 / 10

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. begin() 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);
    }
  3. 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 again
  4. Find: 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 there
  5. Join: 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 installed
  6. Prove: 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_CONNECTED
  7. Address: 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.1
  8. Got 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 IP
  9. The 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 code
  10. Power: 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

اختبار يتاح بعد الخطوة الأخيرة.