• 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

How to save a LOCAL variable that survives Deep Sleep. Cannot use RTC_DATA_ATTR.

Q&A Forum › Category: ESP32 › How to save a LOCAL variable that survives Deep Sleep. Cannot use RTC_DATA_ATTR.
0 Vote Up Vote Down
Ron asked 5 years ago
Question Tags: Deep sleep
9 Answers
0 Vote Up Vote Down
Steph answered 5 years ago

Hey Ron,

Why can’t you use RTC_DATA_ATTR?

You can also choose to save the value of your variable in Flash memory with the Preferences library (EEPROM is deprecated. It is provided on ESP32 for backwards compatibility with existing Arduino applications).

0 Vote Up Vote Down
Ron answered 5 years ago

Using RTC_DATA_ATTR gives an error during compile: “section attribute cannot be specified for local variables”.

I am trying to learn how to use the EEPROM but I think the following line (used after Deep Sleep) is no good: waterWeight = EEPROM.read(8). The variable was stored as EEPROM.write(8, waterWeight) before Deep Sleep.

0 Vote Up Vote Down
Ron answered 5 years ago

#include
#include
long bucketWeight_new = 0;
long bucketWeight_old = 0;
#define EEPROM_SIZE 8
void setup(){
esp_sleep_enable_timer_wakeup(10*1e6);
bucketWeight_new = 10.1;
EEPROM.begin (EEPROM_SIZE);
EEPROM.write(8, bucketWeight_new);
EEPROM.commit();
bucketWeight_old = EEPROM.read(8); //assign 10.1 to old
Serial.print(“bucketWeight_old = “); Serial.println(bucketWeight_old);
esp_deep_sleep_start();
//Serial print statement is not executed

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

Hi Ron.
I don’t think there is any problem saving variables using EEPROM and deep sleep.
Doesn’t it work?
Also, it is weird that the RTC_DATA_ATTR is not working.
Regards,
Sara

0 Vote Up Vote Down
Steph answered 5 years ago

You must have declared your variable as a local variable.
However, a variable that is intended to be hosted in RTC memory must be global.

@Sandra… Looks like there’s a rendering bug in the HTML code since Ron’s last answer… a bit like if a <pre> tag or a <code> tag wasn’t closed properly…

0 Vote Up Vote Down
Ron answered 5 years ago

You have the answer! I did not know I had to declare the variable using RTC_DATA_ATTR (variable name)=0 before Setup. It makes sense now. Hopefully this will solve my problem. Thank you very much for your help!!

0 Vote Up Vote Down
Steph answered 5 years ago

You’re welcome.
No more need to bother with the EEPROM library (although it’s deprecated as I told you before).

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

Thank you all for your contributions.
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

0 Vote Up Vote Down
Ron answered 5 years ago

It is working perfectly now! THANKS AGAIN!! (not using EEPROM)

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.