当前位置:网站首页>Arduino框架下对ESP32 NVS非易失性存储解读以及应用示例
Arduino框架下对ESP32 NVS非易失性存储解读以及应用示例
2022-08-03 07:35:00 【perseverance52】
Arduino框架下对ESP32 NVS非易失性存储解读以及应用示例
NVS非易失性存储库介绍
非易失性存储 (NVS) 库主要用于在 flash 中存储键值格式的数据。本文档将详细介绍 NVS 常用的一些概念。
- 引用:https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/api-reference/storage/nvs_flash.html
NVS 最适合存储一些较小的数据,而非字符串或二进制大对象 (BLOB) 等较大的数据。如需存储较大的 BLOB 或者字符串,请考虑使用基于磨损均衡库的 FAT 文件系统。
在Arduino框架下,NVS区域在分区表上的体现:
分区表配置文件位置:
C:\Users\Administrator\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\tools\partitions

Arduino IDE分区表配置信息选项:
NVS区域具体在每一份分区表当中的体现
以
app13Mfat3M.csv为例:

在Arduino环境下,不同的分区表默认配置的NVS容量都是
5000Byte(字节)
NVS 存储对象
像用户的一般数据存储例如wifi信息,eeprom库也是使用了其中的一部分。
用户如何使用NVS 来存储数据
相关头文件位置:
C:\Users\Administrator\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\libraries\Preferences\src

- 获取数据函数
int8_t getChar(const char* key, int8_t defaultValue = 0);
uint8_t getUChar(const char* key, uint8_t defaultValue = 0);
int16_t getShort(const char* key, int16_t defaultValue = 0);
uint16_t getUShort(const char* key, uint16_t defaultValue = 0);
int32_t getInt(const char* key, int32_t defaultValue = 0);
uint32_t getUInt(const char* key, uint32_t defaultValue = 0);
int32_t getLong(const char* key, int32_t defaultValue = 0);
uint32_t getULong(const char* key, uint32_t defaultValue = 0);
int64_t getLong64(const char* key, int64_t defaultValue = 0);
uint64_t getULong64(const char* key, uint64_t defaultValue = 0);
float_t getFloat(const char* key, float_t defaultValue = NAN);
double_t getDouble(const char* key, double_t defaultValue = NAN);
bool getBool(const char* key, bool defaultValue = false);
size_t getString(const char* key, char* value, size_t maxLen);
String getString(const char* key, String defaultValue = String());
size_t getBytesLength(const char* key);
size_t getBytes(const char* key, void * buf, size_t maxLen);
- 读取数据函数
size_t putChar(const char* key, int8_t value);
size_t putUChar(const char* key, uint8_t value);
size_t putShort(const char* key, int16_t value);
size_t putUShort(const char* key, uint16_t value);
size_t putInt(const char* key, int32_t value);
size_t putUInt(const char* key, uint32_t value);
size_t putLong(const char* key, int32_t value);
size_t putULong(const char* key, uint32_t value);
size_t putLong64(const char* key, int64_t value);
size_t putULong64(const char* key, uint64_t value);
size_t putFloat(const char* key, float_t value);
size_t putDouble(const char* key, double_t value);
size_t putBool(const char* key, bool value);
size_t putString(const char* key, const char* value);
size_t putString(const char* key, String value);
size_t putBytes(const char* key, const void* value, size_t len);
- 数据操作函数
bool clear();
bool remove(const char * key);
操作流程

