mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
Refactor WiFi scan example (#7655)
This commit is contained in:
parent
c65626622a
commit
95de525af9
@ -3,43 +3,57 @@
|
|||||||
The API is almost the same as with the WiFi Shield library,
|
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 most obvious difference being the different file you need to include:
|
||||||
*/
|
*/
|
||||||
#include "ESP8266WiFi.h"
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
Serial.println(F("\nESP8266 WiFi scan example"));
|
||||||
|
|
||||||
// Set WiFi to station mode and disconnect from an AP if it was previously connected
|
// Set WiFi to station mode
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
|
// Disconnect from an AP if it was previously connected
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
Serial.println("Setup done");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
Serial.println("scan start");
|
String ssid;
|
||||||
|
int32_t rssi;
|
||||||
|
uint8_t encryptionType;
|
||||||
|
uint8_t* bssid;
|
||||||
|
int32_t channel;
|
||||||
|
bool hidden;
|
||||||
|
int scanResult;
|
||||||
|
|
||||||
// WiFi.scanNetworks will return the number of networks found
|
Serial.println(F("Starting WiFi scan..."));
|
||||||
int n = WiFi.scanNetworks();
|
|
||||||
Serial.println("scan done");
|
scanResult = WiFi.scanNetworks(/*async=*/false, /*hidden=*/true);
|
||||||
if (n == 0) {
|
|
||||||
Serial.println("no networks found");
|
if (scanResult == 0) {
|
||||||
|
Serial.println(F("No networks found"));
|
||||||
|
} else if (scanResult > 0) {
|
||||||
|
Serial.printf(PSTR("%d networks found:\n"), scanResult);
|
||||||
|
|
||||||
|
// Print unsorted scan results
|
||||||
|
for (int8_t i = 0; i < scanResult; i++) {
|
||||||
|
WiFi.getNetworkInfo(i, ssid, encryptionType, rssi, bssid, channel, hidden);
|
||||||
|
|
||||||
|
Serial.printf(PSTR(" %02d: [CH %02d] [%02X:%02X:%02X:%02X:%02X:%02X] %ddBm %c %c %s\n"),
|
||||||
|
i,
|
||||||
|
channel,
|
||||||
|
bssid[0], bssid[1], bssid[2],
|
||||||
|
bssid[3], bssid[4], bssid[5],
|
||||||
|
rssi,
|
||||||
|
(encryptionType == ENC_TYPE_NONE) ? ' ' : '*',
|
||||||
|
hidden ? 'H' : 'V',
|
||||||
|
ssid.c_str());
|
||||||
|
delay(0);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Serial.print(n);
|
Serial.printf(PSTR("WiFi scan error %d"), scanResult);
|
||||||
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
|
// Wait a bit before scanning again
|
||||||
delay(5000);
|
delay(5000);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user