我需要像 arduino EEPROM 一样使用 NVS。
我有很多字符串(MQTT 配置)。
我根本不能写一些值。
请帮我。
代码:
#define nvsPar
tition "storage"
#define nvsMaxLen_String 100
#define nvsKey_MQTT_HOST "mqttHOST"
#define nvsKey_MQTT_PORT "mqttPORT"
#define nvsKey_MQTT_USER "mqttUSER"
#define nvsKey_MQTT_PASS "mqttPASS"
#define nvsKey_MQTT_Topic_PUB "mqttTopicPub"
#define nvsKey_MQTT_Topic_SUB "mqttTopicSub"
#define nvsKey_Device_SerialNumber "deviceSN"
#define nvsKey_MQTT_INTERVAL "mqttInterval"
代码:
// write function
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
nvs_handle_t my_handle;
err = nvs_open(nvsPartition, NVS_READWRITE, &my_handle);
char nvsChar[nvsMaxLen_String];
memset(nvsChar, 0x00, nvsMaxLen_String);
if (err != ESP_OK)
{
printf("Error (%s) opening NVS handle!n", esp_err_to_name(err));
}
else
{
nvs_set_str(my_handle, nvsKey_MQTT_HOST, c->mqttHOST);
nvs_set_i16(my_handle, nvsKey_MQTT_PORT, c->mqttPORT);
nvs_set_str(my_handle, nvsKey_MQTT_USER, c->mqttUSER);
nvs_set_str(my_handle, nvsKey_MQTT_PASS, c->mqttPASS);
nvs_set_str(my_handle, nvsKey_MQTT_Topic_PUB, c->mqttTopPub);
nvs_set_str(my_handle, nvsKey_MQTT_Topic_SUB, c->mqttTopSub);
nvs_set_str(my_handle, nvsKey_Device_SerialNumber, c->deviceSN);
nvs_set_i16(my_handle, nvsKey_MQTT_INTERVAL, c->mqttInterval);
nvs_commit(my_handle);
printf("Commit passrn");
nvs_close(my_handle);
}
代码:
// read function
void readNVS()
{
printf("----------readNVS----------rn");
nvs_handle_t my_handle;
esp_err_t err = nvs_open(nvsPartition, NVS_READWRITE, &my_handle);
char nvsChar[nvsMaxLen_String];
memset(nvsChar, 0x00, nvsMaxLen_String);
size_t buf_len = sizeof(nvsChar);
if (nvs_get_str(my_handle, nvsKey_MQTT_HOST, nvsChar, &buf_len) != ESP_OK)
{
printf("nvs_get_str errorrn");
}
else
{
printf("MQTT_HOST=|%s|rn", nvsChar);
memset(nvsChar, 0x00, nvsMaxLen_String);
}
int16_t mqttPort = 0;
if (nvs_get_i16(my_handle, nvsKey_MQTT_PORT, &mqttPort) != ESP_OK)
{
printf("nvs_get_str errorrn");
}
else
{
printf("MQTT_PORT=|%d|rn", mqttPort);
}
if (nvs_get_str(my_handle, nvsKey_MQTT_USER, nvsChar, &buf_len) != ESP_OK)
{
printf("nvs_get_str errorrn");
}
else
{
printf("MQTT_USER=|%s|rn", nvsChar);
memset(nvsChar, 0x00, nvsMaxLen_String);
}
if (nvs_get_str(my_handle, nvsKey_MQTT_PASS, nvsChar, &buf_len) != ESP_OK)
{
printf("nvs_get_str errorrn");
}
else
{
printf("MQTT_PASS=|%s|rn", nvsChar);
memset(nvsChar, 0x00, nvsMaxLen_String);
}
if (nvs_get_str(my_handle, nvsKey_MQTT_Topic_PUB, nvsChar, &buf_len) != ESP_OK)
{
printf("nvs_get_str errorrn");
}
else
{
printf("MQTT_TOP_PUB=|%s|rn", nvsChar);
memset(nvsChar, 0x00, nvsMaxLen_String);
}
if (nvs_get_str(my_handle, nvsKey_MQTT_Topic_SUB, nvsChar, &buf_len) != ESP_OK)
{
printf("nvs_get_str errorrn");
}
else
{
printf("MQTT_TOP_SUB=|%s|rn", nvsChar);
memset(nvsChar, 0x00, nvsMaxLen_String);
}
int16_t mqttInterval = 0;
if (nvs_get_i16(my_handle, nvsKey_MQTT_PORT, &mqttInterval) != ESP_OK)
{
printf("nvs_get_str errorrn");
}
else
{
printf("MQTT_INTERVAL=|%d|rn", mqttInterval);
}
if (nvs_get_str(my_handle, nvsKey_Device_SerialNumber, nvsChar, &buf_len) != ESP_OK)
{
printf("nvs_get_str errorrn");
}
else
{
printf("deviceSN=|%s|rn", nvsChar);
memset(nvsChar, 0x00, nvsMaxLen_String);
}
nvs_close(my_handle);
}
在我将设置值发送到 nvs 之后。这是结果。
代码:
----------readNVS----------
MQTT_HOST=|
MQTT_PORT=|1883|
MQTT_USER=|txst|
nvs_get_str error
nvs_get_str error
nvs_get_str error
MQTT_INTERVAL=|1883|
deviceSN=|SNxxxxx|
middle 的 3 值未设置为 nvs 并且间隔不正确(我发送间隔 15sec)。
在正确写入 nvs 之前,我确实检查了结构。
请帮我。