Skip to content

Update WiFiScan example to show more useful data #7378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions libraries/WiFi/examples/WiFiScan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

# Supported Targets
## Supported Targets

Currently this example supports the following targets.

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

```
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1412
load:0x40078000,len:13400
load:0x40080400,len:3672
entry 0x400805f8
Setup done
scan start
scan done
Scan start
Scan done
17 networks found
1: IoTNetwork (-62)*
2: WiFiSSID (-62)*
3: B3A7992 (-63)*
4: WiFi (-63)
5: IoTNetwork2 (-64)*
Nr | SSID | RSSI | CH | Encryption
1 | IoTNetwork | -62 | 1 | WPA2
2 | WiFiSSID | -62 | 1 | WPA2-EAP
3 | B3A7992 | -63 | 6 | WPA+WPA2
4 | WiFi | -63 | 6 | WPA3
5 | IoTNetwork2 | -64 | 11 | WPA2+WPA3
...
```

Expand Down
70 changes: 56 additions & 14 deletions libraries/WiFi/examples/WiFiScan/WiFiScan.ino
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* This sketch demonstrates how to scan WiFi networks.
* The API is almost the same as with the WiFi Shield library,
* the most obvious difference being the different file you need to include:
* 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"

void setup()
{
Serial.begin(115200);

// Set WiFi to station mode and disconnect from an AP if it was previously connected
// Set WiFi to station mode and disconnect from an AP if it was previously connected.
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Expand All @@ -19,30 +19,72 @@ void setup()

void loop()
{
Serial.println("scan start");
Serial.println("Scan start");

// WiFi.scanNetworks will return the number of networks found
// WiFi.scanNetworks will return the number of networks found.
int n = WiFi.scanNetworks();
Serial.println("scan done");
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");
Copy link
Contributor

@Xylopyrographer Xylopyrographer Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest changing this to accommodate 32 character SSID maximum to:

Serial.println( " # | SSID                             | RSSI | CH | Encryption" );

Suggest adding the following after the above to "pretty up" the output table:
Serial.println( "---------------------------------------------------------------" );

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will also suggest to add BSSID column to differentiate between APs with the same SSID

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@me-no-dev Should I increase the column size? The limitation was on purpose because it is easier to read a table row without a huge space between the columns. Most SSIDs were shorter than 15 chars on my test setup.

Copy link
Collaborator

@mrengineer7777 mrengineer7777 Nov 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is an example, I recommend printing the whole SSID. I agree a shorter SSID field looks better, but that can be customized for each application. This is the header I use for printing WiFi list (debug use only):

Serial.println(" #                             SSID  BSSID              RSSI  Ch  Authmode  Rate" );
Serial.println("-----------------------------------------------------------------------------------" );

I print the full SSID with sprintf(ptr, "%32s ", AP->ssid[0] != 0 ? (const char *)AP->ssid : "<hidden>");.

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)?" ":"*");
Serial.printf("%2d",i + 1);
Serial.print(" | ");
if (WiFi.SSID(i).length() < 15) { // Compact column for better readability.
Serial.printf("%-15s", WiFi.SSID(i).c_str());
} else {
Serial.print(WiFi.SSID(i));
}
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("");

// Wait a bit before scanning again
// Delete the scan result to free memory for code below.
WiFi.scanDelete();

// Wait a bit before scanning again.
delay(5000);
}