示例程序
本示例展示的是对
结构体数据对象进行存储,比起使用eeprom库来实现,在操作上来讲要简单的多。
/* This example shows how to use Preferences (nvs) to store a structure. Note that the maximum size of a putBytes is 496K or 97% of the nvs partition size. nvs has signifcant overhead, so should not be used for data that will change often. */
#include <Preferences.h>//引入头文件
Preferences prefs;
typedef struct {
uint8_t hour;
uint8_t minute;
uint8_t setting1;
uint8_t setting2;
} schedule_t;
void setup() {
Serial.begin(115200);
delay(2000);
while(!Serial);
prefs.begin("schedule"); // use "schedule" namespace
Serial.println(prefs.freeEntries());//获取该"schedule" 命名空间大小:414
uint8_t content[] = {
9, 30, 235, 255, 20, 15, 0, 1}; // two entries
prefs.putBytes("schedule", content, sizeof(content));//将数组存储进"schedule"命名的空间
size_t schLen = prefs.getBytesLength("schedule");
char buffer[schLen]; // prepare a buffer for the data
prefs.getBytes("schedule", buffer, schLen);//获取"schedule"命名的空间数据的大小
if (schLen % sizeof(schedule_t)) {
// 对存储的数据进行校验
log_e("Data is not correct size!");
return;
}
schedule_t *schedule = (schedule_t *) buffer; // 用结构体指针指向buffer数组名,将数组成员赋值给结构体成员变量
Serial.printf("%02d:%02d %d/%d\n",
schedule[1].hour, schedule[1].minute,
schedule[1].setting1, schedule[1].setting2);//20:15 0/1
Serial.printf("%02d:%02d %d/%d\n",
schedule[0].hour, schedule[0].minute,
schedule[0].setting1, schedule[0].setting2);//09:30 235/255
schedule[2] = {
8, 30, 20, 21}; // add a third entry (unsafely)
// force the struct array into a byte array
prefs.putBytes("schedule", schedule, 3*sizeof(schedule_t));
schLen = prefs.getBytesLength("schedule");
char buffer2[schLen];
prefs.getBytes("schedule", buffer2, schLen);
for (int x=0; x<schLen; x++) Serial.printf("%02X ", buffer[x]);//09 1E EB FF 14 0F 00 01 08 1E 14 15
Serial.println();
}
void loop() {
}
- 串口打印信息

边栏推荐
- Roson的Qt之旅#105 QML Image引用大尺寸图片
- 海思项目总结
- 【图像去噪】基于matlab稀疏表示KSVD图像去噪【含Matlab源码 2016期】
- 学习笔记:机器学习之逻辑回归
- PMP每日一练 | 考试不迷路-8.2(包含敏捷+多选)
- Roson的Qt之旅#106 QML在图片上方放置按钮并实现点击按钮切换图片
- ArcEngine(六)用tool工具实现拉框放大缩小和平移
- mysql5.7服务器The innodb_system data file 'ibdata1' must be writable导致无法启动服务器
- Postman will return to results generated CSV file to the local interface
- 【着色器实现Glow可控局部发光效果_Shader效果第十三篇】
猜你喜欢

一文搞懂什么是@Component和@Bean注解以及如何使用

Windows安装MySQL(MIS)

WordPress主题-B2美化通用子主题商业运营版

Postman will return to the interface to generate a json file to the local

redis AOF持久化个人理解

pyspark---encode the suuid interval (based on the number of exposures and clicks)

Haisi project summary

Qt5开发从入门到精通——第二篇(控件篇)

DeFi明斯基时刻:压力测试与启示

Nanny level explains Transformer
随机推荐
2022年 SQL 优化大全总结详解
requests库
day12---接口和协议
华为设备配置BFD与接口联动(触发与BFD联动的接口物理状态变为Down)
tmp
The use of the database table structure document generation tool screw
ORB-SLAM2提取特征点
【云原生--Kubernetes】Pod重启策略
智能客服,还有多少AI泡沫?
002-字段不为null
帆软11版本参数联动为null查询全部
《21天精通TypeScript-5》类型注解与原始类型
推荐系统-排序层-模型:Wide&Deep
JS函数获取本月的第一天和最后一天
ArcEngine(五)用ICommand接口实现放大缩小
REST学习
2022下半年软考「高项&集成」复习计划ta来喽~
Fortify白盒神器20.1.1下载及安装(非百度网盘)
ArcEngine(三)通过MapControl控件实现放大缩小全图漫游
[机缘参悟-59]:《素书》-6-安于礼仪[安礼章第六]