Entering deep sleep and wake up with one button

pixelpress
Posts: 1
Joined: Fri Dec 14, 2018 4:00 pm

Entering deep sleep and wake up with one button

Postby pixelpress » Fri Dec 14, 2018 4:31 pm

I'm trying to enter deep sleep mode and wake up again with just one push button. I'm using an interrupt (to enter sleep) while ESP32 is in normal mode, and before setting up the the pin as source for wake up, I detach the interrupt from the pin. The interrupt works fine. However, I never stay in deep sleep. There is some kind of deep sleep reset. So I assume the interrupt and wake up are called at the same time. Which is probably also caused by the button being pressed for some milliseconds of time. But I learned that I can not use delays in interrupts.

I'm wondering if there is a solution for this.

Here is my full code:

Code: Select all

#include <Arduino.h>

void print_wakeup_reason(){
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch(wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  }
}

void IRAM_ATTR isr(){
  detachInterrupt(digitalPinToInterrupt(32)); //because later used for wake up
  Serial.println("Going to sleep now");
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_32,1); //1 = High, 0 = Low connected to GPIO32
  esp_deep_sleep_start();
  
}

void setup() {

  esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
  // put your setup code here, to run once:
  pinMode(32, INPUT_PULLDOWN); //pin 34-39 do not offer internal resistors; This will only work if RTC peripherals
  //are on in sleep, otherwise use 10kOhm external resistor for pull down
  Serial.begin(115200);
  delay(1000); 
  Serial.println("Wake up!");
      //Print the wakeup reason for ESP32
  print_wakeup_reason();
  attachInterrupt(digitalPinToInterrupt(32), isr, RISING);

}

void loop() {

}
Thank you for your help!

boarchuz
Posts: 605
Joined: Tue Aug 21, 2018 5:28 am

Re: Entering deep sleep and wake up with one button

Postby boarchuz » Sat Dec 15, 2018 10:22 pm

You should only be flagging it in the interrupt routine (eg. _goToSleep=true;). That's all.

Then handle it in the loop: if(_goToSleep)...

I'd suggest an external pulldown too.

Who is online

Users browsing this forum: No registered users and 65 guests