🛠 Scanner les réseaux WiFi environnants

ESP32-S2 · intermediaire · Schéma : IMG-esp32-s2-intermediaire-1-esp32-s2-scan-wifi-reseaux.svg — ESP32-S2 reliée par câble USB à un ordinateur, la console série affiche la liste des réseaux WiFi 2,4 GHz détectés avec leur puissance de signal.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials