ESP.restart() kills eeprom memory? ESP32 Node32s

Hi to every one,

ESP.restart() kills eeprom and RTC memories?

/*
   Testing EEPROMClas
   ESP32 Flash memory in a multiple user-defined EEPROM class objects.
   Created for arduino-esp32 on 25 Dec, 2017
   by Elochukwu Ifediora (fedy0)
   converted to nvs by lbernstone - 06/22/2019
   https://rntlab.com/question/eeprom-update-on-esp32/#answer-70346
   https://github.com/espressif/arduino-esp32/tree/master/libraries/Preferences
  putString(const char* key, const String value)
*/
#include "EEPROM.h"
#include "Preferences.h"
#define uS_TO_S_FACTOR 1000000ULL
RTC_DATA_ATTR uint32_t age = 0;
RTC_DATA_ATTR uint32_t check2 = 0;
RTC_DATA_ATTR uint32_t check3 = 0;

EEPROMClass NAMES("eeprom0", 0x500);
EEPROMClass HEIGHT("eeprom1", 0x200);
EEPROMClass AGE("eeprom2", 0x100);


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

  Serial.println(EEPROM.read(0x100));
  Serial.println(EEPROM.read(0));
  printf("\n-----------------");

  // Instantiate eeprom objects with parameter/argument names and sizes

  if (!NAMES.begin(NAMES.length())) {
    Serial.println("Failed to initialise NAMES");
    Serial.println("Restarting...");
    ESP.restart();
  }
  if (!HEIGHT.begin(HEIGHT.length())) {
    Serial.println("Failed to initialise HEIGHT");
    Serial.println("Restarting...");
    delay(1000);
    ESP.restart();
  }
  if (!AGE.begin(AGE.length())) {
    Serial.println("Failed to initialise AGE");
    Serial.println("Restarting...");
    delay(1000);
    ESP.restart();
  }
  AGE.get(0, age);
  printf("\nrestarted age = %d\n", age);

  Serial.println(check2);


  if ((check2 < 1)) {
    const char* name = "Some text";
    double height = 5.8;
    uint32_t age = 47;
    check2 += 1;
    Serial.print("In check2:  "); Serial.println(check2);
    Serial.println("----------------- WRITE -------------------\n");
    NAMES.put(0, name);
    HEIGHT.put(0, height);
    AGE.put(0, 47);

    // Write: Variables ---> EEPROM stores
    Serial.print("name: "); Serial.println(name);
    Serial.print("height: "); Serial.println(height);
    Serial.print("age: "); Serial.println(age);

    Serial.println("----------------- CLEAR -------------------\n");
    // Clear variables
    Serial.printf("name: %s", name);
    name = '\0';
    Serial.println(name);
    height = 0;
    age = 0;
    Serial.print("name: "); Serial.println(name);
    Serial.print("height: "); Serial.println(height);
    Serial.print("age: "); Serial.println(age);

    Serial.println("----------------- READ -------------------\n");
    // Read: Variables <--- EEPROM stores

    NAMES.get(0, name);
    HEIGHT.get(0, height);
    AGE.get(0, age);
    Serial.print("name: "); Serial.println( name);
    Serial.print("height: "); Serial.println(height);
    Serial.print("age: "); Serial.println(age);
    uint32_t age3 = 47;
    //AGE.put(0, age3);
    AGE.put(0, 47);
    Serial.println("\n------------ Going to sleep. --------------");
    delay(1000);
    esp_sleep_enable_timer_wakeup(30 * uS_TO_S_FACTOR);
    esp_deep_sleep_start();
    //ESP.restart();
  }
  if (check2 > 0) {
    Serial.println("\nIn else after sleep");
    const char rname[24] = {};
    NAMES.get(0, rname[0]);
    double height2;
    HEIGHT.get(0, height2);
    uint32_t age2;
    AGE.get(0, age2);

    Serial.print("name: "); Serial.println( rname);
    Serial.print("height: "); Serial.println(height2);
    Serial.print("age2: "); Serial.println(age2);
    check3 += 1;
  }
  delay(20000);
  if (check3 < 2) {
    Serial.println("\n-----------  ESP.restart() ---------------");
    ESP.restart();
  }

    Serial.println("\n--------------  Done!");
}
void loop() {
  delay(0xFFFFFFFF);
}

Here is the OUTPUT:

entry 0x400806ac
0
0

-----------------
restarted age = -1
0
In check2:  1
----------------- WRITE -------------------

name: Some text
height: 5.80
age: 47
----------------- CLEAR -------------------

name: Some text
name: 
height: 0.00
age: 0
----------------- READ -------------------

name: Some text
height: 5.80
age: 47

------------ Going to sleep. --------------

entry 0x400806ac
0
0

-----------------
restarted age = -1
1

In else after sleep
name: ⸮
height: nan
age2: 4294967295

------------------    ---

entry 0x400806ac
0  
0		Again and again because RTC is erased?
-----------------
restarted age = -1
0
In check2:  1	// 0 and 1 alternately
----------------- WRITE -------------------

name: Some text
height: 5.80
age: 47
----------------- CLEAR -------------------

name: Some text
name: 
height: 0.00
age: 0
----------------- READ -------------------

name: Some text
height: 5.80
age: 47

------------ Going to sleep. --------------  //second  run

entry 0x400806ac
0
0

-----------------

Again and again because EEPROM is erased?

Regards

JPDaviiau

I cannot find a commit() call in your sketch, so the data is never written to the EEPROM but simply stored in RAM, which is cleared of course in the case of a restart.

and RTC memories?

Yes.

See the ESP32 API, SPIFFS API Reference - ESP32 - — ESP-IDF Programming Guide latest documentation

I cannot find a commit() call in your sketch, so the data is never written to the EEPROM but simply stored in RAM, which is cleared of course in the case of a restart.

So nvs is not a permanent memory storage?

name=Preferences
version=1.0
author=Hristo Gochkov
maintainer=Hristo Gochkov <hristo@espressif.com>
sentence=Provides friendly access to ESP32's Non-Volatile Storage

NVS commit

size_t Preferences::putShort(const char* key, int16_t value){
    if(!_started || !key || _readOnly){
        return 0;
    }
    esp_err_t err = nvs_set_i16(_handle, key, value);
    if(err){
        log_e("nvs_set_i16 fail: %s %s", key, nvs_error(err));
        return 0;
    }
    err = nvs_commit(_handle);
    if(err){
        log_e("nvs_commit fail: %s %s", key, nvs_error(err));
        return 0;
    }
    return 2;
}

Regard

JPD

add one thing at a time. Test step by step if the code is working.
Did you do a check if your values are are stored by storing and then reading them without a restart?
best regards Stefan

Should you not be using EEPROM.write(Address, value) or EEPROM.update whith will only change if the value changes.

before ESP.restart(), you should either do EEPROM.commit(); or probably even more to the point EEPROM.end() Also before writing anything to the EEPROM on an ESP, you have to doEEPROM. begin(size);

I think you cant mix the Preferences class with the EEPROM write read etc.

I tied it anyway without success,

Preferences has it's own commit technique.

Apparently it was created to replace the esp32 deprecated EEPROM.

EEPROM is deprecated and replaced by Preferences. This is a lightweight wrapper around NVS.
Preferences automatically commits any change you make to your NVS storage.
You can inspect the nvs storage using one of the tools available in Github. I've used Ed Smallenburg's tool and it does the job. You may want to add a little CLI to inspect the contents of a selected namespace (read the docs about nvs to understand how it works).

EEPROM is deprecated

Can you please provide a link to where it is deprecated ?

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