ESP32 waking from sleep, but doesn't call setup
Posted: Wed Feb 14, 2024 9:53 pm
Hi!
I've been experimenting with ESP8266 for a while, and have just got started looking into ESP32 I have an ESP-WROOM-32 WEMOS D1 MINI board. My aim is to set it to sleep (in setup), and then have it wake up whenever either of two GPIO pins change state (high->low, or low->high). After some research, it seems to me GPIO25 and GPIO32 are RTC pins, and can be used for interrupt/wakeup.
The hardware setup is connecting an input pin to an SPDT switch (middle pin), and connect the other pins on the switch to VCC (3.3V) and GND:
(for some reason, I'm apparently unable to inline an image here... Please see the simple circuit at https://ibb.co/whySNGY)
I was able to configure an ext0 interrupt on either of these pins, but not on both at the same time. As I understand it, to do something like this you have to use the ESP-IDF API -- and not "only" the Arduino library. With some help from ChatGPT, I was able to set up this:
When I boot the ESP, the setup method is called and configures the interrupts etc, and goes to sleep. Then, when I flip either of the switches (a single interrupt "event"), the ISR is indeed invoked; I tried some serial.print inside handleInterrupt to verify. However, the setup method is never called. So if I flip another switch, the ESP crashes.
Am I right to expect a "wake up event" should do something like; run the ISR, run setup, and (possibly) run loop?
I've been experimenting with ESP8266 for a while, and have just got started looking into ESP32 I have an ESP-WROOM-32 WEMOS D1 MINI board. My aim is to set it to sleep (in setup), and then have it wake up whenever either of two GPIO pins change state (high->low, or low->high). After some research, it seems to me GPIO25 and GPIO32 are RTC pins, and can be used for interrupt/wakeup.
The hardware setup is connecting an input pin to an SPDT switch (middle pin), and connect the other pins on the switch to VCC (3.3V) and GND:
(for some reason, I'm apparently unable to inline an image here... Please see the simple circuit at https://ibb.co/whySNGY)
I was able to configure an ext0 interrupt on either of these pins, but not on both at the same time. As I understand it, to do something like this you have to use the ESP-IDF API -- and not "only" the Arduino library. With some help from ChatGPT, I was able to set up this:
Code: Select all
#include <esp_sleep.h>
#include <driver/gpio.h>
void IRAM_ATTR handleInterrupt(void* arg) {
}
void setup()
{
Serial.begin(115200);
Serial.println("\nBoot");
// Initialize GPIO pins
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_ANYEDGE; // Interrupt on rising and falling edges
io_conf.pin_bit_mask = ((1ULL<<GPIO_NUM_25) | (1ULL<<GPIO_NUM_32));
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
gpio_config(&io_conf);
// Install the ISR service with default configuration
gpio_install_isr_service(0);
// Hook ISR handler for specific GPIO pin
gpio_isr_handler_add(GPIO_NUM_25, handleInterrupt, NULL);
gpio_isr_handler_add(GPIO_NUM_32, handleInterrupt, NULL);
// Enable wake-up from external pin
esp_sleep_enable_ext1_wakeup(((1ULL<<GPIO_NUM_25) | (1ULL<<GPIO_NUM_32)), ESP_EXT1_WAKEUP_ANY_HIGH);
}
void loop() {
}
Am I right to expect a "wake up event" should do something like; run the ISR, run setup, and (possibly) run loop?