Skip to content

Instantly share code, notes, and snippets.

@m1cr0lab
Last active April 22, 2021 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save m1cr0lab/fd125625ca1321cedb0f407c41b59042 to your computer and use it in GitHub Desktop.
Save m1cr0lab/fd125625ca1321cedb0f407c41b59042 to your computer and use it in GitHub Desktop.
A snippet to find a specific WiFi network with ESP32
/**
* 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