今天我們來說說ESP32 for Arduino NVS分區(qū)永久保存數(shù)據(jù)。
ESP32 for Arduino NVS分區(qū)
上一節(jié)我們講了整個(gè)ESP32的存儲分布,其中有一個(gè)NVS分區(qū),這個(gè)分區(qū)專門用來存儲數(shù)據(jù)的,系統(tǒng)在復(fù)位或斷電后數(shù)據(jù)仍然存在,我們可以使用Preferences庫保存網(wǎng)絡(luò)SSID,密碼,一些閾值或者IO的最后狀態(tài)等。
在保存數(shù)據(jù)的時(shí)候,我們推薦使用Preferences庫,不推薦使用EEPROM庫。
使用Preferences庫保存的數(shù)據(jù)結(jié)構(gòu)如下,也叫鍵值對:
namespace {
key:value
}
一個(gè)命名空間中也可以有不同的鍵:
namespace {
key1: value1
key2: value2
}
實(shí)際使用中,我們可以用來保存網(wǎng)絡(luò)憑證:
credentials {
ssid: "your_ssid"
pass: "your_pass"
}
也可以有多個(gè)具有相同鍵的命名空間(但每個(gè)鍵都有其值):
namespace1{
key:value1
}
namespace2{
key:value2
}
使用Preferences庫時(shí),應(yīng)該定義要保存的數(shù)據(jù)類型。如果想讀取該數(shù)據(jù),則必須知道保存的數(shù)據(jù)類型,也就是說,寫入和讀取的數(shù)據(jù)類型應(yīng)該相同。
支持以下數(shù)據(jù)類型的保存:char、char、short、Ushort、int、Uint、long、Ulong、long64、Ulong64、float、double、bool、字符串和字節(jié)。
Preferences庫函數(shù)說明
首先包含頭文件
Preferences 庫
然后定義一個(gè)實(shí)例
Preferences preferences;
打開一個(gè)命名空間
begin方法打開一個(gè)帶有定義命名空間的“儲存空間”,參數(shù)為false代表我們在讀/寫模式下使用,為true代表以只讀的方式打開或創(chuàng)建命令空間,命名空間名稱最多為15個(gè)字符。
preferences.begin("my-app", false);
清除preferences
從打開的命名空間中刪除一個(gè)鍵。
preferences.remove(key);
關(guān)閉preferences
使用end方法在打開的命名空間下關(guān)閉preferences
preferences.end();
放置一個(gè)k-v
獲取一個(gè)k-v
刪除命名空間
在Preferences 庫中,并沒有完全刪除命令空間的方法,我們存儲很多數(shù)據(jù)之后,nvs分區(qū)可能就滿了,所以我們想要完全擦除nvs分區(qū),可以使用以下程序運(yùn)行一次:
#include < nvs_flash.h >
void setup() {
nvs_flash_erase(); // 擦除NVS分區(qū)
nvs_flash_init(); // 初始化NVS分區(qū)
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() {
}
這個(gè)例子增加了一個(gè)counter鍵,每次運(yùn)行都加一,我們在按下復(fù)位鍵之后,可以看到下面你的現(xiàn)象,數(shù)據(jù)保存起來了。
Preferences庫很方便保存鍵:值對。即使在重置 ESP32 或斷電后,閃存中保存的數(shù)據(jù)仍然存在。
感謝大家,關(guān)于ESP32的學(xué)習(xí),希望大家Enjoy!
-
存儲
+關(guān)注
關(guān)注
13文章
4499瀏覽量
87059 -
網(wǎng)絡(luò)
+關(guān)注
關(guān)注
14文章
7763瀏覽量
90353 -
EEPROM
+關(guān)注
關(guān)注
9文章
1082瀏覽量
83263 -
數(shù)據(jù)結(jié)構(gòu)
+關(guān)注
關(guān)注
3文章
573瀏覽量
40597 -
ESP32
+關(guān)注
關(guān)注
20文章
1006瀏覽量
18821
發(fā)布評論請先 登錄
請問esp32s3如何保存突然停電時(shí)的數(shù)據(jù)?
ESP32-S3-WROMM-1U同時(shí)讀取nvs和寫ota分區(qū)會造成系統(tǒng)異常嗎?
ESP32-S3無法使用NVS分區(qū)是怎么回事?
基于PlatfromIO-Arduino的ESP32-Flash分區(qū)
ESP32之ESP-IDF學(xué)習(xí)筆記
使用ESP32-S3無法使用NVS分區(qū)是為什么?
如何將ESP-IDF引導(dǎo)加載程序與用于NVS的ESP32-Arduino代碼一起使用?
ESP32 開發(fā)筆記(四)LVGL控件學(xué)習(xí) Window 窗口控件 X

ESP32驅(qū)動AD7705

ESP32-Flash分區(qū),基于PlatfromIO-Arduino

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

評論