How to retrieve WiFi provision ssid and password stored by provisioning manager in nvs flash

karunt
Posts: 93
Joined: Sat Apr 03, 2021 7:58 am

How to retrieve WiFi provision ssid and password stored by provisioning manager in nvs flash

Postby karunt » Sun Dec 17, 2023 1:49 am

I'm trying to figure out if there's a way to retrieve the key and value of the wifi ssid that an app feeds to ESP32 using the wifi provisioning example (using NimBLE protocol). This is what I've tried so far, but get a whole load of gibberish in return:

Step 1 - Find the name of the namespace in partition "nvs"

Code: Select all

    nvs_iterator_t it = NULL;
    esp_err_t err = nvs_entry_find("nvs", NULL, NVS_TYPE_ANY, &it);
    while(err == ESP_OK) {
        nvs_entry_info_t info;
        nvs_entry_info(it, &info); // Can omit error check if parameters are guaranteed to be non-NULL
        printf("key '%s', type '%d', namespace '%s' \n", info.key, info.type, info.namespace_name);
        err = nvs_entry_next(&it);
    }
    nvs_release_iterator(it);
Output:
  • key 'ap.sndchan', type '1', namespace 'nvs.net80211'
    key 'opmode', type '1', namespace 'nvs.net80211'
    key 'cal_data', type '66', namespace 'phy'
    key 'cal_mac', type '66', namespace 'phy'
    key 'cal_version', type '4', namespace 'phy'
    key 'sta.ssid', type '66', namespace 'nvs.net80211'
    key 'sta.pswd', type '66', namespace 'nvs.net80211'
    key 'bssid.set', type '1', namespace 'nvs.net80211'
    key 'sta.lis_intval', type '2', namespace 'nvs.net80211'
    key 'sta.scan_method', type '1', namespace 'nvs.net80211'
    key 'sta.sort_method', type '1', namespace 'nvs.net80211'
    key 'sta.minrssi', type '17', namespace 'nvs.net80211'
    key 'sta.minauth', type '1', namespace 'nvs.net80211'
    key 'sta.apsw', type '66', namespace 'nvs.net80211'
    key 'sta.pmf_e', type '1', namespace 'nvs.net80211'
    key 'sta.pmf_r', type '1', namespace 'nvs.net80211'
    key 'sta.rrm_e', type '1', namespace 'nvs.net80211'
    key 'sta.btm_e', type '1', namespace 'nvs.net80211'
    key 'sta.mbo_e', type '1', namespace 'nvs.net80211'
    key 'sta.ft', type '1', namespace 'nvs.net80211'
    key 'sta.owe', type '1', namespace 'nvs.net80211'
    key 'sta.bss_retry', type '1', namespace 'nvs.net80211'
    key 'sta.trans_d', type '1', namespace 'nvs.net80211'
    key 'sta.sae_h2e', type '1', namespace 'nvs.net80211'
    key 'sta.sae_pk_mode', type '1', namespace 'nvs.net80211'
    key 'sta.sae_h2e_id', type '66', namespace 'nvs.net80211'
    key 'sta.chan', type '1', namespace 'nvs.net80211'
    key 'sta.apinfo', type '66', namespace 'nvs.net80211'
Step 2:
Key "sta.ssid" is associated with namespace name "nvs.net80211". So I used this in function nvs_open:
nvs_handle_t my_handle;
err = nvs_open("nvs.net80211", NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
} else {
printf("Done\n");

Step 3:
Try to get the value associated with key "sta.ssid" (type 66, which is associated with NVS_TYPE_BLOB). So I use the nvs_get_blob function:

Code: Select all

printf("Opening Non-Volatile Storage (NVS) handle... ");
    nvs_handle_t my_handle;
    err = nvs_open("nvs.net80211", NVS_READWRITE, &my_handle);
    if (err!= ESP_OK) {
            printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
        } else {
            printf("Done\n");
        }

    size_t size_ssid;
    nvs_get_blob(my_handle, "sta.apinfo", NULL, &size_ssid);
    char* ssid = malloc(size_ssid);
    nvs_get_blob(my_handle, "sta.ssid", ssid, &size_ssid);
    printf("Value of ssid: %s\n", ssid);
Output from this step is:
Opening Non-Volatile Storage (NVS) handle... Done
Value of ssid:
Now if i run similar code to get the value of sta.pswd, I get the right value. Why is it that sta.ssid gives me a blank result when I'm able to connect to the ssid successfully after a successful wifi provisioning process?

karunt
Posts: 93
Joined: Sat Apr 03, 2021 7:58 am

Re: How to retrieve WiFi provision ssid and password stored by provisioning manager in nvs flash

Postby karunt » Sat Dec 23, 2023 12:06 pm

7000+ views and still no attention from anyone in the Espressif community. Really sad - how do you expect people to learn your platform and build things with ESP chips if nobody within your organization is available to help when people get stuck?

After an entire week of looking around, I finally found a way to get this information. Here it goes:

Code: Select all

        esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg);
        //(char *) wifi_cfg.sta.ssid and (char *) wifi_cfg.sta.password will provide values of already store wifi ssid and password credentials
        ESP_LOGI(TAG, "WIFI SSID: %s", (char *) wifi_cfg.sta.ssid);
        ESP_LOGI(TAG, "WIFI Password: %s", (char *) wifi_cfg.sta.password);

imdahisaria
Posts: 6
Joined: Thu Sep 29, 2016 7:40 am

Re: How to retrieve WiFi provision ssid and password stored by provisioning manager in nvs flash

Postby imdahisaria » Mon Jul 01, 2024 9:35 am

@karunt
I tried your solution but for me when I use esp_wifi_get_config api
both password and ssid are empty

I am simply calling the esp_wifi_get_config(ESP_IF_WIFI_STA,&wifi_cfg);

Do i need to send or use any other wifi api before this ?
Any help would be appreciated.

I am using IDF v 5.0.1

karunt
Posts: 93
Joined: Sat Apr 03, 2021 7:58 am

Re: How to retrieve WiFi provision ssid and password stored by provisioning manager in nvs flash

Postby karunt » Tue Jul 23, 2024 9:39 am

@imdahisaria Did you populate wifi_cfg first with the SSID and password? In my example, I've already sent over wifi credentials (SSID and password) from an app to the ESP32 chip from an app using the NimBLE protocol. My solution below pertains to retrieving the wifi SSID and password at a later point in time. Email me at karunt@yahoo.com if you still have more questions and use "HELP FOR ESP32 DEVELOPMENT" in your Subject so I don't accidentally delete it.

Who is online

Users browsing this forum: No registered users and 137 guests