1
1
/*
2
2
* This sketch demonstrates how to scan WiFi networks.
3
- * The API is almost the same as with the WiFi Shield library,
4
- * the most obvious difference being the different file you need to include:
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
5
*/
6
6
#include " WiFi.h"
7
7
8
8
void setup ()
9
9
{
10
10
Serial.begin (115200 );
11
11
12
- // Set WiFi to station mode and disconnect from an AP if it was previously connected
12
+ // Set WiFi to station mode and disconnect from an AP if it was previously connected.
13
13
WiFi.mode (WIFI_STA);
14
14
WiFi.disconnect ();
15
15
delay (100 );
@@ -19,30 +19,69 @@ void setup()
19
19
20
20
void loop ()
21
21
{
22
- Serial.println (" scan start" );
22
+ Serial.println (" Scan start" );
23
23
24
- // WiFi.scanNetworks will return the number of networks found
24
+ // WiFi.scanNetworks will return the number of networks found.
25
25
int n = WiFi.scanNetworks ();
26
- Serial.println (" scan done" );
26
+ Serial.println (" Scan done" );
27
27
if (n == 0 ) {
28
28
Serial.println (" no networks found" );
29
29
} else {
30
30
Serial.print (n);
31
31
Serial.println (" networks found" );
32
+ Serial.println (" Nr | SSID | RSSI | CH | Encryption" );
32
33
for (int i = 0 ; i < n; ++i) {
33
34
// Print SSID and RSSI for each network found
34
- Serial.print (i + 1 );
35
- Serial.print (" : " );
36
- Serial.print (WiFi.SSID (i));
37
- Serial.print (" (" );
38
- Serial.print (WiFi.RSSI (i));
39
- Serial.print (" )" );
40
- Serial.println ((WiFi.encryptionType (i) == WIFI_AUTH_OPEN)?" " :" *" );
35
+ Serial.printf (" %2d" ,i + 1 );
36
+ Serial.print (" | " );
37
+ if (WiFi.SSID (i).length () < 15 ) {
38
+ Serial.printf (" %-15s" , WiFi.SSID (i).c_str ());
39
+ } else {
40
+ Serial.print (WiFi.SSID (i));
41
+ }
42
+ Serial.print (" | " );
43
+ Serial.printf (" %4d" , WiFi.RSSI (i));
44
+ Serial.print (" | " );
45
+ Serial.printf (" %2d" , WiFi.channel (i));
46
+ Serial.print (" | " );
47
+ switch (WiFi.encryptionType (i))
48
+ {
49
+ case WIFI_AUTH_OPEN:
50
+ Serial.print (" open" );
51
+ break ;
52
+ case WIFI_AUTH_WEP:
53
+ Serial.print (" WEP" );
54
+ break ;
55
+ case WIFI_AUTH_WPA_PSK:
56
+ Serial.print (" WPA" );
57
+ break ;
58
+ case WIFI_AUTH_WPA2_PSK:
59
+ Serial.print (" WPA2" );
60
+ break ;
61
+ case WIFI_AUTH_WPA_WPA2_PSK:
62
+ Serial.print (" WPA+WPA2" );
63
+ break ;
64
+ case WIFI_AUTH_WPA2_ENTERPRISE:
65
+ Serial.print (" WPA2-EAP" );
66
+ break ;
67
+ case WIFI_AUTH_WPA3_PSK:
68
+ Serial.print (" WPA3" );
69
+ break ;
70
+ case WIFI_AUTH_WPA2_WPA3_PSK:
71
+ Serial.print (" WPA2+WPA3" );
72
+ break ;
73
+ case WIFI_AUTH_WAPI_PSK:
74
+ Serial.print (" WAPI" );
75
+ break ;
76
+ default :
77
+ Serial.print (" unknown" );
78
+ }
79
+ Serial.println ();
41
80
delay (10 );
42
81
}
43
82
}
44
83
Serial.println (" " );
45
84
46
- // Wait a bit before scanning again
85
+ // Wait a bit before scanning again.
47
86
delay (5000 );
48
87
}
0 commit comments