Skip to content

Commit 8d1a845

Browse files
SuGliderme-no-dev
andauthored
Wifi async scan example and fix (#8981)
* Create WiFiScanAsync.ino * Create .skip.esp32h2 * Create README.md * Update README.md - adds C6 * Update wifi.rst with new example * avoid timeout with Async Mode --------- Co-authored-by: Me No Dev <[email protected]>
1 parent 42ed9c0 commit 8d1a845

File tree

6 files changed

+187
-8
lines changed

6 files changed

+187
-8
lines changed

Diff for: docs/source/api/wifi.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ Loads all infos from a scanned wifi in to the ptr parameters.
643643
644644
bool getNetworkInfo(uint8_t networkItem, String &ssid, uint8_t &encryptionType, int32_t &RSSI, uint8_t* &BSSID, int32_t &channel);
645645
646-
To see how to use the ``WiFiScan``, take a look at the ``WiFiScan.ino`` example available.
646+
To see how to use the ``WiFiScan``, take a look at the ``WiFiScan.ino`` or ``WiFiScanAsync.ino`` example available.
647647

648648
Examples
649649
--------

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ This example demonstrates how to use the WiFi library to scan available WiFi net
66

77
Currently this example supports the following targets.
88

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

1212
## How to Use Example
1313

@@ -61,4 +61,5 @@ Before creating a new issue, be sure to try the Troubleshooting and to check if
6161
* ESP32 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
6262
* ESP32-S2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf)
6363
* ESP32-C3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf)
64+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
6465
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)

Diff for: libraries/WiFi/examples/WiFiScanAsync/.skip.esp32h2

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

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

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# WiFiScanAsync Example
2+
3+
This example demonstrates how to use the WiFi library to scan available WiFi networks in asynchronous mode and print the results.
4+
5+
## Supported Targets
6+
7+
Currently this example supports the following targets.
8+
9+
| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 | ESP32-C6 |
10+
| ----------------- | ----- | -------- | -------- | -------- | -------- |
11+
12+
## How to Use Example
13+
14+
* How to install the Arduino IDE: [Install Arduino IDE](https://github.com/espressif/arduino-esp32/tree/master/docs/arduino-ide).
15+
16+
#### Using Arduino IDE
17+
18+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
19+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
20+
21+
#### Using Platform IO
22+
23+
* Select the COM port: `Devices` or setting the `upload_port` option on the `platformio.ini` file.
24+
25+
## Example/Log Output
26+
27+
```
28+
Setup done
29+
Scan start
30+
Loop running...
31+
Loop running...
32+
Loop running...
33+
Loop running...
34+
Loop running...
35+
Loop running...
36+
Loop running...
37+
Loop running...
38+
Loop running...
39+
40+
Scan done
41+
17 networks found
42+
Nr | SSID | RSSI | CH | Encryption
43+
1 | IoTNetwork | -62 | 1 | WPA2
44+
2 | WiFiSSID | -62 | 1 | WPA2-EAP
45+
3 | B3A7992 | -63 | 6 | WPA+WPA2
46+
4 | WiFi | -63 | 6 | WPA3
47+
5 | IoTNetwork2 | -64 | 11 | WPA2+WPA3
48+
...
49+
```
50+
51+
## Troubleshooting
52+
53+
***Important: Be sure you're using a good quality USB cable and you have enought power source for your project.***
54+
55+
* **Programming Fail:** If the programming/flash procedure fails, try to reduce the serial connection speed.
56+
* **COM port not detected:** Check the USB cable connection and the USB to Serial driver installation.
57+
58+
If the error persist, you can ask help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
59+
60+
## Contribute
61+
62+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
63+
64+
If you have any **feedback** or **issue** to report on this example/library, please open an issue or fix it by creating a new PR. Contributions are more than welcome!
65+
66+
Before creating a new issue, be sure to try the Troubleshooting and to check if the same issue was already created by someone else.
67+
68+
## Resources
69+
70+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
71+
* ESP32 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
72+
* ESP32-S2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf)
73+
* ESP32-C3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf)
74+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
75+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
+102
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+
}

Diff for: libraries/WiFi/src/WiFiScan.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,18 @@ void * WiFiScanClass::_getScanInfoByIndex(int i)
141141
*/
142142
int16_t WiFiScanClass::scanComplete()
143143
{
144-
if (WiFiScanClass::_scanStarted && (millis()-WiFiScanClass::_scanStarted) > WiFiScanClass::_scanTimeout) { //Check is scan was started and if the delay expired, return WIFI_SCAN_FAILED in this case
145-
WiFiGenericClass::clearStatusBits(WIFI_SCANNING_BIT);
146-
return WIFI_SCAN_FAILED;
147-
}
148-
149144
if(WiFiGenericClass::getStatusBits() & WIFI_SCAN_DONE_BIT) {
150145
return WiFiScanClass::_scanCount;
151146
}
152147

153148
if(WiFiGenericClass::getStatusBits() & WIFI_SCANNING_BIT) {
154149
return WIFI_SCAN_RUNNING;
155150
}
151+
// last one to avoid time affecting Async mode
152+
if (WiFiScanClass::_scanStarted && (millis()-WiFiScanClass::_scanStarted) > WiFiScanClass::_scanTimeout) { //Check is scan was started and if the delay expired, return WIFI_SCAN_FAILED in this case
153+
WiFiGenericClass::clearStatusBits(WIFI_SCANNING_BIT);
154+
return WIFI_SCAN_FAILED;
155+
}
156156

157157
return WIFI_SCAN_FAILED;
158158
}

0 commit comments

Comments
 (0)