Code: Select all
[Codebox=c file=Untitled.c]void set_nvs_string(char* data, char* key)
{
nvs_handle my_handle;
esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
}
err = nvs_set_str(my_handle, key, data);
printf((err != ESP_OK) ? "Failed! Setting String from NVS\n" : "Done Setting String from NVS\n");
printf("Committing updates in NVS ... ");
err = nvs_commit(my_handle);
printf((err != ESP_OK) ? "Failed! Commiting\n" : "Done Commiting\n");
}
esp_err_t get_nvs_string(char* key, char* data)
{
nvs_handle my_handle;
esp_err_t err = nvs_open("storage", NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
}
size_t size = strlen(data);
err = nvs_get_str(my_handle, key, data, &size);
switch (err)
{
case ESP_OK:
printf("Done Getting String from NVS\n");
break;
case ESP_ERR_NVS_NOT_FOUND:
printf("The value is not initialized yet!\n");
break;
default :
printf("Error (%s) reading!\n", esp_err_to_name(err));
}
return err;
}
ESP_LOGI(TAG_ap, "SSID Local: %s PASSWORD Local: %s Inverter ID %s", ssidLocal, passwordLocal, InverterId);
set_nvs_string(InverterId, "InverterId");
if (strlen(ssidLocal) != 0 && strlen(passwordLocal) != 0)
{
set_nvs_string(ssidLocal, "ssid");
set_nvs_string(passwordLocal, "password");
wifi_config_t wifi_config;
char* tempSsid = malloc(sizeof(char) * 32);
char* tempPassword = malloc(sizeof(char) * 64);
get_nvs_string("ssid", tempSsid);
get_nvs_string("password", tempPassword);
strcpy((char*)wifi_config.sta.ssid, tempSsid);
strcpy((char*)wifi_config.sta.password, tempPassword);
ESP_LOGI("WIFI CHECK4", "Set SSID: %s", (char*)wifi_config.sta.ssid);
ESP_LOGI("WIFI CHECK4", "Set Password: %s", (char*)wifi_config.sta.password);
free(tempSsid);
free(tempPassword);
ESP_LOGI(TAG_ap, "=========== PARSED DATA ==========");
ESP_LOGI(TAG_ap, "SSID: %s PASSWORD: %s", wifi_config.sta.ssid, wifi_config.sta.password);
ESP_LOGI(TAG_ap, "====================================");
ESP_ERROR_CHECK(esp_wifi_stop());
wifi_init_sta();
s_retry_num = 0;
}[/Codebox]
Output:
- I (146517) Hojaa Pls: SSID Local: ZongAltium1 PASSWORD Local: Xavor123 Inverter ID Schneider
- Done Setting String from NVS
- Committing updates in NVS ... Done Commiting
- Done Setting String from NVS
- Committing updates in NVS ... Done Commiting
- Done Setting String from NVS
- Committing updates in NVS ... Done Commiting
- Done Getting String from NVS
- Error (ESP_ERR_NVS_INVALID_LENGTH) reading!
- I (146557) wifi: station: 9e:62:e3:2e:66:f5 leave, AID = 1, bss_flags is 134243
- I (146567) WIFI CHECK4: Set SSID: ZongAltium1
- I (146567) wifi: n:1 0, o:1 0, ap:1 1, sta:0 0, prof:1
- I (146567) WIFI CHECK4: Set Password: ???
- I (146577) Hojaa Pls: =========== PARSED DATA ==========
- I (146587) Hojaa Pls: SSID: ZongAltium1 PASSWORD: ???
- I (146587) Hojaa Pls: ====================================
- I (146637) wifi: flush txq
- I (146637) wifi: stop sw txq
- I (146637) wifi: lmac stop hw txq
- Error (ESP_ERR_NVS_INVALID_LENGTH) reading!