ESP32 C3 persist data in NVM between flashing sketches

I have using an ESP32-C3 SOC. I'm needing to write a small string in memory from one sketch and read the value in a second sketch. I've tried using the Preferences library and my tests have not been sucessful. Is what I'm trying to do possible?

What does Your attempt look like? Spells: Please post the code using IDE autoformat and code tags.

The ESP32-C3 has a memory protection.

Someone else had the same problem: https://randomnerdtutorials.com/esp32-save-data-permanently-preferences/#comment-756929

Also here: https://community.platformio.org/t/esp32-c3-continuously-reset/28568

Does this have to do with it ? (it is too long to read it all): https://github.com/espressif/arduino-esp32/issues/6572

I can not found a solution yet.

#include <Preferences.h>
Preferences preferences;
//static char key = "sid";

void setup() {
  // put your setup code here, to run once:
  preferences.begin("mrid", false); 
  Serial.begin(115200);
  //preferences.putString("mrid", "X123");
  
  //preferences.end();
}

void loop() {
  // put your main code here, to run repeatedly:
  
   Serial.println("Serial Number is...");
   Serial.println(preferences.getString("mrid"));
 
  delay(2000);


}

Ok, writing from this sketch then commenting out line 11 works here. Let me now see if I can't get it to read from the second sketch that I'm really needing to use it in.

Found my issue. preferences.getString returns a String and I was needing to convert to a const char* . Was trying preferences.getString("blah").c_str() and needed to do assign to a String first, then do the .c_str().

Can you show a sketch that works ?

So Your okey now?
Make sure thar this doesn't happen: The reading task is reading at the same time as the writing task is writing. Writing needs to have exclusive acces the shared memory. Else a mix of the new and the previous string might be read.

Yup, all good.

1 Like

Then You can mark the topic as solved. Maybe a certain reply can be marked as "solution".

resolved

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.