Skip to content

Commit cae08d1

Browse files
committed
feat(wifi): Allow setting minimum time for wifi scan
1 parent 0fa4aa6 commit cae08d1

File tree

7 files changed

+176
-5
lines changed

7 files changed

+176
-5
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This example demonstrates how to use the WiFi library to scan available WiFi net
44

55
## Supported Targets
66

7-
Currently this example supports the following targets.
7+
Currently, this example supports the following targets.
88

99
| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 | ESP32-C6 |
1010
| ----------------- | ----- | -------- | -------- | -------- | -------- |
@@ -45,7 +45,7 @@ Nr | SSID | RSSI | CH | Encryption
4545
* **Programming Fail:** If the programming/flash procedure fails, try to reduce the serial connection speed.
4646
* **COM port not detected:** Check the USB cable connection and the USB to Serial driver installation.
4747

48-
If the error persist, you can ask help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
48+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
4949

5050
## Contribute
5151

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This example demonstrates how to use the WiFi library to scan available WiFi net
44

55
## Supported Targets
66

7-
Currently this example supports the following targets.
7+
Currently, this example supports the following targets.
88

99
| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 | ESP32-C6 |
1010
| ----------------- | ----- | -------- | -------- | -------- | -------- |
@@ -55,7 +55,7 @@ Nr | SSID | RSSI | CH | Encryption
5555
* **Programming Fail:** If the programming/flash procedure fails, try to reduce the serial connection speed.
5656
* **COM port not detected:** Check the USB cable connection and the USB to Serial driver installation.
5757

58-
If the error persist, you can ask help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
58+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
5959

