首页 > 其他分享 >ESP32学习笔记:NVS分区永久保存数据

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

时间:2024-06-11 18:54:27浏览次数:8  
标签:NVS Preferences ESP32 counter void Serial 分区 preferences

程序示例:

/*
 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() {

}

其中几个函数的intro如下:

preferences.begin("my-app", false);

#include < nvs_flash.h >

void setup() {
  nvs_flash_erase(); // 擦除NVS分区
  nvs_flash_init();  // 初始化NVS分区
  while(true);
}

void loop() {

}

 放置一个k-v

获取一个k-v

 

标签:NVS,Preferences,ESP32,counter,void,Serial,分区,preferences
From: https://www.cnblogs.com/FBsharl/p/18242573

相关文章

  • 腾讯云CVM主机在原分区(主分区)上增加磁盘空间
    #现有环境:vdb1000G-vdb1500G-剩余500G需要加在vdb1上#1、安装-yuminstall-ycloud-utils-growpart-一般系统都自带#2、执行以下命令,使用growpart工具扩容分区-growpart/dev/vdb1#1表示是第一个分区:vdb1-返回结果如下图......
  • 【esp32 项目】使用I2C
    原理图:图I2C接口的RTC芯片图单片机I2C部分引脚图 软件启动I2C启动Wire库并作为主机或者从机加入总线,这个函数调用一次即可,参数为7位从机地址,不带参数就以主机的形式加入总线。Wire.begin();Wire.begin(address);主设备从从设备请求字节由主设备向从设备请求字节,之......
  • 【esp32 项目】使用I2C第一篇——I2C的科普
    https://www.eepw.com.cn/zhuanlan/315431.html 天我们来玩儿I2C。I2C概述I2C全称是Inter-IntegratedCircuit,是飞利浦半导体公司(06年迁移到NXP了)在1982年发明的,是使用非常广泛的一种通信协议,很多传感器、存储芯片、OLED等,都是在使用I2C。标准输出模式下能达到100kbps的传输......
  • 【esp32 项目】使用esp32的wifi
    原文:《老宇哥带你玩转ESP32:13终于到WiFi功能的使用了》链接:https://www.eepw.com.cn/zhuanlan/315164.html 今天我们来说说ESP32的WiFi。ESP32的WiFi我们之所以使用ESP32做一些小项目,最重要的就是看中了ESP32的WiFi功能,ESP32相当于一个强大的单片机+无线WiFi+蓝牙多合一......
  • 【备忘录】手动重新设置windowsRE分区,命令行方式
    手动将RE分区大小扩大500MB以管理员身份(cmd)打开命令提示符窗口。若要检查WinRE状态,请运行 reagentc/info。如果安装了WinRE,则应有一个“WindowsRE位置”,其中包含WinRE目录的路径。例如,“WindowsRE位置:[file://%3f/GLOBALROOT/device/harddisk0/partition4......
  • 【esp32 项目】中断读取按键
    原理图:图 按键部分图 单片机部分程序:KEY_USR 引脚配置成上拉输入在Arduino中,配置一个IO为上拉输入可以使用pinMode()函数和digitalWrite()函数。pinMode()函数用于设置引脚模式,而digitalWrite()函数用于设置上拉电阻。以下是一个示例代码,展示如何将Arduino的数字引脚3配......
  • 基于ESP32+arduino+platformIO驱动小米模组接入米家app(以温湿度传感器为例)
    1.选择开发板以及开发环境1.ESP32-C3-DevKitC-02作为主控(以下称为ESP32模块)相关文档:ESP32-C3-DevKitC-02-ESP32-C3-—ESP-IDF编程指南latest文档https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32c3/hw-reference/esp32c3/user-guide-devkitc-02.ht......
  • parted分区项目
    parted分区项目用parted交互式分区方法分区:项目1 ext4 分区添加一块100G的硬盘/dev/sdb,分出一个大小为20G的分区/dev/sdb1,然后格式化成ext4的文件系统,创建挂载点/mypart,把格式化好的分区挂载到该挂载点上,并使用该分区,在该分区下创建文件等。步骤:1)关机,添加一块100G硬盘。......
  • fdisk磁盘分区项目
    fdisk分区项目用fdisk交互式分区方法分区:项目1 ext4 分区添加一块100G的硬盘/dev/sdb,分出一个大小为20G的分区/dev/sdb1,然后格式化成ext4的文件系统,创建挂载点/mypart,把格式化好的分区挂载到该挂载点上,并使用该分区,在该分区下创建文件等。步骤:1)关机,添加一块100G硬盘。2......
  • 如何在 Proteus 中添加 esp32 库进行仿真
    前言:    最近有B站粉丝问到如何使用proteus进行ESP32仿真,然后自己去查阅了很多资料进修了一下,整理了出这篇文章,分享一下如何将ESP32库添加到proteus中并进行仿真。  在Proteus中添加ESP32库进行仿真下面具体介绍步骤:1.打开Proteus,转到"库"->"库管理"......