2017-02-11 23:08:22 +01:00
|
|
|
/*
|
2018-01-17 23:59:35 +01:00
|
|
|
ESP32 startup counter example with Preferences library.
|
2017-02-11 23:08:22 +01:00
|
|
|
|
2018-01-17 23:59:35 +01:00
|
|
|
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
|
2017-02-11 23:08:22 +01:00
|
|
|
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();
|
|
|
|
|
2018-01-17 23:59:35 +01:00
|
|
|
// 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
|
2017-02-11 23:08:22 +01:00
|
|
|
// RW-mode (second parameter has to be false).
|
2018-01-17 23:59:35 +01:00
|
|
|
// Note: Namespace name is limited to 15 chars.
|
2017-02-11 23:08:22 +01:00
|
|
|
preferences.begin("my-app", false);
|
|
|
|
|
2018-01-17 23:59:35 +01:00
|
|
|
// Remove all preferences under the opened namespace
|
2017-02-11 23:08:22 +01:00
|
|
|
//preferences.clear();
|
|
|
|
|
|
|
|
// Or remove the counter key only
|
|
|
|
//preferences.remove("counter");
|
|
|
|
|
2018-01-17 23:59:35 +01:00
|
|
|
// Get the counter value, if the key does not exist, return a default value of 0
|
|
|
|
// Note: Key name is limited to 15 chars.
|
2017-02-11 23:08:22 +01:00
|
|
|
unsigned int counter = preferences.getUInt("counter", 0);
|
|
|
|
|
2018-01-17 23:59:35 +01:00
|
|
|
// Increase counter by 1
|
2017-02-11 23:08:22 +01:00
|
|
|
counter++;
|
|
|
|
|
2018-01-17 23:59:35 +01:00
|
|
|
// Print the counter to Serial Monitor
|
2017-02-11 23:08:22 +01:00
|
|
|
Serial.printf("Current counter value: %u\n", counter);
|
|
|
|
|
2018-01-17 23:59:35 +01:00
|
|
|
// Store the counter to the Preferences
|
2017-02-11 23:08:22 +01:00
|
|
|
preferences.putUInt("counter", counter);
|
|
|
|
|
|
|
|
// Close the Preferences
|
|
|
|
preferences.end();
|
|
|
|
|
|
|
|
// Wait 10 seconds
|
|
|
|
Serial.println("Restarting in 10 seconds...");
|
|
|
|
delay(10000);
|
|
|
|
|
|
|
|
// Restart ESP
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
|
2018-01-17 23:59:35 +01:00
|
|
|
void loop() {}
|