今天我们来说说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 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 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!
植物病害监测:孢子捕捉仪在农业中的实际应用
RECOM推高性价比板载AC/DC转换器 适用于全球范围内的各种应用
怎样利用CAE来研发电动车电池?
精密恒温培养箱BPH-9042的产品特点介绍
微型空气监测站是如何建设网格化环境监测系统的
ESP32学习笔记:NVS分区永久保存数据
深圳市市场监督管理局抽查发现4批次防爆灯产品抽查不合格
汽车遥控门禁系统/遥控车门开关系统简介
深度解读半导体厂商为何对车用芯片虎视眈眈
时钟闹铃控制电路原理图
国轩高科在电池结构上的创新思路
电子温控器工作原理
荣耀9什么时候上市?荣耀9最新消息:荣耀9再次用色彩吸引用户,外观向小米看齐
苹果新iPhone第一次比较靠谱的与外界“亲密接触”了
建设“智慧校园”,从“移动校园开始”
编码器AB信号脉冲计数器,角度速度位移测量IO采集模块/转速测量
Diodes电池保护电路配备精准电压检测功能有效延长电池寿命
凌阳科技推出带AD功能小资源单片机
PEM格式RSA密钥解析(一)Base64转16进制格式
TMS Software:TMS BIZ产品——TMS Logging