NVS
非易失性存储库介绍非易失性存储 (NVS) 库主要用于在 flash 中存储键值格式的数据。本文档将详细介绍 NVS 常用的一些概念。
NVS 最适合存储一些较小的数据,而非字符串或二进制大对象 (BLOB) 等较大的数据。如需存储较大的 BLOB 或者字符串,请考虑使用基于磨损均衡库的 FAT 文件系统。
分区表配置文件位置:
C:\Users\Administrator\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.4\tools\partitions
以
app13Mfat3M.csv
为例:
在Arduino环境下,不同的分区表默认配置的NVS容量都是
5000
Byte(字节)
像用户的一般数据存储例如wifi信息,eeprom库也是使用了其中的一部分。
相关头文件位置:
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);
15
个字符。(键的名字不要取过长的字符串名)NameSpace
)当中,键名不能有重复。本示例展示的是对
结构体
数据对象进行存储,比起使用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 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() {}