How to detect the validness of WiFi STA configuration stored in NVS?
Posted: 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.
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
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);
}
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