本项止转自github官方arduino-esp32 传在这里仅为阅读源码方便
projectuser
2019-07-05 50148ccffe21ff54262064ec9f2245900eaf18aa
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 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)
*/
 
#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: %u\n", 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() {}