ESP32 - Variables after Deep Sleep

sage1234
Posts: 1
Joined: Tue Sep 24, 2024 4:28 am

ESP32 - Variables after Deep Sleep

Postby sage1234 » Tue Sep 24, 2024 4:33 am

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();
}

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 - Variables after Deep Sleep

Postby ESP_Sprite » Tue Sep 24, 2024 8:49 am

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;'

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32 - Variables after Deep Sleep

Postby MicroController » Tue Sep 24, 2024 10:55 am

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?

Who is online

Users browsing this forum: No registered users and 180 guests