Skip to content

Commit 70d445b

Browse files
authored
Create WiFiScanAsync.ino
1 parent 51cb927 commit 70d445b

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
This sketch demonstrates how to scan WiFi networks in Async Mode.
3+
The API is based on the Arduino WiFi Shield library, but has significant changes as newer WiFi functions are supported.
4+
E.g. the return value of `encryptionType()` different because more modern encryption is supported.
5+
*/
6+
#include "WiFi.h"
7+
8+
void startWiFiScan() {
9+
Serial.println("Scan start");
10+
// WiFi.scanNetworks will return immediately in Async Mode.
11+
WiFi.scanNetworks(true); // 'true' turns Async Mode ON
12+
}
13+
14+
void printScannedNetworks(uint16_t networksFound) {
15+
if (networksFound == 0) {
16+
Serial.println("no networks found");
17+
} else {
18+
Serial.println("\nScan done");
19+
Serial.print(networksFound);
20+
Serial.println(" networks found");
21+
Serial.println("Nr | SSID | RSSI | CH | Encryption");
22+
for (int i = 0; i < networksFound; ++i) {
23+
// Print SSID and RSSI for each network found
24+
Serial.printf("%2d", i + 1);
25+
Serial.print(" | ");
26+
Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
27+
Serial.print(" | ");
28+
Serial.printf("%4ld", WiFi.RSSI(i));
29+
Serial.print(" | ");
30+
Serial.printf("%2ld", WiFi.channel(i));
31+
Serial.print(" | ");
32+
switch (WiFi.encryptionType(i))
33+
{
34+
case WIFI_AUTH_OPEN:
35+
Serial.print("open");
36+
break;
37+
case WIFI_AUTH_WEP:
38+
Serial.print("WEP");
39+
break;
40+
case WIFI_AUTH_WPA_PSK:
41+
Serial.print("WPA");
42+
break;
43+
case WIFI_AUTH_WPA2_PSK:
44+
Serial.print("WPA2");
45+
break;
46+
case WIFI_AUTH_WPA_WPA2_PSK:
47+
Serial.print("WPA+WPA2");
48+
break;
49+
case WIFI_AUTH_WPA2_ENTERPRISE:
50+
Serial.print("WPA2-EAP");
51+
break;
52+
case WIFI_AUTH_WPA3_PSK:
53+
Serial.print("WPA3");
54+
break;
55+
case WIFI_AUTH_WPA2_WPA3_PSK:
56+
Serial.print("WPA2+WPA3");
57+
break;
58+
case WIFI_AUTH_WAPI_PSK:
59+
Serial.print("WAPI");
60+
break;
61+
default:
62+
Serial.print("unknown");
63+
}
64+
Serial.println();
65+
delay(10);
66+
}
67+
Serial.println("");
68+
// Delete the scan result to free memory for code below.
69+
WiFi.scanDelete();
70+
}
71+
}
72+
73+
void setup() {
74+
Serial.begin(115200);
75+
76+
// Set WiFi to station mode and disconnect from an AP if it was previously connected.
77+
WiFi.mode(WIFI_STA);
78+
WiFi.disconnect();
79+
delay(100);
80+
81+
Serial.println("Setup done");
82+
startWiFiScan();
83+
}
84+
85+
void loop() {
86+
// check WiFi Scan Async process
87+
int16_t WiFiScanStatus = WiFi.scanComplete();
88+
if (WiFiScanStatus < 0) { // it is busy scanning or got an error
89+
if (WiFiScanStatus == WIFI_SCAN_FAILED) {
90+
Serial.println("WiFi Scan has failed. Starting again.");
91+
startWiFiScan();
92+
}
93+
// other option is status WIFI_SCAN_RUNNING - just wait.
94+
} else { // Found Zero or more Wireless Networks
95+
printScannedNetworks(WiFiScanStatus);
96+
startWiFiScan(); // start over...
97+
}
98+
99+
// Loop can do something else...
100+
delay(250);
101+
Serial.println("Loop running...");
102+
}

0 commit comments

Comments
 (0)