-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <me-no-dev@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
187 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# WiFiScanAsync Example | ||
|
||
This example demonstrates how to use the WiFi library to scan available WiFi networks in asynchronous mode and print the results. | ||
|
||
## Supported Targets | ||
|
||
Currently this example supports the following targets. | ||
|
||
| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 | ESP32-C6 | | ||
| ----------------- | ----- | -------- | -------- | -------- | -------- | | ||
|
||
## How to Use Example | ||
|
||
* How to install the Arduino IDE: [Install Arduino IDE](https://github.com/espressif/arduino-esp32/tree/master/docs/arduino-ide). | ||
|
||
#### Using Arduino IDE | ||
|
||
* Before Compile/Verify, select the correct board: `Tools -> Board`. | ||
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port. | ||
|
||
#### Using Platform IO | ||
|
||
* Select the COM port: `Devices` or setting the `upload_port` option on the `platformio.ini` file. | ||
|
||
## Example/Log Output | ||
|
||
``` | ||
Setup done | ||
Scan start | ||
Loop running... | ||
Loop running... | ||
Loop running... | ||
Loop running... | ||
Loop running... | ||
Loop running... | ||
Loop running... | ||
Loop running... | ||
Loop running... | ||
Scan done | ||
17 networks found | ||
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 | ||
... | ||
``` | ||
|
||
## Troubleshooting | ||
|
||
***Important: Be sure you're using a good quality USB cable and you have enought power source for your project.*** | ||
|
||
* **Programming Fail:** If the programming/flash procedure fails, try to reduce the serial connection speed. | ||
* **COM port not detected:** Check the USB cable connection and the USB to Serial driver installation. | ||
|
||
If the error persist, you can ask help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute). | ||
|
||
## Contribute | ||
|
||
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst) | ||
|
||
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! | ||
|
||
Before creating a new issue, be sure to try the Troubleshooting and to check if the same issue was already created by someone else. | ||
|
||
## Resources | ||
|
||
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32) | ||
* ESP32 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf) | ||
* ESP32-S2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf) | ||
* ESP32-C3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf) | ||
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf) | ||
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com) |
102 changes: 102 additions & 0 deletions
102
libraries/WiFi/examples/WiFiScanAsync/WiFiScanAsync.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
This sketch demonstrates how to scan WiFi networks in Async Mode. | ||
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 startWiFiScan() { | ||
Serial.println("Scan start"); | ||
// WiFi.scanNetworks will return immediately in Async Mode. | ||
WiFi.scanNetworks(true); // 'true' turns Async Mode ON | ||
} | ||
|
||
void printScannedNetworks(uint16_t networksFound) { | ||
if (networksFound == 0) { | ||
Serial.println("no networks found"); | ||
} else { | ||
Serial.println("\nScan done"); | ||
Serial.print(networksFound); | ||
Serial.println(" networks found"); | ||
Serial.println("Nr | SSID | RSSI | CH | Encryption"); | ||
for (int i = 0; i < networksFound; ++i) { | ||
// Print SSID and RSSI for each network found | ||
Serial.printf("%2d", i + 1); | ||
Serial.print(" | "); | ||
Serial.printf("%-32.32s", WiFi.SSID(i).c_str()); | ||
Serial.print(" | "); | ||
Serial.printf("%4ld", WiFi.RSSI(i)); | ||
Serial.print(" | "); | ||
Serial.printf("%2ld", 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(""); | ||
// Delete the scan result to free memory for code below. | ||
WiFi.scanDelete(); | ||
} | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
|
||
// Set WiFi to station mode and disconnect from an AP if it was previously connected. | ||
WiFi.mode(WIFI_STA); | ||
WiFi.disconnect(); | ||
delay(100); | ||
|
||
Serial.println("Setup done"); | ||
startWiFiScan(); | ||
} | ||
|
||
void loop() { | ||
// check WiFi Scan Async process | ||
int16_t WiFiScanStatus = WiFi.scanComplete(); | ||
if (WiFiScanStatus < 0) { // it is busy scanning or got an error | ||
if (WiFiScanStatus == WIFI_SCAN_FAILED) { | ||
Serial.println("WiFi Scan has failed. Starting again."); | ||
startWiFiScan(); | ||
} | ||
// other option is status WIFI_SCAN_RUNNING - just wait. | ||
} else { // Found Zero or more Wireless Networks | ||
printScannedNetworks(WiFiScanStatus); | ||
startWiFiScan(); // start over... | ||
} | ||
|
||
// Loop can do something else... | ||
delay(250); | ||
Serial.println("Loop running..."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters