VideoBar

This content is not yet available over encrypted connections.

Thursday, February 9, 2017

ESP32 Deep Sleep Example

ESP32 deep sleep API


Using the ESP32 in a more low power mode you have to consider from time to time to send the ESP32 to deep sleep. To do so you can use the deep sleep API from the esp-idf (Espressif IoT Development Framework).

There are three topics you can use the deep sleep mode: GPIO, TIMER or ULP (only available in some Assembler code today).

ESP32 deep sleep API

 For GPIO related deep sleep you have the following API calls:

But be aware only GPIOs which are have RTC functionality can be used: 0,2,4,12-15,25-27,32-39

esp_err_t esp_deep_sleep_pd_config(esp_deep_sleep_pd_domain_t domain,
                                   esp_deep_sleep_pd_option_t option)

esp_err_t esp_deep_sleep_enable_ext0_wakeup(gpio_num_t gpio_num,
                                            int level)

esp_err_t esp_deep_sleep_enable_ext1_wakeup(uint64_t mask,                                                     esp_ext1_wakeup_mode_t mode)

void esp_deep_sleep_start()



And here an example:

#define GPIO_INPUT_IO_TRIGGER     0  // There is the Button on GPIO 0

void app_main() {
    printf("start ESP32\n");
    printf("config IO\n");
    esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_AUTO);
    gpio_pullup_en(GPIO_INPUT_IO_TRIGGER);        // use pullup on GPIO
    gpio_pulldown_dis(GPIO_INPUT_IO_TRIGGER);     // not use pulldown on GPIO

    esp_deep_sleep_enable_ext0_wakeup(GPIO_INPUT_IO_TRIGGER, 0);
                                          // Wake if GPIO is low

    printf("deep sleep\n");
    esp_deep_sleep_start();

}


For TIMER related deep sleep you have this API calls:

esp_err_t esp_deep_sleep_enable_timer_wakeup(uint64_t time_in_us)

void esp_deep_sleep(uint64_t time_in_us)

void esp_deep_sleep_start()



And here an example:

#define GPIO_DEEP_SLEEP_DURATION     10  // sleep 10 seconds and then wake up
RTC_DATA_ATTR static time_t last;        // remember last boot in RTC Memory

void app_main() {
    struct timeval now;

    printf("start ESP32\n");

    gettimeofday(&now, NULL);

    printf("deep sleep (%lds since last reset,
                        %lds since last boot)\n",now.tv_sec,now.tv_sec-last);

    last = now.tv_sec;
    esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);

}

And an example for GPIO wake up or TIMER wake up:


#define GPIO_INPUT_IO_TRIGGER     0  // There is the Button on GPIO 0

#define GPIO_DEEP_SLEEP_DURATION     10  // sleep 30 seconds and then wake up
RTC_DATA_ATTR static time_t last;        // remember last boot in RTC Memory

void app_main() {
    struct timeval now;

    printf("start ESP32\n");

    gettimeofday(&now, NULL);

    printf("deep sleep (%lds since last reset, %lds since last boot)\n",now.tv_sec,now.tv_sec-last);

    last = now.tv_sec;

    printf("config Timer\n");
    esp_deep_sleep_enable_timer_wakeup(1000000LL * GPIO_DEEP_SLEEP_DURATION); // set timer but don't sleep now

    printf("config IO\n");
    esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_AUTO); //!< Keep power domain enabled in deep sleep, if it is needed by one of the wakeup options. Otherwise power it down.
    gpio_pullup_en(GPIO_INPUT_IO_TRIGGER);        // use pullup on GPIO
    gpio_pulldown_dis(GPIO_INPUT_IO_TRIGGER);       // not use pulldown on GPIO

    esp_deep_sleep_enable_ext0_wakeup(GPIO_INPUT_IO_TRIGGER, 0); // Wake if GPIO is low

    printf("deep sleep\n");
    esp_deep_sleep_start();

}

Full code can be found at github:

GPIO Example
https://github.com/pcbreflux/espressi...

Timer Example
https://github.com/pcbreflux/espressi...

GPIO+Timer Example
https://github.com/pcbreflux/espressi...

Example uses 

- xtensa 5.2.0
- esp-idf git commit 21c7fc624af3a9eea287c51eee943732111d7b10


More info about the ESP32 and deep sleep:
http://esp-idf.readthedocs.io/en/latest/api/system/deep_sleep.html

And you can watch my YouTube video:
https://youtu.be/6BN1FOMQSPg




1 comment:

  1. Hello, do you think that is possible to wake up with any of two buttons? I tried to call esp_deep_sleep_enable_ext0_wakeup() twice but just the last call had effect. Thanks for the post!

    ReplyDelete