6060
## Contribute
6161

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

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# WiFiScanTime Example
2+
3+
This example demonstrates how to use the WiFi library to scan available WiFi networks with custom scan timing 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+
Scan done, elapsed time: 4960 ms
31+
17 networks found
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
38+
...
39+
```
40+
41+
## Troubleshooting
42+
43+
***Important: Be sure you're using a good quality USB cable and you have enough power source for your project.***
44+
45+
* **Programming Fail:** If the programming/flash procedure fails, try to reduce the serial connection speed.
46+
* **COM port not detected:** Check the USB cable connection and the USB to Serial driver installation.
47+
48+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
49+
50+
## Contribute
51+
52+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
53+
54+
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!
55+
56+
Before creating a new issue, be sure to try the Troubleshooting and to check if the same issue was already created by someone else.
57+
58+
## Resources
59+
60+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
61+
* ESP32 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
62+
* ESP32-S2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf)
63+
* 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)
65+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* This sketch demonstrates how to scan WiFi networks with custom scanning time.
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+
7+
/*
8+
* WiFi scan timing parameters explained:
9+
*
10+
* min=0, max=0: scan dwells on each channel for 120 ms.
11+
* min>0, max=0: scan dwells on each channel for 120 ms.
12+
* min=0, max>0: scan dwells on each channel for max ms.
13+
* min>0, max>0: the minimum time the scan dwells on each channel is min ms. If no AP is found during this time frame, the scan switches to the next channel. Otherwise, the scan dwells on the channel for max ms.
14+
*/
15+
16+
#include "WiFi.h"
17+
18+
void wifiScan(uint16_t min_time, uint16_t max_time) {
19+
Serial.println("Scan start");
20+
21+
// Set the minimum time per channel for active scanning.
22+
WiFi.setScanActiveMinTime(min_time);
23+
24+
// Capture the start time of the scan.
25+
uint32_t start = millis();
26+
27+
// WiFi.scanNetworks will return the number of networks found.
28+
// Scan networks with those options: Synchrone mode, show hidden networks, active scan, max scan time per channel.
29+
int n = WiFi.scanNetworks(false, true, false, max_time);
30+
Serial.printf("Scan done, elapsed time: %lu ms\n", millis() - start);
31+
if (n == 0) {
32+
Serial.println("no networks found");
33+
} else {
34+
Serial.print(n);
35+
Serial.println(" networks found");
36+
Serial.println("Nr | SSID | RSSI | CH | Encryption");
37+
for (int i = 0; i < n; ++i) {
38+
// Print SSID and RSSI for each network found
39+
Serial.printf("%2d", i + 1);
40+
Serial.print(" | ");
41+
Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
42+
Serial.print(" | ");
43+
Serial.printf("%4ld", WiFi.RSSI(i));
44+
Serial.print(" | ");
45+
Serial.printf("%2ld", WiFi.channel(i));
46+
Serial.print(" | ");
47+
switch (WiFi.encryptionType(i)) {
48+
case WIFI_AUTH_OPEN: Serial.print("open"); break;
49+
case WIFI_AUTH_WEP: Serial.print("WEP"); break;
50+
case WIFI_AUTH_WPA_PSK: Serial.print("WPA"); break;
51+
case WIFI_AUTH_WPA2_PSK: Serial.print("WPA2"); break;
52+
case WIFI_AUTH_WPA_WPA2_PSK: Serial.print("WPA+WPA2"); break;
53+
case WIFI_AUTH_WPA2_ENTERPRISE: Serial.print("WPA2-EAP"); break;
54+
case WIFI_AUTH_WPA3_PSK: Serial.print("WPA3"); break;
55+
case WIFI_AUTH_WPA2_WPA3_PSK: Serial.print("WPA2+WPA3"); break;
56+
case WIFI_AUTH_WAPI_PSK: Serial.print("WAPI"); break;
57+
default: Serial.print("unknown");
58+
}
59+
Serial.println();
60+
delay(10);
61+
}
62+
}
63+
Serial.println("");
64+
65+
// Delete the scan result to free memory for code below.
66+
WiFi.scanDelete();
67+
68+
// Wait a bit before scanning again
69+
delay(2000);
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+
// Scan WiFi networks with a minimum time of 100 ms per channel and a maximum time of 300 ms per channel (default values).
82+
wifiScan(100, 300);
83+
84+
// Scan WiFi networks with a minimum time of 100 ms per channel and a maximum time of 1500 ms per channel.
85+
wifiScan(100, 1500);
86+
87+
// Scan WiFi networks with a minimum time of 0 ms per channel and a maximum time of 1500 ms per channel.
88+
wifiScan(0, 1500);
89+
}
90+
91+
void loop() {
92+
// Nothing to do here
93+
}

Diff for: libraries/WiFi/examples/WiFiScanTime/ci.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"targets": {
3+
"esp32h2": false
4+
}
5+
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@ bool WiFiScanClass::_scanAsync = false;
4646
uint32_t WiFiScanClass::_scanStarted = 0;
4747
uint32_t WiFiScanClass::_scanTimeout = 60000;
4848
uint16_t WiFiScanClass::_scanCount = 0;
49+
uint32_t WiFiScanClass::_scanActiveMinTime = 100;
50+
4951
void *WiFiScanClass::_scanResult = 0;
5052

5153
void WiFiScanClass::setScanTimeout(uint32_t ms) {
5254
WiFiScanClass::_scanTimeout = ms;
5355
}
5456

57+
void WiFiScanClass::setScanActiveMinTime(uint32_t ms) {
58+
WiFiScanClass::_scanActiveMinTime = ms;
59+
}
60+
5561
/**
5662
* Start scan WiFi networks available
5763
* @param async run in async mode
@@ -80,7 +86,7 @@ int16_t
8086
config.scan_time.passive = max_ms_per_chan;
8187
} else {
8288
config.scan_type = WIFI_SCAN_TYPE_ACTIVE;
83-
config.scan_time.active.min = 100;
89+
config.scan_time.active.min = _scanActiveMinTime;
8490
config.scan_time.active.max = max_ms_per_chan;
8591
}
8692
if (esp_wifi_scan_start(&config, false) == ESP_OK) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class WiFiScanClass {
3232

3333
public:
3434
void setScanTimeout(uint32_t ms);
35+
void setScanActiveMinTime(uint32_t ms);
3536

3637
int16_t scanNetworks(
3738
bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300, uint8_t channel = 0, const char *ssid = nullptr,
@@ -62,6 +63,7 @@ class WiFiScanClass {
6263
static uint32_t _scanStarted;
6364
static uint32_t _scanTimeout;
6465
static uint16_t _scanCount;
66+
static uint32_t _scanActiveMinTime;
6567

6668
static void *_scanResult;
6769

0 commit comments

Comments
 (0)