Prevent persisting of global variables to flash

Is that possible?

My goal is that the variables (in RTC memory?) should survive deep sleep, but not power cycling.

Overall I want to have the following logic:

  • When I power-up the ESP32 I need to have WiFi enabled for e.g. 10 minutes and no deep sleep yet
  • Then it disables wifi and goes into deep sleep with timer wake up

I’m using a global variable to determine in which state I am but since it’s persisted - there’s no difference if I power-cycle or deep sleep.

I’ve tried setting

preferences:
  flash_write_interval: 99999d

but it does not seem to help. Maybe this logic can be implemented differently?

Does calling ‘esp_sleep_get_wakeup_cause()’ give you the answer?
Susan

restore_value: false doesn’t work?? - ignore - I see you want them sometimes. I think the wakeup_reason for wake on timer and reboot are the same.

Ok props to Susan - I actually read the docs.

There is actually a seperate wakeup reason for timer:

enum esp_sleep_source_t
Sleep wakeup cause.

Values:

enumerator ESP_SLEEP_WAKEUP_UNDEFINED
In case of deep sleep, reset was not caused by exit from deep sleep.

enumerator ESP_SLEEP_WAKEUP_ALL
Not a wakeup cause, used to disable all wakeup sources with esp_sleep_disable_wakeup_source.

enumerator ESP_SLEEP_WAKEUP_EXT0
Wakeup caused by external signal using RTC_IO.

enumerator ESP_SLEEP_WAKEUP_EXT1
Wakeup caused by external signal using RTC_CNTL.

enumerator ESP_SLEEP_WAKEUP_TIMER
Wakeup caused by timer.

enumerator ESP_SLEEP_WAKEUP_TOUCHPAD
Wakeup caused by touchpad.

enumerator ESP_SLEEP_WAKEUP_ULP
Wakeup caused by ULP program.

enumerator ESP_SLEEP_WAKEUP_GPIO
Wakeup caused by GPIO (light sleep only on ESP32, S2 and S3)

enumerator ESP_SLEEP_WAKEUP_UART
Wakeup caused by UART (light sleep only)

enumerator ESP_SLEEP_WAKEUP_WIFI
Wakeup caused by WIFI (light sleep only)

enumerator ESP_SLEEP_WAKEUP_COCPU
Wakeup caused by COCPU int.

enumerator ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG
Wakeup caused by COCPU crash.

enumerator ESP_SLEEP_WAKEUP_BT
Wakeup caused by BT (light sleep only)

So check wakeup_reason using the function call and ESP_SLEEP_WAKEUP_TIMER should be by timer and ESP_SLEEP_WAKEUP_UNDEFINED should be by reboot… Let us know how you go.

Thanks! Indeed it’s the best solution, I knew about this function from Arduino API, but didn’t think it was available from ESPHome lambas, but it works perfectly:

  on_boot:
    priority: -100
    then:
    - if:
        condition:
          lambda: 'return esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TIMER;'
        then:
        - deep_sleep.prevent: deep_sleep_1
        - wifi.enable:
        - delay: 60s
        - wifi.disable:
        - delay: 1s
        - deep_sleep.allow: deep_sleep_1
1 Like