Skip to content

Commit 7a8b344

Browse files
Imported Preferences api examples
1 parent 71ceede commit 7a8b344

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Microcontroller startup counter example with ESP32 Preferences library.
3+
4+
This simple example demonstrates using the Preferences library to store how many times
5+
the microcontroller has booted. The Preferences library is a wrapper around the Non-volatile
6+
storage on ESP32 processor.
7+
8+
created for arduino-esp32 09 Feb 2017
9+
by Martin Sloup (Arcao)
10+
*/
11+
12+
#include <Preferences.h>
13+
14+
Preferences preferences;
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
while(!Serial);
19+
Serial.println();
20+
21+
// Open Preferences with my-app namespace. Each application module, library, etc
22+
// has to use a namespace name to prevent key name collisions. We will open storage in
23+
// RW-mode (second parameter has to be false).
24+
// Note: Namespace name is limited to 15 chars.
25+
if (!preferences.begin("my-app1", false)) {
26+
Serial.println("Cannot initialize preferences");
27+
Serial.println("Make sure your WiFi firmware version is greater than 0.3.0");
28+
while(1) {};
29+
}
30+
31+
// Serial.println(">Clear");
32+
// preferences.clear();
33+
// Serial.println("<Clear");
34+
35+
36+
// Remove all preferences under the opened namespace
37+
//preferences.clear();
38+
39+
// Or remove the counter key only
40+
//preferences.remove("counter");
41+
42+
// Get the counter value, if the key does not exist, return a default value of 0
43+
// Note: Key name is limited to 15 chars.
44+
unsigned int counter = preferences.getUInt("counter2", 0);
45+
46+
// Increase counter by 1
47+
counter++;
48+
49+
// Print the counter to Serial Monitor
50+
Serial.print("Current counter value: ");
51+
Serial.println(counter, HEX);
52+
53+
// Store the counter to the Preferences
54+
preferences.putUInt("counter2", counter);
55+
56+
// Close the Preferences
57+
preferences.end();
58+
59+
// Wait 10 seconds
60+
Serial.println("Restarting in 10 seconds...");
61+
delay(10000);
62+
63+
// Reset
64+
NVIC_SystemReset();
65+
}
66+
67+
void loop() {}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
This example shows how to use Preferences (nvs) to store a
3+
structure. Note that the maximum size of a putBytes is 496K
4+
or 97% of the nvs partition size. nvs has significant overhead,
5+
so should not be used for data that will change often.
6+
*/
7+
#include <Preferences.h>
8+
Preferences prefs;
9+
10+
typedef struct {
11+
uint8_t hour;
12+
uint8_t minute;
13+
uint8_t setting1;
14+
uint8_t setting2;
15+
} schedule_t;
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
20+
if (!prefs.begin("schedule")) { // use "schedule" namespace
21+
Serial.println("Cannot initialize preferences");
22+
Serial.println("Make sure your WiFi firmware version is greater than 0.3.0");
23+
while(1) {};
24+
}
25+
uint8_t content[] = {9, 30, 235, 255, 20, 15, 0, 1}; // two entries
26+
prefs.putBytes("schedule", content, sizeof(content));
27+
size_t schLen = prefs.getBytesLength("schedule");
28+
char buffer[schLen]; // prepare a buffer for the data
29+
prefs.getBytes("schedule", buffer, schLen);
30+
if (schLen % sizeof(schedule_t)) { // simple check that data fits
31+
Serial.println("Data is not correct size!");
32+
return;
33+
}
34+
schedule_t *schedule = (schedule_t *) buffer; // cast the bytes into a struct ptr
35+
Serial.print(schedule[1].hour);
36+
Serial.print(":");
37+
Serial.print(schedule[1].minute);
38+
Serial.print(" ");
39+
Serial.print(schedule[1].setting1);
40+
Serial.print("/");
41+
Serial.print(schedule[1].setting2);
42+
Serial.println();
43+
44+
schedule[2] = {8, 30, 20, 21}; // add a third entry (unsafely)
45+
46+
// force the struct array into a byte array
47+
prefs.putBytes("schedule", schedule, 3*sizeof(schedule_t));
48+
schLen = prefs.getBytesLength("schedule");
49+
char buffer2[schLen];
50+
prefs.getBytes("schedule", buffer2, schLen);
51+
for (int x=0; x<schLen; x++) Serial.print((uint8_t)buffer[x]);
52+
Serial.println();
53+
prefs.end();
54+
}
55+
56+
void loop() {}

0 commit comments

Comments
 (0)