关于esp32s2利用nvs存储blob出现乱码的问题

steven_peng
Posts: 6
Joined: Wed Jun 02, 2021 8:45 am

关于esp32s2利用nvs存储blob出现乱码的问题

Postby steven_peng » Sat Sep 25, 2021 3:47 pm

<r>我想利用NVS存储WiFi列表,定义了结构体如下:<br/>
typedef struct _wifi_conf wifi_conf_struct; <br/>
struct _wifi_conf<br/>
{<br/>
char *ssid;<br/>
char *password;<br/>
};<br/>
<br/>
typedef struct _wifi_conf_node_t wifi_conf_node_t;<br/>
struct _wifi_conf_node_t<br/>
{<br/>
wifi_conf_struct item;<br/>
wifi_conf_node_t *p;<br/>
};<br/>
<br/>
typedef struct _wifi_conf_link_t wifi_conf_link_t;<br/>
struct _wifi_conf_link_t<br/>
{<br/>
wifi_conf_node_t *np;<br/>
int length;<br/>
};<br/>
NVS存储函数:<br/>
esp_err_t nvs_write_data_to_flash_in_main(wifi_conf_link_t *wifi_conf_list_link)<br/>
{<br/>
//ESP_ERROR_CHECK(nvs_flash_erase());<br/>
nvs_handle handle;<br/>
<br/>
size_t wifi_conf_size = 0;<br/>
int list_cont = wifi_conf_list_link->length;<br/>
struct _wifi_conf_node_t *tmp = wifi_conf_list_link->np;<br/>
struct _wifi_conf wifi_conf_list[list_cont];<br/>
int i = 0;<br/>
while (i < list_cont)<br/>
{<br/>
wifi_conf_list<I><s></s> = tmp->item;<br/>
wifi_conf_size = wifi_conf_size + sizeof(tmp->item);<br/>
tmp = tmp->p;<br/>
i++;<br/>
}<br/>
<br/>
<br/>
ESP_ERROR_CHECK(nvs_open(NVS_CUSTOMER, NVS_READWRITE, &handle));<br/>
<br/>
ESP_ERROR_CHECK(nvs_set_i32(handle, DATA_KEY_2, list_cont));<br/>
<br/>
ESP_ERROR_CHECK(nvs_set_blob(handle, DATA_KEY_1, wifi_conf_list, wifi_conf_size));<br/>
<br/>
ESP_ERROR_CHECK(nvs_commit(handle));<br/>
<br/>
// free(wifi_conf_list);<br/>
// free(tmp);<br/>
<br/>
printf("NVS_WRITE:存储wifi信息完毕\n");<br/>
nvs_close(handle);<br/>
<br/>
return ESP_OK;<br/>
}<br/>
NVS读取函数:<br/>
struct _wifi_conf_link_t nvs_read_data_from_flash_in_main(void)<br/>
{<br/>
<br/>
struct _wifi_conf_link_t wifi_conf_list_link_read = {NULL, 0};<br/>
<br/>
nvs_handle handle;<br/>
<br/>
int32_t value = 0;<br/>
<br/>
esp_err_t err = nvs_open(NVS_CUSTOMER, NVS_READWRITE, &handle);<br/>
if (err != ESP_OK)<br/>
{<br/>
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));<br/>
}<br/>
else<br/>
{<br/>
printf("Done\n");<br/>
<br/>
// Read<br/>
printf("Reading restart counter from NVS ... ");<br/>
<br/>
err = nvs_get_i32(handle, DATA_KEY_2, &value);<br/>
switch (err)<br/>
{<br/>
case ESP_OK:<br/>
printf("Done\n");<br/>
printf("Restart counter = %d\n", value);<br/>
break;<br/>
case ESP_ERR_NVS_NOT_FOUND:<br/>
printf("The value is not initialized yet!\n");<br/>
break;<br/>
default:<br/>
printf("Error (%s) reading!\n", esp_err_to_name(err));<br/>
}<br/>
}<br/>
if (value == 0)<br/>
{<br/>
return wifi_conf_list_link_read;<br/>
}<br/>
<br/>
struct _wifi_conf wifi_conf_list[value];<br/>
<br/>
size_t required_size = 0; // value will default to 0, if not set yet in NVS<br/>
nvs_get_blob(handle, DATA_KEY_1, NULL, &required_size);<br/>
<br/>
ESP_ERROR_CHECK(nvs_get_blob(handle, DATA_KEY_1, &wifi_conf_list, &required_size));<br/>
<br/>
nvs_close(handle);<br/>
<br/>
<br/>
<br/>
for (int i = 0; i < value; i++)<br/>
{<br/>
wifi_conf_link_add(&wifi_conf_list_link_read, wifi_conf_list<I><s></s>);<br/>
//printf("%s\r\n",wifi_conf_list<I><s></s>.ssid);<br/>
}<br/>
return wifi_conf_list_link_read;<br/>
}<br/>
<br/>
问题来了:<br/>
wifi_conf_struct tmp = {"yangyanghome24", "abcd1234"};//重点<br/>
<br/>
wifi_conf_link_del_all(&m_wifi_conf_link_main);<br/>
wifi_conf_link_add(&m_wifi_conf_link_main, tmp);<br/>
<br/>
nvs_write_data_to_flash(&m_wifi_conf_link_main);<br/>
重启后,NVS读取正常,但是:<br/>
const char* pwd_selected_const = lv_textarea_get_text(textarea_pswd);<br/>
int nLen = strlen(pwd_selected_const);<br/>
char* pCh = (char*)malloc(sizeof(pwd_selected_const));<br/>
memcpy(pCh, pwd_selected_const, nLen + 1);<br/>
wifi_conf_struct scan_seleced_wifi_conf = {"splite_ssid_",pCh};//重点<br/>
重启后,读取出现乱码!!求帮助,困扰好长时间了。</I></I></I></r>

Who is online

Users browsing this forum: Baidu [Spider], WKJay_ and 50 guests