• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

esp restart() kills eeprom memory with Preferences.h

Q&A Forum › esp restart() kills eeprom memory with Preferences.h
0 Vote Up Vote Down
Jean Pierre Daviau asked 5 years ago

Hi,

Start your code here
/*
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
EEPROM.update() on ESP32
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 = "2020-09-16 | 17:04:28 Humidité du sol: 114.00% Température du sol: 14.87°C Température: 26.40°C Humidité: 65.70% Ressentie: 27.66°C Rosé 19.48°C"; 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); }

Regard

JPDaviau

Question Tags: eeprom, esp.restart(), memory, preferences.h
6 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
I’m sorry but I didn’t understand your question.
Regards,
Sara

0 Vote Up Vote Down
Jean Pierre Daviau answered 5 years ago

After restart or deep_sleep the EEPROM memories are not readable, we cant recover the data.

0 Vote Up Vote Down
Steve Mercer answered 5 years ago

After looking at this the EEPROM library is deprecated in favor of the Preferences library.
 
I tried the example at https://github.com/espressif/arduino-esp32/blob/master/libraries/Preferences/examples/StartCounter/StartCounter.ino and it worked perfectly.
 
My understanding is that ESP32 does not have an EEPROM and the Preferences library is just a wrapper around Flash memory to emulate an EEPROM.

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
As Steve suggested, use the preferences library instead.
Then, let us know if it solved your problem.
Regards,
Sara
 

0 Vote Up Vote Down
Jean Pierre Daviau answered 5 years ago

It does work.

Thank you.

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Great!
I’ll close this issue.
If you need further help, just open a new question.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.