#include <Preferences.h>
Preferences preferences; // Create a Preferences object
int variable1;
String variable2;
void setup() {
Serial.begin(9600);
Serial.println("BEGIN");
Serial.println();
}
void loop() {
// Start preferences with the namespace "myApp" in read-write mode
preferences.begin("myApp", false);
variable1 = 45;
// Store a signed integer
// my variable name, value to store
preferences.putInt("variable1", variable1);
Serial.print("Stored variable1: ");
Serial.println(variable1);
// Get the variable1 from the memory (with a default value 0 if not found in flesh)
// my variable name, default value, if memory is empty
variable1 = preferences.getInt("variable1", 0);
Serial.print("Retrieved variable1: ");
Serial.println(variable1);
Serial.println();
// Store a string
variable2 = "Hello ESP32";
preferences.putString("variable2", variable2);
Serial.print("Stored variable2: ");
Serial.println(variable2);
// retrieve the string, with a default value (No data) if not found
variable2 = preferences.getString("variable2", "No data");
Serial.print("Retrieved variable2: ");
Serial.println(variable2);
// Calling preferences.clear(); removes all stored data under the current
// namespace from the flash memory.
preferences.clear();
// End the preferences session
preferences.end();
delay(100000);
}