0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

ESP32学习笔记:NVS分区永久保存数据

CHANBAEK 来源:跳动的字节 作者:晓宇 2023-07-15 16:14 次阅读

今天我们来说说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!

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 存储
    +关注

    关注

    12

    文章

    3859

    浏览量

    84666
  • 网络
    +关注

    关注

    14

    文章

    7251

    浏览量

    87443
  • EEPROM
    +关注

    关注

    9

    文章

    927

    浏览量

    80324
  • 数据结构
    +关注

    关注

    3

    文章

    564

    浏览量

    39900
  • ESP32
    +关注

    关注

    13

    文章

    896

    浏览量

    15815
收藏 人收藏

    评论

    相关推荐

    基于PlatfromIO-Arduino的ESP32-Flash分区

    ESP32-Flash分区,基于PlatfromIO-Arduino在PlatformIO中添加分区表在工程根目录下新建partition.csv文件在工程下的platformio.ini文件中添加
    发表于 01-26 08:00

    ESP32ESP-IDF学习笔记

    ESP32ESP-IDF 学习笔记(六)【I2C数据总线(I²C)】文章目录ESP32
    发表于 02-22 07:30

    使用ESP32-S3无法使用NVS分区是为什么?

    用的ESP32-S3-DevKitC-1 N16R8开发板,使用官方示例程测试也无法写入NVS,使用的版本是IDF4.4,
    发表于 02-15 06:21

    ESP32-S3-DevKitC-1 v1.1开发板的nvs分区可以写入,但是无法正常读取出来是怎么回事?

    板子的nvs分区可以正常写入,但是读取出来的时候数据读不出来。下面的日志是我测试的nvs_rw_value这个例程的日志。发现数据读取不出来
    发表于 03-06 06:26

    如何将ESP-IDF引导加载程序与用于NVSESP32-Arduino代码一起使用?

    的 Arduino 代码并将其闪存Stage 2 - ESP32-Arduino 代码我当前的问题是无论 NVS 是否已加密,我都无法从 NVS 分区写入/读取,我什至尝试禁用闪存加密
    发表于 04-13 08:11

    ESP32驱动AD7705

    wifi配置信息保存nvs_flash,实现掉电重新启动直接连入wifi(已完成)3、idf v4.0 调试esp-aliyun-master 生成对应的 NVS
    发表于 11-23 17:51 15次下载
    <b class='flag-5'>ESP32</b>驱动AD7705

    ESP32-Flash分区,基于PlatfromIO-Arduino

    ESP32-Flash分区,基于PlatfromIO-Arduino在PlatformIO中添加分区表在工程根目录下新建partition.csv文件在工程下的platformio.ini文件中添加
    发表于 12-02 12:21 13次下载
    <b class='flag-5'>ESP32</b>-Flash<b class='flag-5'>分区</b>,基于PlatfromIO-Arduino

    [ESP8266学习笔记]components_nvs 非易失性存储 Non-Volatile Storage(NVS),保存数据到flash

    [ESP8266学习笔记]components_nvs 非易失性存储 Non-Volatile Storage(NVS),
    发表于 12-02 12:51 11次下载
    [<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

    [ESP32]学习笔记02

    [ESP32学习笔记02]使用ViusalStudio2017开发ESP32、按键输入检测前言一、安装Visual Studio 2017二、配置开发环境1.安装ViusalGDB2.
    发表于 12-03 17:36 23次下载
    [<b class='flag-5'>ESP32</b>]<b class='flag-5'>学习</b><b class='flag-5'>笔记</b>02

    [ESP32]学习笔记04

    Analog-to-Digital Converter(模数转换器)的使用今天我们学习ESP32提供的ADC外设的使用,ESP32的每个ADC单元都支持两种模式,单次读取和连续读取(DMA),本次
    发表于 12-22 19:02 9次下载
    [<b class='flag-5'>ESP32</b>]<b class='flag-5'>学习</b><b class='flag-5'>笔记</b>04

    SPI主线协议——ESP32学习笔记

    目录SPI主线协议——ESP32学习笔记零、前言一、什么是SPI?二、通信过程​三、极性和相位四、总结SPI主线协议——ESP32学习
    发表于 12-22 19:23 17次下载
    SPI主线协议——<b class='flag-5'>ESP32</b><b class='flag-5'>学习</b><b class='flag-5'>笔记</b>

    ESP32ESP-IDF 教学(六)——I2C数据总线(I²C)

    ESP32ESP-IDF 学习笔记(六)【I2C数据总线(I²C)】文章目录ESP32
    发表于 12-28 19:25 22次下载
    <b class='flag-5'>ESP32</b> 之 <b class='flag-5'>ESP</b>-IDF 教学(六)——I2C<b class='flag-5'>数据</b>总线(I²C)

    ESP32 单片机学习笔记 - 04 - ADC和定时器

    ESP32 单片机学习笔记 - 04 - ADC和定时器一、模拟数字转换器 ADC编程指南:Analog to Digital Converter。数据手册:
    发表于 01-17 13:18 11次下载
    <b class='flag-5'>ESP32</b> 单片机<b class='flag-5'>学习</b><b class='flag-5'>笔记</b> - 04 - ADC和定时器

    ESP32学习笔记:双核

    今天我们来说说ESP32 for Arduino 的双核。
    的头像 发表于 07-15 16:16 3033次阅读
    <b class='flag-5'>ESP32</b><b class='flag-5'>学习</b><b class='flag-5'>笔记</b>:双核

    ESP32学习笔记:WiFi

    今天我们来说说ESP32 的WiFi。
    的头像 发表于 07-15 16:20 2362次阅读
    <b class='flag-5'>ESP32</b><b class='flag-5'>学习</b><b class='flag-5'>笔记</b>:WiFi