docs

a slatepencil documentail site

View on GitHub

WIFI SCAN EXAMPLE

code

/*
 *  This sketch demonstrates how to scan WiFi networks.
 *  The API is based on the Arduino WiFi Shield library, but has significant changes as newer WiFi functions are supported.
 *  E.g. the return value of `encryptionType()` different because more modern encryption is supported.
 */
#include "WiFi.h"

void setup() {
  Serial.begin(115200);

  // Set WiFi to station mode and disconnect from an AP if it was previously connected.
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  Serial.println("Setup done");
}

void loop() {
  Serial.println("Scan start");

  // WiFi.scanNetworks will return the number of networks found.
  int n = WiFi.scanNetworks();
  Serial.println("Scan done");
  if (n == 0) {
    Serial.println("no networks found");
  } else {
    Serial.print(n);
    Serial.println(" networks found");
    Serial.println("Nr | SSID                             | RSSI | CH | Encryption");
    for (int i = 0; i < n; ++i) {
      // Print SSID and RSSI for each network found
      Serial.printf("%2d", i + 1);
      Serial.print(" | ");
      Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
      Serial.print(" | ");
      Serial.printf("%4d", WiFi.RSSI(i));
      Serial.print(" | ");
      Serial.printf("%2d", WiFi.channel(i));
      Serial.print(" | ");
      switch (WiFi.encryptionType(i)) {
        case WIFI_AUTH_OPEN:
          Serial.print("open");
          break;
        case WIFI_AUTH_WEP:
          Serial.print("WEP");
          break;
        case WIFI_AUTH_WPA_PSK:
          Serial.print("WPA");
          break;
        case WIFI_AUTH_WPA2_PSK:
          Serial.print("WPA2");
          break;
        case WIFI_AUTH_WPA_WPA2_PSK:
          Serial.print("WPA+WPA2");
          break;
        case WIFI_AUTH_WPA2_ENTERPRISE:
          Serial.print("WPA2-EAP");
          break;
        case WIFI_AUTH_WPA3_PSK:
          Serial.print("WPA3");
          break;
        case WIFI_AUTH_WPA2_WPA3_PSK:
          Serial.print("WPA2+WPA3");
          break;
        case WIFI_AUTH_WAPI_PSK:
          Serial.print("WAPI");
          break;
        default:
          Serial.print("unknown");
      }
      Serial.println();
      delay(10);
    }
  }
  Serial.println("");

  // Delete the scan result to free memory for code below.
  WiFi.scanDelete();

  // Wait a bit before scanning again.
  delay(5000);
}

result

Scan start
Scan done
25 networks found
Nr | SSID                             | RSSI | CH | Encryption
 1 | Redmi_BF53                       |  -48 |  6 | WPA2
 2 | zhang                            |  -52 |  6 | WPA2
 3 | ChinaNet-Utng                    |  -55 | 13 | WPA+WPA2
 4 | 509                              |  -59 | 12 | WPA+WPA2
 5 | HUAWEI-P10N4U                    |  -70 |  1 | WPA+WPA2
 6 | HUAWEI-P10N4U                    |  -74 |  1 | WPA+WPA2
 7 | ChinaNet-XnzF                    |  -75 |  1 | WPA+WPA2
 8 | MERCURY_C0BF                     |  -79 |  6 | WPA+WPA2
 9 | 601                              |  -79 | 11 | WPA+WPA2
10 | someguy                          |  -80 |  6 | WPA+WPA2
11 | somegirl                         |  -82 |  8 | WPA+WPA2
12 | linghy_Wi-Fi5                    |  -83 |  1 | WPA2
13 | CMCC-jKGz                        |  -85 |  1 | WPA2
14 | linghy                           |  -85 |  1 | WPA2
15 | TP-LINK_01B7                     |  -87 |  6 | WPA+WPA2
16 | 301                              |  -89 |  1 | WPA+WPA2
17 | TP-LINK_ED48                     |  -89 | 11 | WPA+WPA2