🛠 Scan des réseaux Wi-Fi
Objectives
Discover the ESP32 Wi-Fi stack (dual-core Xtensa, integrated 2.4 GHz radio) by performing a passive scan of surrounding networks. By the end, you will know how to initialize station mode, launch a scan, and interpret the SSID, RSSI, and channel of each network.
Hardware
- 1 ESP32 DevKit v1 board (ESP32-WROOM-32 module, 4 MB flash)
- 1 micro-USB cable (data, not just charging)
- A PC with a serial port
Complete code
#include <WiFi.h>
void setup {
Serial.begin(115200);
delay(500);
WiFi.mode(WIFI_STA); // mode station : indispensable avant un scan
WiFi.disconnect; // s'assure qu'aucune connexion ne gêne le scan
delay(200);
Serial.println(F("Scan en cours..."));
}
void loop {
int n = WiFi.scanNetworks;
if (n == 0) {
Serial.println(F("Aucun reseau trouve"));
} else {
Serial.printf("%d reseau(x) :\n", n);
for (int i = 0; i < n; i++) {
Serial.printf(" [%d] %-20s RSSI %d dBm canal %d\n",
i, WiFi.SSID(i).c_str, WiFi.RSSI(i), WiFi.channel(i));
}
}
delay(5000);
}
Expected verification
The console at 115200 bauds displays "Scan in progress..." followed by a list of networks, each showing the name (SSID), signal strength (RSSI between approximately -40 and -90 dBm), and channel (1 to 13). Moving the board closer to or further away from the router changes the RSSI. The scan repeats every 5 seconds.
Common errors
- "Failed to connect to ESP32: Invalid head of packet": Hold the BOOT button during the first second of flashing, or check the COM port and the selected board type.
- Brown-out detected (boot loops): Wi-Fi scanning causes current spikes; power via a good cable, not a tired USB hub.
- Empty list while the router is on: WiFi.mode(WIFI_STA) was forgotten before the scan.
- Scan perceived as very slow: it sweeps all 13 channels; use WiFi.scanNetworks(true) in asynchronous mode to avoid blocking loop.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht22.inoArduinooled-i2c.inoArduinoserveur-web.inoArduino