Save ESP32 data and settings permanently

You have often noticed that settings, variables or entries no longer exist after a restart of the ESP32. Today we show you how to store variables or settings permanently, surviving a power failure or reboot.

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

To access it, we use preferences provided in the ESP library. If you have not yet installed this library on your computer, take a look at this ebook: ESP32 Lora module with OLED

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

 

'35; includes <Preferences.h>

Preferences preferences;                         void setup() {   Serial.start(115200);   Serial.Print();   preferences.start("az", false);                         //Create/use directory az   //preferences.clear(); //Delete all "files" under the az folder   //preferences.remove("Start"); /"File" Delete Start   unsuitable int counter = preferences.getUInt("Autumn", 0); ///"File" read start and write in variable counter                                                           //", 0" means to pass default value 0 if startup does not exist

 counter++;                                               //Increase counter variable by 1   Serial.printf("Number of Starts:%u\n", counter);          //Publish counter variable in series   preferences.putUInt("Autumn", counter);                  //Save counter variable under "File" Start   preferences.end();                                      //Quit Preferences   Serial.Print("Reboot in ten seconds...");   delay(10000);                                           //Waiting time 10000ms = 10s   ESP.restart();
}

void run() {}

 

What does this code do?

The first is to create a new folder. use this as the current folder.

preferences.start("az", false);  

If you want to delete all contents in the az folder, you use:

preferences.clear();  

 

You can also delete a certain file:

preferences.remove("Autumn");  

 

With the commands

preferences.putUInt

Integer data can be saved and with

preferences.getUInt

saved Integer data can be read again.

In addition, there are the following other options (string, long...) to save and read files:

putChar
putUChar
putShort
putUShort
putinth
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

 

unsuitable int counter = preferences.getUInt("Autumn", 0);

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

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

preferences.putUInt("Autumn", 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 separate the power supply and always know exactly how often the ESP has been started, because this value is now read out when it is switched on and saved by one increased.

With this function other values can be saved and used, e.g. WLAN access data, load status... Think of something.

Have fun experimenting!

 

 

 

Esp-32Basics 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

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN