Page 1 of 1
ESP32 using NVS does not work
Posted: Mon Feb 10, 2025 4:27 pm
by hennesf
Code: Select all
//ESP32_permanent_Speicher_schreiben.ino
#include <Preferences.h>
Preferences tab; // Namensraum festlegen
int a;
int min12 = 555;
int min9 = 183;
int min6 = 520;
int min3 = 920;
int min1 = 720; // Beispielwerte
void setup() {
tab.begin("wert1", false); //Ordner "wertX" für Luftdruckwerte anlegen, lesen und schreiben
tab.begin("wert2", false);
tab.begin("wert3", false);
tab.begin("wert4", false);
tab.begin("wert5", false);
Serial.begin(115200);
while (!Serial)
;
delay(1000);
Serial.println();
Serial.println("Test nichtfluechtiger Speicher");
Serial.println();
Serial.println("Werte die gespeichert werden sollen");
Serial.println();
Serial.print(min12);
Serial.print(" ");
Serial.print(min9);
Serial.print(" ");
Serial.print(min6);
Serial.print(" ");
Serial.print(min3); //Werte die gespeichert werden sollen
Serial.print(" ");
Serial.print(min1);
Serial.print(" ");
Serial.println();
tab.putInt("wert1", min12);
tab.putInt("wert2", min9);
tab.putInt("wert3", min6);
tab.putInt("wert4", min3);
tab.putInt("wert5", min1);
tab.end();
}
void loop() {
}
I tried writing to the NVS of an ESP32 using the code above in Arduino IDE Version 2.3.4. To check whether it worked i tried the following code:
Code: Select all
//ESP32_permanent_Speicher_lesen.ino
#include <Preferences.h>
Preferences tab; // Namensraum festlegen
int min12 = 10;
int min9 = 20;
int min6 = 30;
int min3 = 40;
int min1 = 50; //Beispielwerte
int min12r;
int min9r;
int min6r;
int min3r;
int min1r; //zu lesende Werte
//--------------------------------------
void setup() {
Serial.begin(115200);
while (!Serial)
;
delay(1000);
tab.begin("wert1", true); //Ordner "wert1" für Luftdruckwerte nur lesen
tab.begin("wert2", false);
tab.begin("wert3", false);
tab.begin("wert4", false);
tab.begin("wert5", false);
Serial.println();
Serial.println("Test nichtfluechtiger Speicher-lesen");
Serial.println();
Serial.println("Werte vor dem Lesen");
Serial.println();
Serial.print(min12);
Serial.print(" ");
Serial.print(min9);
Serial.print(" ");
Serial.print(min6);
Serial.print(" ");
Serial.print(min3);
Serial.print(" ");
Serial.print(min1);
Serial.print(" ");
Serial.println();
Serial.println();
Serial.println("Werte nach dem Lesen");
Serial.println();
Serial.println();
tab.begin("wert1", true); //Ordner "Tab" für Luftdruckwerte lesen
tab.getUInt("wert1", min1r);
Serial.print(min1r);
Serial.print(" ");
tab.getUInt("wert2", min9r);
Serial.print(min9r);
Serial.print(" ");
tab.getUInt("wert3", min6r);
Serial.print(min6r);
Serial.print(" ");
tab.getUInt("wert4", min3r);
Serial.print(min3r);
Serial.print(" ");
tab.getUInt("wert5", min1r);
Serial.print(min1r);
Serial.print(" ");
Serial.println();
Serial.println("erwartet wurde 555 183 520 920 720");
Serial.println();
}
void loop() {
}
It did not work at all. The result of reading back was all Zero's.
What's wrong.
Thanks for your assistance
Re: ESP32 using NVS does not work
Posted: Tue Feb 11, 2025 1:11 am
by lbernstone
Does the
example work for you?
Re: ESP32 using NVS does not work
Posted: Tue Feb 11, 2025 9:13 am
by hennesf
Hi there,
yes, it does.
I assume that my "writing-code; schreiben" works, but when flashing the "Read-code; lesen", the NVS Area gets erased by the flashing.
Could that be the possible reason?
Regards
Re: ESP32 using NVS does not work
Posted: Tue Feb 11, 2025 3:40 pm
by hennesf
It seems when flashing the esp with a new sketch (Arduino), the NVS gets overwritten.
So the code in my first post could not work.
Right now i combined writing and reading in one sketch and, hurray, it works. Here it is:
Code: Select all
```cpp
//ESP32_permanent_Speicher_schreiben_und_lesen.ino
#include <Preferences.h>
Preferences tab; // Objektname tab festlegen
int min12 = 555;
int min9 = 16;
int min6 = 520;
int min3 = 999;
int min1 = 720; // Beispielwerte zum Schreiben
int min12r, min9r, min6r, min3r, min1r; // Variable für rückgelesene Werte
void setup() {
tab.begin("druckwerte", false); //namensraum "druckwerte" für Luftdruckwerte anlegen, zum lesen und schreiben, Namensraum öffnen
tab.clear(); //löscht alle Werte im Namensraum "druckwerte"
// oder einzelne Werte löschen
tab.remove("wert1");
tab.remove("wert2");
tab.remove("wert3");
tab.remove("wert4");
tab.remove("wert5"); //löscht alle Werte einzeln
//tab.remove("wert1"),("wert2"),("wert3"),("wert4"),("wert5"); //funktioniert NICHT
tab.putInt("wert1", min12); //key "wert1" Value min12
tab.putInt("wert2", min9);
tab.putInt("wert3", min6);
tab.putInt("wert4", min3);
tab.putInt("wert5", min1);
Serial.begin(115200);
while (!Serial)
;
delay(100);
Serial.println();
Serial.println("Test nichtfluechtiger Speicher");
Serial.println();
Serial.println("Werte die gespeichert werden sollen");
Serial.println();
Serial.print(min12);
Serial.print(" ");
Serial.print(min9);
Serial.print(" ");
Serial.print(min6);
Serial.print(" ");
Serial.print(min3); //Werte die gespeichert werden sollen
Serial.print(" ");
Serial.print(min1);
Serial.print(" ");
Serial.println();
tab.end();
//=====================================================================================LESEN==============================
tab.begin("druckwerte", true); //namensraum "druckwerte" für Luftdruckwerte anlegen, Namensraum öffnen nur lesen
min12r = tab.getInt("wert1", min12);
min9r = tab.getInt("wert2", min9);
min6r = tab.getInt("wert3", min6);
min3r = tab.getInt("wert4", min3);
min1r = tab.getInt("wert5", min1);
Serial.println();
Serial.println("Werte die gelesen wurden");
Serial.println();
Serial.print(min12r);
Serial.print(" ");
Serial.print(min9r);
Serial.print(" ");
Serial.print(min6r);
Serial.print(" ");
Serial.print(min3r); //Werte die gelesen wurden
Serial.print(" ");
Serial.print(min1r);
Serial.print(" ");
Serial.println();
// Restart ESP
// ESP.restart();
}
void loop() {
}
```
Re: ESP32 using NVS does not work
Posted: Wed Feb 12, 2025 3:11 am
by lbernstone
Flashing new code only erases the nvs partition if you
a) Change the partition scheme
b) Have "Erase flash on upload" enabled
Flashing over OTA will never erase the nvs.
Re: ESP32 using NVS does not work
Posted: Wed Feb 12, 2025 10:25 am
by hennesf
Flash will be erased from 0x00001000 to 0x00007fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00010000 to 0x000f7fff...
It seems, that is what happens?
Re: ESP32 using NVS does not work
Posted: Tue Feb 18, 2025 2:35 pm
by hennesf
Does that mean that the NVS Contents get overwritten?
Flash will be erased from 0x00001000 to 0x00007fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Flash will be erased from 0x0000e000 to 0x0000ffff...
Flash will be erased from 0x00010000 to 0x000f7fff...
This is what my Arduino IDE announces.
What are the adresses of the NVS in delivery state; nothing changed by me?
Hope i got understood.
Regards
Re: ESP32 using NVS does not work
Posted: Tue Feb 18, 2025 3:57 pm
by lbernstone
Re: ESP32 using NVS does not work
Posted: Tue Feb 18, 2025 10:35 pm
by lbernstone
Preferences::begin() opens a namespace. You call tab.begin() last on wert5. So, everything is going into namespace wert5. If you want to manage multiple namespaces, you need to open multiple Preferences objects.