NVS (Non-Volatile Storage)
Persistent key-value storage for WiFi credentials, device config, and calibration data.
Key idea:
NVS (Non-Volatile Storage) is ESP-IDF's key-value store in flash. It handles wear leveling, encryption, and namespaces automatically.
Basic NVS Operations
#include "nvs_flash.h"
#include "nvs.h"
#include "esp_log.h"
static const char *TAG = "nvs_demo";
void app_main(void) {
// Initialize NVS
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);
// Open namespace
nvs_handle_t handle;
err = nvs_open("storage", NVS_READWRITE, &handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error opening NVS: %s", esp_err_to_name(err));
return;
}
// Write values
err = nvs_set_i32(handle, "boot_count", 42);
err |= nvs_set_str(handle, "device_id", "ESP32-001");
err |= nvs_set_blob(handle, "calibration", cal_data, sizeof(cal_data));
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error writing: %s", esp_err_to_name(err));
}
// Commit changes (required!)
err = nvs_commit(handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error committing: %s", esp_err_to_name(err));
}
// Read values
int32_t boot_count = 0;
err = nvs_get_i32(handle, "boot_count", &boot_count);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Boot count: %ld", boot_count);
}
char device_id[32];
size_t len = sizeof(device_id);
err = nvs_get_str(handle, "device_id", device_id, &len);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Device ID: %s", device_id);
}
nvs_close(handle);
}Namespaces for Organization
// Separate namespaces for different purposes
nvs_handle_t wifi_handle, config_handle, calib_handle;
nvs_open("wifi", NVS_READWRITE, &wifi_handle);
nvs_open("config", NVS_READWRITE, &config_handle);
nvs_open("calib", NVS_READONLY, &calib_handle); // Read-only for calibrationData Types Supported
| Function | Type |
|---|---|
nvs_set_u8 / nvs_get_u8 | uint8_t |
nvs_set_i8 / nvs_get_i8 | int8_t |
nvs_set_u16 / nvs_get_u16 | uint16_t |
nvs_set_i16 / nvs_get_i16 | int16_t |
nvs_set_u32 / nvs_get_u32 | uint32_t |
nvs_set_i32 / nvs_get_i32 | int32_t |
nvs_set_u64 / nvs_get_u64 | uint64_t |
nvs_set_i64 / nvs_get_i64 | int64_t |
nvs_set_str / nvs_get_str | String (null-terminated) |
nvs_set_blob / nvs_get_blob | Binary data |
Storing WiFi Credentials
typedef struct {
char ssid[32];
char password[64];
} wifi_creds_t;
void save_wifi_creds(const char *ssid, const char *password) {
nvs_handle_t handle;
nvs_open("wifi", NVS_READWRITE, &handle);
wifi_creds_t creds = {0};
strncpy(creds.ssid, ssid, sizeof(creds.ssid) - 1);
strncpy(creds.password, password, sizeof(creds.password) - 1);
nvs_set_blob(handle, "creds", &creds, sizeof(creds));
nvs_commit(handle);
nvs_close(handle);
}
bool load_wifi_creds(wifi_creds_t *creds) {
nvs_handle_t handle;
esp_err_t err = nvs_open("wifi", NVS_READONLY, &handle);
if (err != ESP_OK) return false;
size_t len = sizeof(wifi_creds_t);
err = nvs_get_blob(handle, "creds", creds, &len);
nvs_close(handle);
return err == ESP_OK;
}Encryption for Sensitive Data
// Enable NVS encryption in menuconfig:
// Component config → NVS → Enable NVS encryption
// Generate encryption key (run once)
// nvs_flash_generate_keys partition
// Or use nvs_sec_provider for custom key managementErase and Error Handling
// Erase all data in namespace
nvs_erase_key(handle, "key_name");
nvs_erase_all(handle); // Erase entire namespace
// Check if key exists
esp_err_t err = nvs_get_i32(handle, "key", &value);
if (err == ESP_ERR_NVS_NOT_FOUND) {
// Key doesn't exist, set default
nvs_set_i32(handle, "key", DEFAULT_VALUE);
nvs_commit(handle);
}Partition Table
# partitions.csv
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x6000,
nvs_keys, data, keys, 0xF000, 0x1000, # For encryption keysKey Takeaways
nvs_open(),nvs_set/get_*()functions for key-value storage- Namespaces for organizing keys (wifi, config, calib)
- Commit required after writes:
nvs_commit() - Encryption available for sensitive data (WiFi passwords, keys)
- Handles wear leveling automatically
Resources
- NVS API Reference: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/storage/nvs_flash.html
- NVS Encryption: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/storage/nvs_flash.html#nvs-encryption
- ESP-IDF Partition Tables: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/partition-tables.html