🛠 Scanner les réseaux WiFi environnants
Objectives
Use the ESP32-S2's 2.4 GHz WiFi controller to detect nearby networks: initialize the WiFi stack in station mode, launch a passive scan, and display the SSID, RSSI, channel, and security type on the serial console. Important reminder: the S2 chip (single-core Xtensa LX7 at 240 MHz) has no Bluetooth; only the 2.4 GHz 802.11 b/g/n band is available.
Hardware
- ESP32-S2 board (DevKitM-1 or equivalent)
- USB cable and computer with serial monitor
- Arduino IDE environment with the esp32 support installed
Complete Code
// Scan des réseaux WiFi sur ESP32-S2
#include <WiFi.h>
void setup {
Serial.begin(115200);
delay(1000);
WiFi.mode(WIFI_MODE_STA); // le scan exige le mode station
Serial.println("Scan des reseaux WiFi 2,4 GHz...");
int n = WiFi.scanNetworks; // scan passif, bloque pendant ~2 s
if (n == 0) {
Serial.println("Aucun reseau trouve.");
} else {
Serial.printf("%u reseau(x) trouve(s) :\n", n);
for (int i = 0; i < n; i++) {
Serial.printf("%2d | %-24s | RSSI %4d dBm | canal %2d | %s\n",
i + 1, WiFi.SSID(i).c_str, WiFi.RSSI(i), WiFi.channel(i),
WiFi.encryptionType(i) == WIFI_AUTH_OPEN ? "ouvert" : "protege");
}
}
WiFi.scanDelete; // libère la RAM du résultat
}
void loop {}
Expected Verification
At startup, the serial console at 115200 bauds lists the visible networks once: name (SSID), signal strength (RSSI in dBm), channel, and security. Bring the board closer to the router: the RSSI of the corresponding network rises towards -30/-50 dBm. Moving the board changes the list.
Common Errors
- Forgetting
WiFi.mode(WIFI_MODE_STA): without an active station mode, the scan returns 0 networks. - Relaunching the scan in
loopwithout callingWiFi.scanDelete: available RAM collapses rapidly. - Looking for a 5 GHz network: the S2's radio only handles 2.4 GHz.
- Expecting Bluetooth: the ESP32-S2 does not include any Bluetooth, unlike the classic ESP32; any BLE scan is impossible.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht22.inoArduinooled-i2c.inoArduinoserveur-web.inoArduino