Skip to content

Commit a6fefb2

Browse files
RotzbuaP-R-O-C-H-Y
andauthored
Update WiFiScan example to show more useful data (#7378)
* Update `WiFiScan` example to show more useful data * Add channel to output. * Add type of encryption to output. * Format output as table. * Update outdated example description. * Add `scanDelete()` as hint to free memory. * added 32 chars SSID + readme edit Co-authored-by: Jan Procházka <[email protected]>
1 parent e9c125f commit a6fefb2

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

Diff for: libraries/WiFi/examples/WiFiScan/README.md

+11-20
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
This example demonstrates how to use the WiFi library to scan available WiFi networks and print the results.
44

5-
# Supported Targets
5+
## Supported Targets
66

77
Currently this example supports the following targets.
88

9-
| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 |
10-
| ----------------- | ----- | -------- | -------- |
9+
| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 |
10+
| ----------------- | ----- | -------- | -------- | -------- |
1111

1212
## How to Use Example
1313

@@ -25,25 +25,16 @@ Currently this example supports the following targets.
2525
## Example/Log Output
2626

2727
```
28-
ets Jul 29 2019 12:21:46
29-
30-
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
31-
configsip: 0, SPIWP:0xee
32-
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
33-
mode:DIO, clock div:1
34-
load:0x3fff0030,len:1412
35-
load:0x40078000,len:13400
36-
load:0x40080400,len:3672
37-
entry 0x400805f8
3828
Setup done
39-
scan start
40-
scan done
29+
Scan start
30+
Scan done
4131
17 networks found
42-
1: IoTNetwork (-62)*
43-
2: WiFiSSID (-62)*
44-
3: B3A7992 (-63)*
45-
4: WiFi (-63)
46-
5: IoTNetwork2 (-64)*
32+
Nr | SSID | RSSI | CH | Encryption
33+
1 | IoTNetwork | -62 | 1 | WPA2
34+
2 | WiFiSSID | -62 | 1 | WPA2-EAP
35+
3 | B3A7992 | -63 | 6 | WPA+WPA2
36+
4 | WiFi | -63 | 6 | WPA3
37+
5 | IoTNetwork2 | -64 | 11 | WPA2+WPA3
4738
...
4839
```
4940

Diff for: libraries/WiFi/examples/WiFiScan/WiFiScan.ino

+52-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/*
22
* 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.
55
*/
66
#include "WiFi.h"
77

88
void setup()
99
{
1010
Serial.begin(115200);
1111

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.
1313
WiFi.mode(WIFI_STA);
1414
WiFi.disconnect();
1515
delay(100);
@@ -19,30 +19,68 @@ void setup()
1919

2020
void loop()
2121
{
22-
Serial.println("scan start");
22+
Serial.println("Scan start");
2323

24-
// WiFi.scanNetworks will return the number of networks found
24+
// WiFi.scanNetworks will return the number of networks found.
2525
int n = WiFi.scanNetworks();
26-
Serial.println("scan done");
26+
Serial.println("Scan done");
2727
if (n == 0) {
2828
Serial.println("no networks found");
2929
} else {
3030
Serial.print(n);
3131
Serial.println(" networks found");
32+
Serial.println("Nr | SSID | RSSI | CH | Encryption");
3233
for (int i = 0; i < n; ++i) {
3334
// 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+
Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
38+
Serial.print(" | ");
39+
Serial.printf("%4d", WiFi.RSSI(i));
40+
Serial.print(" | ");
41+
Serial.printf("%2d", WiFi.channel(i));
42+
Serial.print(" | ");
43+
switch (WiFi.encryptionType(i))
44+
{
45+
case WIFI_AUTH_OPEN:
46+
Serial.print("open");
47+
break;
48+
case WIFI_AUTH_WEP:
49+
Serial.print("WEP");
50+
break;
51+
case WIFI_AUTH_WPA_PSK:
52+
Serial.print("WPA");
53+
break;
54+
case WIFI_AUTH_WPA2_PSK:
55+
Serial.print("WPA2");
56+
break;
57+
case WIFI_AUTH_WPA_WPA2_PSK:
58+
Serial.print("WPA+WPA2");
59+
break;
60+
case WIFI_AUTH_WPA2_ENTERPRISE:
61+
Serial.print("WPA2-EAP");
62+
break;
63+
case WIFI_AUTH_WPA3_PSK:
64+
Serial.print("WPA3");
65+
break;
66+
case WIFI_AUTH_WPA2_WPA3_PSK:
67+
Serial.print("WPA2+WPA3");
68+
break;
69+
case WIFI_AUTH_WAPI_PSK:
70+
Serial.print("WAPI");
71+
break;
72+
default:
73+
Serial.print("unknown");
74+
}
75+
Serial.println();
4176
delay(10);
4277
}
4378
}
4479
Serial.println("");
4580

46-
// Wait a bit before scanning again
81+
// Delete the scan result to free memory for code below.
82+
WiFi.scanDelete();
83+
84+
// Wait a bit before scanning again.
4785
delay(5000);
4886
}

0 commit comments

Comments
 (0)