Demo 23: How to use Preferences to backup Arduino ESP32 data in main flash memory when power is off

1. Introduction
- This library use a portion of main flash memory to store data (beside this way we can use sdcard to store data Demo 7: How to use Arduino ESP32 to store data to sdcard).
- Data is stored under key-value pair. Key is 15 ASCII characters length and must be unique. Value can be:
+ integer types
+ variable length binary data (blob)
- Data type of writing and reading value must be matched.
- Our Preferences has the structure like this:
namespace {
    key: value
}
where namespace is to avoid key collisions (in case we have 2 keys with same name, we use namespace to separate them). E.g:
section1 {
    counter: value
}
section2 {
    counter: value
}
Here, we have a duplicate key named counter but in different namespaces.
- We create a demo for this tutorial: storing number of restart times when user press reset button. and print it on Terminal.
2. Hardware
This library uses main flash memory then no need extra hardware.
3. Software
- To test the program just press the Reset button and see reset_times is increased.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <Preferences.h>

/* create an instance of Preferences library */
Preferences preferences;

void setup() {
  Serial.begin(115200);
  Serial.println();

  /* Start a namespace "iotsharing"
  in Read-Write mode: set second parameter to false 
  Note: Namespace name is limited to 15 chars */
  preferences.begin("iotsharing", false);

  /* if you want to remove all preferences under opened namespace uncomment it */
  //preferences.clear();

  /* if we want to remove the reset_times key uncomment it */
  //preferences.remove("reset_times");

  /* get value of key "reset_times", if key not exist return default value 0 in second argument
  Note: Key name is limited to 15 chars too */
  unsigned int reset_times = preferences.getUInt("reset_times", 0);

  /* we have just reset ESP then increase reset_times */
  reset_times++;

  Serial.printf("Number of restart times: %d\n", reset_times);

  /* Store reset_times to the Preferences */
  preferences.putUInt("reset_times", reset_times);

  /* Close the Preferences */
  preferences.end();
}

void loop() 
{
}
4. Result
 Figure: Press Reset button to see restart times

Post a Comment

4 Comments

Unknown said…

Now there are more methods and functions in the library.
# Syntax Coloring Map NVS
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

Preferences KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
end KEYWORD2

clear KEYWORD2
remove KEYWORD2

putChar KEYWORD2
putUChar KEYWORD2
putShort KEYWORD2
putUShort KEYWORD2
putInt KEYWORD2
putUInt KEYWORD2
putLong KEYWORD2
putULong KEYWORD2
putLong64 KEYWORD2
putULong64 KEYWORD2
putFloat KEYWORD2
putDouble KEYWORD2
putBool KEYWORD2
putString KEYWORD2
putBytes KEYWORD2

getChar KEYWORD2
getUChar KEYWORD2
getShort KEYWORD2
getUShort KEYWORD2
getInt KEYWORD2
getUInt KEYWORD2
getLong KEYWORD2
getULong KEYWORD2
getLong64 KEYWORD2
getULong64 KEYWORD2
getFloat KEYWORD2
getDouble KEYWORD2
getBool KEYWORD2
getString KEYWORD2
getBytes KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
Vu Tran said…
Hardware:
Board: ESP32 Dev Module
Core Installation/update date:
IDE name: Arduino IDE
Flash Frequency: 80Mhz
Upload Speed: 115200

Description:
I can not use if statetement with variable get value from Preference in setup() function. It run if i replace variable with 1 or 0.

Sketch:
//Change the code below by your sketch
#include

void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);

// Get all saved data
preferences.begin("preferences", false);
bluetooth_status = preferences.getBool("bluetooth_status", false); //NVS key bluetooth status

preferences.end();

DEBUG_PRINT("bluetooth_status: ");
DEBUG_PRINTLN(bluetooth_status);

if(bluetoot_status)
{

}
}
void loop() {
}
Debug Messages:
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_
load:0x3fff0010,len:4
load:0x3fff0014,len:732drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x40078000,len:0
load:0x40078000,len:11572
entry 0x40078a14
[D][ESP32RGBmatrixPanel.cpp:262] ESP32RGBmatrixPanel(): Init ESP32RGBmatrixPanel!

[D][ESP32RGBmatrixPanel.cpp:262] ESP32RGBmatrixPanel(): Init ESP32RGBmatrixPanel!

[D][ESP32RGBmatrixPanel.cpp:262] ESP32RGBmatrixPanel(): Init ESP32RGBmatrixPanel!

[D][ESP32RGBmatrixPanel.cpp:262] ESP32RGBmatrixPanel(): Init ESP32RGBmatrixPanel!

assertion "res == pdTRUE" failed: file "/Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/esp32/./ipc.c", line 85, function: esp_ipc_init
abort() was called at PC 0x4014a6e7 on core 0

Backtrace: 0x40089620:0x3ffe3b50 0x4008971f:0x3ffe3b70 0x4014a6e7:0x3ffe3b90 0x4015fe81:0x3ffe3bc0 0x40083009:0x3ffe3c00 0x400831f0:0x3ffe3c30 0x4007886a:0x3ffe3c50 0x400788d1:0x3ffe3c80 0x40078a0e:0x3ffe3cb0 0x40078b33:0x3ffe3e70 0x40007c31:0x3ffe3eb0 0x4000073d:0x3ffe3f20

Rebooting...
Anonymous said…
@Vu Tran: long time ago, but if its still an issue
Could be the typo in your if clause "if(bluetoot_status)"
You wrote Bluetooth without "h"
Greetings