ESP32 waking from sleep, but doesn't call setup

hobby_guy
Posts: 20
Joined: Sat Jan 29, 2022 3:29 pm

ESP32 waking from sleep, but doesn't call setup

Postby hobby_guy » 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:

Image
(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() {
}
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?

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: ESP32 waking from sleep, but doesn't call setup

Postby liaifat85 » Thu Feb 15, 2024 3:42 pm

You can modify your code like this:

Code: Select all

#include <esp_sleep.h>
#include <driver/gpio.h>

bool isFirstBoot = true;

void IRAM_ATTR handleInterrupt(void* arg) {
  // Handle interrupt event here
}

void setup()
{
  Serial.begin(115200);
  if (isFirstBoot) {
    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);

    isFirstBoot = false;
  }
}

void loop() {
  // Your main code logic here
}

hobby_guy
Posts: 20
Joined: Sat Jan 29, 2022 3:29 pm

Re: ESP32 waking from sleep, but doesn't call setup

Postby hobby_guy » Fri Feb 16, 2024 7:35 pm

Thanks for your response :)

I had another look at my code, and one thing it seems I am missing is the command to actually set the ESP to deep sleep? I take it, `esp_sleep_enable_ext1_wakeup` needs to be followed by `esp_deep_sleep_start()` :)

In any case, my problem is that I want the ESP to wake on any state change of two pins, connected to SPDT slide/toggle switches. They can go from high-to-low, or from low-to-high -- and remain in that state until flipped again. As I understand it, ext1 can monitor multiple (RTC) pins, but it can only wake the ESP on criterion ANY_HIGH or ALL_LOW. What I want is something like "any edge", but apparently there is no such thing?

Trying your suggestion with `isFirstBoot` fixes not re-initializing the wakeup handler etc, but I still have the main problem:

What happens now (with esp_deep_sleep_start) is that the ESP does indeed go to sleep, and wakes if I flip (to HIGH) any of the inputs. However, the ESP keeps waking from sleep immediately for as long as either of the switches remain flipped to high... This is a manual door switch circuit.

Unless there are ways to achieve this with the Arduino/IDF APIs, I'm considering trying a workaround where I use some external circuitry to "swap the states" of a switch after it is toggled. My idea is something along the lines of, if a switch's middle pin is connected to a GPIO (RTC) input, which wakes on HIGH, then let's ensure that the state of the switch after reboot (wakeup) is always low. I.e., I can't connect the switch's "outer pins" to VCC and GND, I'd have to somehow swap VCC and GND on the switch pins so that the GPIO pin would always see GND after going back to sleep. This was hard to explain, but hopefully it makes sense? :)

hobby_guy
Posts: 20
Joined: Sat Jan 29, 2022 3:29 pm

Re: ESP32 waking from sleep, but doesn't call setup

Postby hobby_guy » Sat Feb 17, 2024 10:16 am

I guess this sort of changes the whole topic of the post. Seems I am indeed calling setup when waking up, but the problem is the criterion for actually waking the esp

Who is online

Users browsing this forum: No registered users and 341 guests