Runtime change of WiFi network not persisted on reboot

someuser
Posts: 2
Joined: Sun Apr 23, 2023 7:04 pm

Runtime change of WiFi network not persisted on reboot

Postby someuser » Mon Apr 24, 2023 1:05 pm

For my project I'm trying to change the WiFi network my ESP32's are connected to based on a command the device receives. What I'm seeing is that the new SSID and password are used to connect in the correct way but on device reboot, the settings are lost.

I've tried both using the purely esp-idf as well as Arduino component code but in both cases it's not persisting the config. Does anyone know why that's the case?

Arduino-component based approach;
[Codebox]
// ssid/password processing removed for brevity
WiFi.disconnect();
WiFi.persistent(true);
WiFi.begin(ssid, password);
[/Codebox]

And the ESP-IDF variant:
[Codebox]
const char *remainder_ptr = param.c_str();
const char *delimiter_ptr = strchr(remainder_ptr, '+');

wifi_config_t wifi_cfg;
wifi_cfg.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
wifi_cfg.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
wifi_cfg.sta.channel = 0;
wifi_cfg.sta.threshold.rssi = -75;
wifi_cfg.sta.pmf_cfg.capable = true;

// set SSID in wifi config, terminating string with null
size_t ssid_length = delimiter_ptr - remainder_ptr;
strncpy((char *)wifi_cfg.sta.ssid, remainder_ptr, ssid_length);
wifi_cfg.sta.ssid[ssid_length] = '\0';

// set pw in wifi config, terminating string with null
size_t pw_length = strlen(remainder_ptr) - ssid_length - 1;
strncpy((char *) wifi_cfg.sta.password, delimiter_ptr + 1, pw_length);
wifi_cfg.sta.password[pw_length] = '\0';

ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg));
ESP_ERROR_CHECK(esp_wifi_connect());
[/Codebox]

In both cases the device returns to its original credentials after boot, which suggests that both erasing nvs and writing the new values failed. Is there an extra step involved that I'm missing?

someuser
Posts: 2
Joined: Sun Apr 23, 2023 7:04 pm

Re: Runtime change of WiFi network not persisted on reboot

Postby someuser » Wed Jun 21, 2023 7:51 am

Bringing this topic up again, I've not been able to find a solution to this, besides retrieving the wifi ssid and password from my own nvs storage. I feel like this should be possible though - anyone with an idea how to do that?

Who is online

Users browsing this forum: No registered users and 88 guests