wemos D1 arduino IoT Development Board Application Note 5 - Scan Hotspots

foreword

In this section, we learn to use the WIFI Scan function of WeMos D1, and write two instances to implement synchronous scanning and asynchronous scanning respectively. The so-called synchronous scanning means that after calling the scanning function, the program will block until the scanning ends, and directly call WiFi.scanComplete() You can view the scan results; and asynchronous operation means that after calling the scan function, the program will continue to execute, but you need to wait until the scan is completed to obtain valid results.

1. Basic knowledge

1. Introduction to WIFI Scan

Usually to join a wireless network, we need to find its network name, that is, SSID, and use the WIFI Scan function to get the SSID of the surrounding wireless network.

Scanning nearby hotspots is also often used in product production testing. Because there are many processes in the factory, it is impossible to test all functions in each process. By testing the strength of scanning hotspots, you can judge whether the module’s WIFI performance is OK.

2.ESP8266WiFi library


As can be seen from the above figure, the ESP8266WiFi library mainly includes Station, Soft AP, config, WiFiClient, WiFiServer, scan, and hostname. This section mainly introduces the use of scan-related functions.

2. Examples

1 Synchronized scan

1.1 Procedure

#include "ESP8266WiFi.h" 
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");
}
 
void loop() {
 Serial.println("scan start");
 
 // WiFi.scanNetworks will return the number of networks found
 int n = WiFi.scanNetworks();
 Serial.println("scan done");
 if (n == 0)
 Serial.println("no networks found");
 else
 {
 Serial.print(n);
 Serial.println(" networks found");
 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) == ENC_TYPE_NONE)?" ":"*");
 delay(10);
 }
 }
 Serial.println("");
 
 // Wait a bit before scanning again
 delay(5000);
}

In the above program, first set the module to WIFI_STA mode, which is the mode in which the client can connect to the wireless network. The program first calls WiFi.scanNetworks() to start scanning, and then calls WiFi.SSID() to get the wireless network name, WiFi.RSSI () to obtain the signal strength of the wireless network, WiFi.encryptionType(i) to obtain the encryption method. Then print them out one by one.

1.2 Compile and run

2 Asynchronous scan

2.1 Procedure

#include "ESP8266WiFi.h"
#include <Ticker.h>
 
Ticker tk;
 
int timer_flag = 0;
int scan_res = 0;
 
void time_coming_cicyle_cb(void)
{
 timer_flag = 1;
}
 
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);
 tk.attach(5, time_coming_cicyle_cb);
 Serial.println("Setup done");
}
 
void loop() {
 
 if (1 == timer_flag)
 {
 Serial.println("scan start");
 timer_flag = 0;
 WiFi.scanNetworks(true);
 }
 
 scan_res = WiFi.scanComplete(); 
 if (-1 == scan_res)
 {
 Serial.println("scan not finish");
 }
 else if(-2 == scan_res)
 {
 Serial.println("scan not triggered");
 }
 else if(scan_res >= 0)
 {
 Serial.println("scan done");
 if (scan_res == 0)
 Serial.println("no networks found");
 else
 {
 Serial.print(scan_res);
 Serial.println(" networks found");
 for (int i = 0; i < scan_res; ++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) == ENC_TYPE_NONE)?" ":"*");
 delay(10);
 }
 }
 Serial.println("");
 }
 
 // Wait a bit before scanning again
 delay(500);
}

In the above program, the program first sets a 5-second periodic timer. When the timer is up, the timer_flag flag is set to 1, and the loop() function is executed in a loop. When the timer_flag is detected to be 1, the asynchronous scan function is called once, and then the flag is cleared, loop () function also calls WiFi.scanComplete() to get the scan result.

2.2 Compile and run

Since a 5-second timer is set in setup(), the scan is not triggered within 5 seconds of program execution, so WiFi.scanComplete() returns -2, prints scan not triggered, and the result cannot be obtained immediately after scanning, so WiFi. scanComplete() returns -1, prints scan not finish, and then obtains and prints the scanned results.

3. Conclusion

1. Use asynchronous scan or synchronous scan?

Generally speaking, asynchronous scanning can be used, so that the program can continue to execute without being affected by scanning. For certain scenarios, synchronous scanning is also possible.

2. Get the source code

If you have any questions during use, please join the QQ group for further communication.

QQ exchange group: 906015840 (Note: Internet of Things project exchange)

Official account: Internet of Things Inn, scan the code to follow, and reply to wemos.

Produced by One Leaf Gusha: One Sand One World, One Leaf One Bodhi

Posted by sissy on Wed, 25 May 2022 05:01:12 +0300