Page 1 of 1

ESP32 - Variables after Deep Sleep

Posted: Tue Sep 24, 2024 4:33 am
by sage1234
Hello all,

I'm new and playing around with deep sleep on an ESP32-S3. The first time I run my code, everything goes as planned. After the device sleeps, however, I never get to the i = 0 condition so the device will no longer print "Just woke up!"

Does anyone know why this may be? Thank you!

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_sleep.h"
#include "esp_timer.h"
#include "esp_log.h"


void awake_operations(void)
{
    static int i = 0;
    while (i <= 10) {
        if (i == 0) {
            printf("Just woke up!\n");
        }
        else if (i == 1) {
            printf("I've been awake for %i second.\n", i);
        }
        else if (i > 1) {
            printf("I've been awake for %i seconds.\n", i);
        }
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        i++;
    }
}

void deep_sleep(void)
{
    printf("Going to deep-sleep for 5 seconds.\n");
    // vTaskDelay(100 / portTICK_PERIOD_MS);
    
    // Configure deep-sleep wake up timer for 5 seconds.
    uint64_t sleeptime = 5000000;
    esp_sleep_enable_timer_wakeup(sleeptime);

    // Go to deep sleep.
    esp_deep_sleep_start();

}

void app_main(void)
{
    awake_operations();
    deep_sleep();
}

Re: ESP32 - Variables after Deep Sleep

Posted: Tue Sep 24, 2024 8:49 am
by ESP_Sprite
Waking up from deep sleep is like the entire chip being reset, with the exception that the RTC subdomain still is alive. In practice, this means you can distinguish the deep sleep wakeup by looking at the reset reason, and the RTC memory is still intact. I think you want to store 'i' in RTC memory, you do that with 'RTC_DATA_ATTR static int i=0;'

Re: ESP32 - Variables after Deep Sleep

Posted: Tue Sep 24, 2024 10:55 am
by MicroController
sage1234 wrote:
Tue Sep 24, 2024 4:33 am
After the device sleeps, however, I never get to the i = 0 condition so the device will no longer print "Just woke up!"
Do you receive any serial output after esp_deep_sleep_start() is called for the first time?