今天我們來說說ESP32 for Arduino NVS分區永久保存數據。
ESP32 for Arduino NVS分區
上一節我們講了整個ESP32的存儲分布,其中有一個NVS分區,這個分區專門用來存儲數據的,系統在復位或斷電后數據仍然存在,我們可以使用Preferences庫保存網絡SSID,密碼,一些閾值或者IO的最后狀態等。
在保存數據的時候,我們推薦使用Preferences庫,不推薦使用EEPROM庫。
使用Preferences庫保存的數據結構如下,也叫鍵值對:
namespace {
key:value
}
一個命名空間中也可以有不同的鍵:
namespace {
key1: value1
key2: value2
}
實際使用中,我們可以用來保存網絡憑證:
credentials {
ssid: "your_ssid"
pass: "your_pass"
}
也可以有多個具有相同鍵的命名空間(但每個鍵都有其值):
namespace1{
key:value1
}
namespace2{
key:value2
}
使用Preferences庫時,應該定義要保存的數據類型。如果想讀取該數據,則必須知道保存的數據類型,也就是說,寫入和讀取的數據類型應該相同。
支持以下數據類型的保存:char、char、short、Ushort、int、Uint、long、Ulong、long64、Ulong64、float、double、bool、字符串和字節。
Preferences庫函數說明
首先包含頭文件
Preferences 庫
然后定義一個實例
Preferences preferences;
打開一個命名空間
begin方法打開一個帶有定義命名空間的“儲存空間”,參數為false代表我們在讀/寫模式下使用,為true代表以只讀的方式打開或創建命令空間,命名空間名稱最多為15個字符。
preferences.begin("my-app", false);
清除preferences
從打開的命名空間中刪除一個鍵。
preferences.remove(key);
關閉preferences
使用end方法在打開的命名空間下關閉preferences
preferences.end();
放置一個k-v
獲取一個k-v
刪除命名空間
在Preferences 庫中,并沒有完全刪除命令空間的方法,我們存儲很多數據之后,nvs分區可能就滿了,所以我們想要完全擦除nvs分區,可以使用以下程序運行一次:
#include < nvs_flash.h >
void setup() {
nvs_flash_erase(); // 擦除NVS分區
nvs_flash_init(); // 初始化NVS分區
while(true);
}
void loop() {
}
程序示例
我們直接打開Example中的例子,StartCounter
/*
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 1
counter++;
// Print the counter to Serial Monitor
Serial.printf("Current counter value: %un", counter);
// Store the counter to the Preferences
preferences.putUInt("counter", counter);
// Close the Preferences
preferences.end();
// Wait 10 seconds
Serial.println("Restarting in 10 seconds...");
delay(10000);
// Restart ESP
ESP.restart();
}
void loop() {
}
這個例子增加了一個counter鍵,每次運行都加一,我們在按下復位鍵之后,可以看到下面你的現象,數據保存起來了。
Preferences庫很方便保存鍵:值對。即使在重置 ESP32 或斷電后,閃存中保存的數據仍然存在。
感謝大家,關于ESP32的學習,希望大家Enjoy!
-
存儲
+關注
關注
13文章
4510瀏覽量
87164 -
網絡
+關注
關注
14文章
7783瀏覽量
90529 -
EEPROM
+關注
關注
9文章
1084瀏覽量
83423 -
數據結構
+關注
關注
3文章
573瀏覽量
40643 -
ESP32
+關注
關注
21文章
1012瀏覽量
18970
發布評論請先 登錄
請問esp32s3如何保存突然停電時的數據?
ESP32-S3無法使用NVS分區是怎么回事?
基于PlatfromIO-Arduino的ESP32-Flash分區
使用ESP32-S3無法使用NVS分區是為什么?
如何將ESP-IDF引導加載程序與用于NVS的ESP32-Arduino代碼一起使用?
ESP32驅動AD7705

ESP32-Flash分區,基于PlatfromIO-Arduino

[ESP8266學習筆記]components_nvs 非易失性存儲 Non-Volatile Storage(NVS),保存數據到flash
![[<b class='flag-5'>ESP</b>8266<b class='flag-5'>學習</b><b class='flag-5'>筆記</b>]components_<b class='flag-5'>nvs</b> 非易失性存儲 Non-Volatile Storage(<b class='flag-5'>NVS</b>),<b class='flag-5'>保存</b><b class='flag-5'>數據</b>到flash](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
[ESP32]學習筆記02
![[<b class='flag-5'>ESP32</b>]<b class='flag-5'>學習</b><b class='flag-5'>筆記</b>02](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
[ESP32]學習筆記04
![[<b class='flag-5'>ESP32</b>]<b class='flag-5'>學習</b><b class='flag-5'>筆記</b>04](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
SPI主線協議——ESP32學習筆記

評論