//*
// * 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"
//int led = 9; // the PWM pin the LED is attached to
//int brightness = 0; // how bright the LED is
//int fadeAmount = 5; // how many points to fade the LED by
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;
// the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long currentMillis = 0;
// constants won't change:
const long interval = 500; // interval at which to blink (milliseconds)
const long interval2= 1000; // delay for recycle (milliseconds)
void setup() {
Serial.begin(115200);
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
// Set WiFi to station mode and disconnect from an AP if it was previously connected.
WiFi.mode(WIFI_STA);
//Serial.println(WiFi.macAddress());
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
//delay(2000);
}
void loop() {
unsigned long currentMillis = millis();
// Call Functions:
BlinkLED(); //Blink 500ms
ScanWiFi(); //Scan for active WiFi
}
void BlinkLED() {
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
void ScanWiFi() {
Serial.println("Scan start");
//unsigned long currentMillis = millis();
//if (currentMillis - previousMillis <= interval2) {
// 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();
}