Skip to content

Instantly share code, notes, and snippets.

@BrianPugh
Created December 2, 2018 23:24
Show Gist options
  • Save BrianPugh/6f41f2139efb0f8da689d64d90868336 to your computer and use it in GitHub Desktop.
Save BrianPugh/6f41f2139efb0f8da689d64d90868336 to your computer and use it in GitHub Desktop.
nvs issue
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "esp_log.h"
const char TAG[] = "NVS_erase_demo";
static esp_err_t init_nvs_namespace(nvs_handle *nvs_h, const char *namespace) {
// Initialize NVS
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
// NVS partition was truncated and needs to be erased
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
// Open
err = nvs_open(namespace, NVS_READWRITE, nvs_h);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error (%d) opening NVS handle with namespace %s!", err, namespace);
esp_restart();
return ESP_FAIL;
} else {
ESP_LOGI(TAG, "Successfully opened NVS with namespace %s!", namespace);
return ESP_OK;
}
}
static bool storage_set_blob(unsigned char *buf, size_t len,
char *namespace, char *key) {
nvs_handle nvs;
esp_err_t err;
init_nvs_namespace(&nvs, namespace);
err = nvs_set_blob(nvs, key, buf, len);
nvs_commit(nvs);
nvs_close(nvs);
return ESP_OK==err;
}
static bool storage_get_blob(unsigned char *buf, size_t *required_size,
char *namespace, char *key) {
nvs_handle nvs;
init_nvs_namespace(&nvs, namespace);
esp_err_t err;
err = nvs_get_blob(nvs, key, buf, required_size);
nvs_close(nvs);
return ESP_OK==err;
}
void app_main() {
/* Attempt to read value. Using Blob instead of Str because in my
* actual application I'm using binary values. */
{
char data_read[50] = { 0 };
size_t required_size;
if( !storage_get_blob(NULL, &required_size, "test", "data") ) {
printf("Stored App Key not found\n");
}
else if( !storage_get_blob( (uint8_t*)data_read, &required_size, "test", "data") ) {
printf("Stored data found, error reading to buf.\n");
}
else {
printf("Successfully read %d bytes: %s\n", required_size, data_read);
}
}
/* Delay so we don't loop like crazy */
{
const size_t delay_amount_seconds = 5;
printf("Delaying for %d seconds.\n", delay_amount_seconds);
vTaskDelay(delay_amount_seconds * 1000 / portTICK_PERIOD_MS);
}
/* Delete all key/value pairs */
/* toggle this for experimental purposes */
#if true
{
printf("Deleting all key/value pairs in NVS.\n");
ESP_ERROR_CHECK(nvs_flash_erase());
printf("Successfully deleted all key/value pairs in NVS.\n");
}
#endif
/* Store Value */
{
const char data_write[] = "Testing NVS Blob";
printf("Attempting to store \"%s\" to \"test/data\"\n", data_write);
if( !storage_set_blob( (uint8_t*)data_write, sizeof(data_write), "test", "data") ) {
printf("Error setting app_key.\n");
}
printf("Successfully set data.\n");
}
/* Reset Device */
{
printf("Reseting Device...\n");
esp_restart();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment