ESP32 Daten und Einstellungen dauerhaft speichern

Often you have noticed that settings, variables or inputs are no longer available after a reboot of the ESP32. Today we show you how to save variables or settings permanently, surviving a power failure or restart.

Arduino has an EEPROM memory where such values can be stored. ESP32 does not have such a memory. With ESP32 there is a flash memory (similar to a SSD/USB stick).

To access it we use preferences which are included in the ESP library. If you don't have this library installed on your computer yet, have a look at this Ebook: ESP32 Lora Module with OLED

Let's start a new Arduino IDE project and paste this code:


#include <Preferences.h>

Preferences preferences;                        

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

  preferences.begin("az", false);                         //Ordner az anlegen/verwenden

  //preferences.clear();                                  //Alle "Dateien" unter dem Ordner az löschen

  //preferences.remove("Start");                          //"Datei" Start löschen

  unsigned int counter = preferences.getUInt("Start", 0); //"Datei" Start auslesen und in Variable counter schreiben
                                                          //", 0" bedeutet, falls Start nicht existiert standardwert 0 übergeben

 counter++;                                               //Variable counter um 1 erhöhen

  Serial.printf("Anzahl Starts: %u\n", counter);          //Variable counter Seriell ausgeben

  preferences.putUInt("Start", counter);                  //Variable counter unter "Datei" Start speichern

  preferences.end();                                      //Preferences beenden

  Serial.println("Neustart in 10 Sekunden...");
  delay(10000);                                           //Wartezeit 10000ms = 10s

  ESP.restart();
}

void loop() {}


What does this code do?

First we create a new folder or use it as a current folder.

preferences.begin("az", false);  

If you want to delete all content in the AZ folder, you use:

preferences.clear();  


You can also delete a specific file:

preferences.remove("Start"); 


With the commands

preferences.putUInt

can integer data are saved and with

preferences.getUInt

stored integer data can be read out again.

In addition there are the following further possibilities (String, Long...) to store and read out files:

putChar
putUChar
putShort
putUShort
putInt
putUInt
putLong
putULong
putLong64
putULong64
putFloat
putDouble
putBool
putString
putString
putBytes
getChar
getUChar
getShort
getUShort
getInt
getUInt
getLong
getULong
getLong64
getULong64
getFloat
getDouble
getBool
getString
getString
getBytes


unsigned int counter = preferences.getUInt("Start", 0);

With getUInt we read the file "Start" and write it into the variable counter. If the file "Start" does not exist, a default value is taken, in this case a 0. The default value is added after the file with a comma.

After increasing and outputting the variable, we store the variable counter in the file Start :

preferences.putUInt("Start", counter);  

After we have saved everything, we close our folder az again, with the command:

preferences.end();  

Now we restart the ESP32 and start again from the beginning. We can now also disconnect the power supply and always know exactly how many times the ESP has been started, because this value is now read out at startup and stored again increased by one.

With this function now also other values can be stored and used, e.g. WLAN access data, load status... think of something.

Have fun experimenting!




Esp-32Grundlagen software

5 comments

Markus

Markus

Danke für den guten Beitrag.
Kann ich eigentlich die Preferences und Spiffs in einem Sketch gleichzeitig nutzen. Oder gibt es da Probleme bei der Nutzung des NVS.

Brian

Brian

Ist es richtig, dass dieser Speicher beim ESP eine begrenzte Anzahl von Schreibzyclen hat? Wie hoch ist diese?

Noob-nerd

Noob-nerd

if (preferences.getUInt(“Start”, -1) == -1 ){
//gibt nichts
}

Peter Pi.

Peter Pi.

Danke! Mich würde interessieren, wie man erfährt, ob bei einem get ein Schlüssel überhaupt existiert.

Hoktaar

Hoktaar

Hi & Danke für diesen sehr guten Beitrag.

Geht das auch mit einem ESP8266 oder gibt es dort etwas ähnliches was so einfach ist?

Leave a comment

All comments are moderated before being published