How to detect the validness of WiFi STA configuration stored in NVS?

huming2207
Posts: 9
Joined: Wed Dec 12, 2018 4:59 am

How to detect the validness of WiFi STA configuration stored in NVS?

Postby huming2207 » Sat Mar 14, 2020 12:53 am

Hi all,

I need to write a function for detecting if there is a valid WiFi STA configuration has been saved to NVS previously. I'm now using esp_wifi_get_config() to achieve that. However, esp_wifi_get_config() will always return ESP_OK if WiFi and NVS are correctly initialised. So I need to detect the output from esp_wifi_get_config() as well. Here's my current solution:

1. If esp_wifi_get_config() returns a non-ESP_OK status, then false; or otherwise true;
2. If SSID is empty, then return false;
3. If BSSID is invalid, then return false.

Code: Select all

bool wifi_helper::has_sta_configured()
{
    uint8_t empty_bssid[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    wifi_config_t config = {};
    if (esp_wifi_get_config(ESP_IF_WIFI_STA, &config) != ESP_OK) return false;
    ESP_LOGI(TAG, "SSID: %s, BSSID: " MACSTR, config.sta.ssid, MAC2STR(config.sta.bssid));
    return (std::strlen((const char *)config.sta.ssid) >= 1)
        || (std::memcmp(empty_bssid, config.sta.bssid, sizeof(empty_bssid)) != 0);
}
However, I think SSID can be empty (e.g. connect to a hidden WiFi network). So simply detecting the SSID may not be a good idea.

As a result, I would like to know that is there a proper way to do so, or how should I improve the current implmenetation? Thanks in advance!

Regards,
Jackson

Who is online

Users browsing this forum: Majestic-12 [Bot] and 184 guests