/* ESP32 WiFi Scanning example */
#include "WiFi.h"
#define LED 4
#define Button 13
int oldValue = LOW; // default/idle value for pin 13 is low.
void setup() {
Serial.begin(115200);
Serial.println("Initializing WiFi...");
WiFi.mode(WIFI_STA);
Serial.println("Setup done!");
pinMode(LED, OUTPUT);
pinMode(Button, INPUT);
}
void loop() {
int newValue = digitalRead(Button);
// Check if the value was changed,
// by comparing it with the previous value.
if(newValue != oldValue)
{
if(newValue == HIGH)
{
Serial.println("The button is pressed.");
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
Serial.println("Scanning...");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("Scan done!");
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
if (n == 0) {
Serial.println("No networks found.");
} else {
Serial.println();
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
}
}
}
else
{
Serial.println("The button is released.");
}
// Remember the value for the next time.
oldValue = newValue;
}
delay(100);
}