程序示例:
/*ESP32 startup counter example with Preferences library.This simple example demonstrates using the Preferences library to store how many times the ESP32 module has booted. The Preferences library is a wrapper around the Non-volatile storage on ESP32 processor.created for arduino-esp32 09 Feb 2017 by Martin Sloup (Arcao)Complete project details at https://RandomNerdTutorials.com/esp32-save-data-permanently-preferences/
*/#include < Preferences.h >Preferences preferences;void setup() {Serial.begin(115200);Serial.println();// Open Preferences with my-app namespace. Each application module, library, etc// has to use a namespace name to prevent key name collisions. We will open storage in// RW-mode (second parameter has to be false).// Note: Namespace name is limited to 15 chars.preferences.begin("my-app", false);// Remove all preferences under the opened namespace//preferences.clear();// Or remove the counter key only//preferences.remove("counter");// Get the counter value, if the key does not exist, return a default value of 0// Note: Key name is limited to 15 chars.unsigned int counter = preferences.getUInt("counter", 0);// Increase counter by 1counter++;// Print the counter to Serial MonitorSerial.printf("Current counter value: %un", counter);// Store the counter to the Preferencespreferences.putUInt("counter", counter);// Close the Preferencespreferences.end();// Wait 10 secondsSerial.println("Restarting in 10 seconds...");delay(10000);// Restart ESPESP.restart();
}void loop() {}
其中几个函数的intro如下:
#include < nvs_flash.h >void setup() {nvs_flash_erase(); // 擦除NVS分区nvs_flash_init(); // 初始化NVS分区while(true);
}void loop() {}
放置一个k-v
获取一个k-v