/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
#include <Arduino.h> | |
#include <WiFi.h> | |
const char * SPECIFIC_SSID = "MyNetwork"; | |
const char * ENC_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, | |
ENC_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