Get stored WiFi AP and STA info
If information about stored WiFi AP or WiFi STA configuration is needed before the WiFi AP is connected or the WiFi STA is activated, ESP-IDF provides the function esp_wifi_get_config() which fills the structure of the type wifi_config_t
typedef union {
wifi_ap_config_t ap; /**< configuration of AP */
wifi_sta_config_t sta; /**< configuration of STA */
} wifi_config_t;
wifi_ap_config_t contains information about the stored AP configuration
/**
@brief Soft-AP configuration settings for the ESP32 */
typedef struct {
uint8_t ssid[32]; /**< SSID of ESP32 soft-AP */
uint8_t password[64]; /**< Password of ESP32 soft-AP */
uint8_t ssid_len; /**< Length of SSID. If
softap_config.ssid_len==0, check the SSID until there is a termination
character; otherwise, set the SSID length according to
softap_config.ssid_len. */
uint8_t channel; /**< Channel of ESP32 soft-AP */
wifi_auth_mode_t authmode; /**< Auth mode of ESP32 soft-AP. Do not
support AUTH_WEP in soft-AP mode */
uint8_t ssid_hidden; /**< Broadcast SSID or not, default 0,
broadcast the SSID */
uint8_t max_connection; /**< Max number of stations allowed to
connect in, default 4, max 4 */
uint16_t beacon_interval; /**< Beacon interval, 100 ~ 60000 ms,
default 100 ms */
} wifi_ap_config_t;
wifi_sta_config_t contains information about the stored STA configuration
/**
@brief STA configuration settings for the ESP32 */
typedef struct {
uint8_t ssid[32]; /**< SSID of target AP*/
uint8_t password[64]; /**< password of target AP*/
wifi_scan_method_t scan_method; /**< do all channel scan or fast
scan */
bool bssid_set; /**< whether set MAC address of target AP or
not. Generally, station_config.bssid_set needs to be 0; and it needs to
be 1 only when users need to check the MAC address of the AP.*/
uint8_t bssid[6]; /**< MAC address of target AP*/
uint8_t channel; /**< channel of target AP. Set to 1~13 to
scan starting from the specified channel before connecting to AP. If the
channel of AP is unknown, set it to 0.*/
wifi_sort_method_t sort_method; /**< sort the connect AP in the
list by rssi or security mode */
wifi_fast_scan_threshold_t threshold; /**< When scan_method is
set to WIFI_FAST_SCAN, only APs which have an auth mode that is more
secure than the selected auth mode and a signal stronger than the
minimum RSSI will be used. */
} wifi_sta_config_t;
The following method prints this information to the serial.
#include <Arduino.h>
#include <WiFi.h>
#include <esp_wifi.h>
void setup() {
// Start serial connection
Serial.begin(115200);
// Get stored information before connecting to WiFi
wifi_config_t conf;
WiFi.mode(WIFI_AP_STA);
Serial.println("Stored STA configuration:");
Serial.println("========================");
esp_wifi_get_config(WIFI_IF_STA, &conf);
Serial.println("SSID: " + String(reinterpret_cast<const char*>(conf.sta.ssid)));
Serial.println("Password: " + String(reinterpret_cast<const char*>(conf.sta.password)));
Serial.println("BSSID: " + String(reinterpret_cast<const char*>(conf.sta.bssid)));
Serial.println("Channel: " + String(conf.sta.channel));
if (conf.sta.scan_method == WIFI_FAST_SCAN) {
Serial.println("WiFi scan method fast");
} else {
Serial.println("WiFi scan method all");
}
if (conf.sta.sort_method == WIFI_CONNECT_AP_BY_SIGNAL) {
Serial.println("WiFi scan results sorted by RSSI");
} else {
Serial.println("WiFi scan results sorted by auth type");
}
wifi_fast_scan_threshold_t staTreshold = conf.sta.threshold;
Serial.println("Minimum RSSI accepted: " + String(staTreshold.rssi));
Serial.print("Minimum required auth method: ");
switch (staTreshold.authmode) {
case WIFI_AUTH_OPEN:
Serial.println("OPEN");
break;
case WIFI_AUTH_WEP:
Serial.println("WEP");
break;
case WIFI_AUTH_WPA_PSK:
Serial.println("WPA PSK");
break;
case WIFI_AUTH_WPA2_PSK:
Serial.println("WPA2 PSK");
break;
case WIFI_AUTH_WPA_WPA2_PSK:
Serial.println("WPA WPA2 PSK");
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
Serial.println("WPA2 ENTERPRISE");
break;
case WIFI_AUTH_MAX:
Serial.println("MAX");
break;
}
Serial.println("\nStored AP configuration:");
Serial.println("=========================");
esp_wifi_get_config(WIFI_IF_AP, &conf);
Serial.println("SSID: " + String(reinterpret_cast<const char*>(conf.ap.ssid)));
Serial.println("Password: " + String(reinterpret_cast<const char*>(conf.ap.password)));
Serial.println("Channel: " + String(conf.ap.channel));
Serial.print("SSID is ");
if (conf.ap.ssid_hidden == 0) {
Serial.println("broadcasted");
} else {
Serial.println("hidden");
}
Serial.print("Auth method: ");
switch (conf.ap.authmode) {
case WIFI_AUTH_OPEN:
Serial.println("OPEN");
break;
case WIFI_AUTH_WEP:
Serial.println("WEP");
break;
case WIFI_AUTH_WPA_PSK:
Serial.println("WPA PSK");
break;
case WIFI_AUTH_WPA2_PSK:
Serial.println("WPA2 PSK");
break;
case WIFI_AUTH_WPA_WPA2_PSK:
Serial.println("WPA WPA2 PSK");
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
Serial.println("WPA2 ENTERPRISE");
break;
case WIFI_AUTH_MAX:
Serial.println("MAX");
break;
}
Serial.println("Max allowed connections: " + String(conf.ap.max_connection));
Serial.println("Beacon interval: " + String(conf.ap.beacon_interval) + "ms");
}
void loop() {
yield();
}
The output would look alike this:
Stored STA configuration:
========================
SSID: NotYourBusiness
Password: WillNotTellYou
BSSID:
Channel: 0
WiFi scan method fast
WiFi scan results sorted by RSSI
Minimum RSSI accepted: 0
Minimum required auth method: OPEN
Stored AP configuration:
=========================
SSID: Slave:24:0A:C4:81:CE:9C
Password: 123456789
Channel: 1
SSID is broadcasted
Auth method: WPA2 PSK
Max allowed connections: 4
Beacon interval: 100ms
A modified version of this code is in Github Repo.