Page 1 of 1

Checking Wifi NVS data

Posted: Fri Dec 25, 2020 8:23 am
by anudocs
Here is my case :

Wifi NVS FLash is enabled. I am using esp_wifi_get_config to find out whether wifi configuration is saved in NVS. My code is :

Code: Select all

wifi_config_t conf;
 ret = esp_wifi_get_config(ESP_IF_WIFI_STA, &conf);
if (ret == ESP_OK)
    {

  ESP_LOGI(TAG, "Wifi configuration already stored in flash partition called NVS");
ESP_LOGI(TAG, "%s" ,conf.sta.ssid);
ESP_LOGI(TAG, "%s" ,conf.sta.password);
    }
else
    {

ESP_LOGI(TAG, "Wifi configuration not found in flash partition called NVS.");    
    configure_wifi();
    }
It worked as expected. Then I erased and then initialized my nvs using nvs_flash_erase(); nvs_flash_init(); before the above written code. Now my output is

Code: Select all

I (697) Wifi station: Wifi configuration already stored in flash partition called NVS

I (707) Wifi station:

I (707) Wifi station:
Expected output is:

Code: Select all

Wifi configuration not found in flash partition called NVS
Is my approach right? If not, then what is the correct way to check Wifi nvs data before configuring wifi?

Re: Checking Wifi NVS data

Posted: Sat Nov 27, 2021 11:07 pm
by Fromen
This reply may be almost a year late, but I just happened to have this same question and figured others may benefit from an answer.

Your solution is looking at the error returned by esp_wifi_get_config(), however this will return ESP_OK even when the NVS has been erased (as indicated by your code not working as you expected). In fact, if you look at the function description in esp_wifi.h, an error will only be returned if the WiFi has not been initialized, or you gave a bad parameter:

Code: Select all

/**
  * @brief     Get configuration of specified interface
  *
  * @param     interface  interface
  * @param[out]  conf  station or soft-AP configuration
  *
  * @return
  *    - ESP_OK: succeed
  *    - ESP_ERR_WIFI_NOT_INIT: WiFi is not initialized by esp_wifi_init
  *    - ESP_ERR_INVALID_ARG: invalid argument
  *    - ESP_ERR_WIFI_IF: invalid interface
  */
My solution is to look at the length of the SSID with

Code: Select all

size_t ssidLen = strlen((char*)conf.sta.ssid);
If it has a length of 0, then you know nothing is stored there. There may be another better solution, but this has worked well in my testing.

Re: Checking Wifi NVS data

Posted: Wed Aug 21, 2024 6:42 pm
by ghawkins
One could also check for WIFI_INIT_CONFIG_MAGIC - that seems to be its purpose:

Code: Select all

if (cfg.magic == WIFI_INIT_CONFIG_MAGIC) {
    // cfg is valid.
}

Re: Checking Wifi NVS data

Posted: Mon Aug 26, 2024 12:50 pm
by AndreaS73
One could also check for WIFI_INIT_CONFIG_MAGIC - that seems to be its purpose
Where did you find the magic member for struct wifi_config_t?
I cannot find it in the latest stable (5.3):

Code: Select all

typedef union {
    wifi_ap_config_t  ap;  /**< configuration of AP */
    wifi_sta_config_t sta; /**< configuration of STA */
    wifi_nan_config_t nan; /**< configuration of NAN */
} wifi_config_t;