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