GPIO External Wakeup on ESP32 with MongooseOS and ESP-IDF

Posted on January 5, 2018

One of the reasons I love MongooseOS is that it makes getting started easy and then provides a variety of conveniences for development, but all the while more powerful capabilities are openly available to you when and if you want them.  In particular, the full power of the Espressif IoT Development Framework (ESP-IDF), the official SDK for the ESP32, is available without giving up all the creature comforts.

MongooseOS provides easy access in JavaScript to the ESP32’s deep sleep capability via a timer in the RTC, however Mongoose doesn’t provide access to the more interesting ext0 wake up which uses GPIO signals to wake from deep sleep.  To access ext0 you’ll need to use the ESP-IDF, which means you’ll need to use C++.

I’ve put an example of deep sleep working on Github.  In short, you need to include the “esp_sleep.h” and “driver/rtc_io.h” headers, setup an ext0 wakeup, and then go to sleep.

#include "mgos.h"
#include "driver/rtc_io.h"
#include "esp_sleep.h"

....
   esp_sleep_enable_ext0_wakeup(13, 1); // 0=LOW, 1=HIGH
   esp_deep_sleep_start();

In the above example you configure pin 13 to wake when it goes HIGH. As soon as you call esp_deep_sleep_start() the ESP32 stops running everything but the Real Time Clock (RTC) and GPIO’s associated with it (so called, RTC pins). Power draw is about 15mA.

There are 2 things you need to know about using GPIO for wakeup:

  1. You must use one of the designated RTC GPIO pins.  Look at an ESP32 Pin Out and you’ll see that, for example, GPIO13 is also RTCIO14.  You’ll never need to know the RTC Pins number, but you must use a GPIO that is also an RTCIO pin
  2. After the board wakes up, you’ll want to take the GPIO pin out of RTC mode by calling rtc_gpio_deinit(pin).  If you fail to do this you will get unexpected GPIO results after wakeup.

When you start working with ESP-IDF directly, ensure you read through the relevant manual sections.

I made a short video of my prototype in action, you can see it here on YouTube.