🛠 Scan des réseaux Wi-Fi

ESP32 (original, ESP32-WROOM-32 class) · debutant · Schéma : IMG-esp32-debutant-1-wifi-scan-reseaux.svg — Carte ESP32 à côté d'un routeur Wi-Fi avec liste des réseaux détectés et barres de signal

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials