/esp32-wifi-finder.cpp Secret
Last active
April 22, 2021 19:13
A snippet to find a specific WiFi network with ESP32
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 piece of code follows a question about the article posted by Sara & Rui: | |
* | |
* ESP32 Useful Wi-Fi Library Functions (Arduino IDE) | |
* @see https://randomnerdtutorials.com/esp32-useful-wi-fi-functions-arduino/ | |
* | |
* Here is Jim's question: | |
* | |
* Is there a way to quickly do a WiFi Scan for a SPECIFIC SSID and, | |
* when it detects the WiFi SSID is available (or not), | |
* does something with this information in the sketch? | |
* | |
* @see https://randomnerdtutorials.com/esp32-useful-wi-fi-functions-arduino/#comment-555652 | |
* | |
* Here is a simple solution... | |
*/ | |
#include <Arduino.h> | |
#include <WiFi.h> | |
const char * SPECIFIC_SSID = "MyNetwork"; | |
const char * ENCRYPTION_TYPE[] = { | |
"Open", | |
"WEP", | |
"WPA-PSK", | |
"WPA2-PSK", | |
"WPA-WPA2-PSK", | |
"WPA2-Enterprise", | |
"Unknown" | |
}; | |
struct WiFiInfo { | |
bool found; | |
int32_t channel; | |
int32_t rssi; | |
wifi_auth_mode_t auth_mode; | |
} wifi_info; | |
void findWiFi(const char * const ssid, WiFiInfo * const info) { | |
info->found = false; | |
int16_t n = WiFi.scanNetworks(); | |
for (uint8_t i=0; i<n; ++i) { | |
if (strcmp(WiFi.SSID(i).c_str(), ssid) == 0) { | |
info->found = true; | |
info->channel = WiFi.channel(i); | |
info->rssi = WiFi.RSSI(i); | |
info->auth_mode = WiFi.encryptionType(i); | |
return; | |
} | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
findWiFi(SPECIFIC_SSID, &wifi_info); | |
Serial.printf(wifi_info.found | |
? "SSID: %s, channel: %i, RSSI: %i dBm, encryption: %s\n" | |
: "SSID: %s ... could not be found\n", | |
SPECIFIC_SSID, | |
wifi_info.channel, | |
wifi_info.rssi, | |
ENCRYPTION_TYPE[wifi_info.auth_mode]); | |
} | |
void loop() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment