How to wake up an ESP8266 from deep sleep using both a timer and an external push button

Hey everyone, I’m currently working on a project that involves an ESP8266 and I’m having some trouble figuring out how to wake it up from deep sleep using both a timer and an external push button. I know how to wake it up using a timer or a push button individually, but I’m not sure how to combine both approaches. The goal is to have the device wake up every hour, read sensor data, send it to Blynk, and then go back into deep sleep. But, the user should also be able to manually wake it up from deep sleep using a push button. Has anyone had experience with this before or know of any resources that can help? Any advice would be greatly appreciated! Thanks!

When you say a “push button” are you talking about a physical button or a Blynk widget button?

Pete.

I’m referring to a physical external push button.By the way, I have an ESP-12E

HI, I have a similar project where my ESP8266 wakes from deep sleep every 30 minutes to check for mail in the mailbox. When I want to manually wake to test for example I jut hit the reset button - you could attach an external button to the ESP32 reset pin (I think it’s labeled EN on some boards).

cul
billd

Thank you! That’s a great suggestion. Now, is there a way to determine whether the ESP8266 was woken up by the timer or by the external push button?

You can try the ESP.getWakeupReason() function.

You could add a line in your code that only runs when the timer function happen “Woken by Timer”, but doesn’t run on startup/restart.
billd

I tried that but ESP.getWakeupReason() returns the same answer for both cases: “Deep-Sleep Wake”. So it’s impossible to distinguish the timer and the push button using that function.

Have you tried something like this

void setup() {
  Serial.begin(115200); // start the serial communication

  // get the wakeup reason
  ESP_SleepWakeupCause wakeupReason = ESP.getWakeupReason();
  switch (wakeupReason) {
    case ESP_SLEEP_WAKEUP_TIMER:
      Serial.println("Wakeup caused by timer");
      break;
    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_TOUCHPAD:
      Serial.println("Wakeup caused by touchpad");
      break;
    case ESP_SLEEP_WAKEUP_ULP:
      Serial.println("Wakeup caused by ULP program");
      break;
    default:
      Serial.println("Wakeup was not caused by deep sleep");
      break;
  }
}

void loop() {
  // code here
}
1 Like

This doesn’t seem to work. I got the error below:

exit status 1
'ESP_SleepWakeupCause' was not declared in this scope

I’m not really sure how to do that. I use ESP.deepSleep(DEEP_SLEEP_TIME*1000000); to wake up the device from deep sleep after the specified deep sleep time. How would you add a line that only runs when the timer function happens?

You need to declare it as a global variable.

Pete.

I think the main issue is that the “ESP_SleepWakeupCause” class doesn’t exist. I tried using the function " ESP.getResetReason()" instead. However, that function returns “Deep-Sleep Wake” in both cases (when the device is woken up by the timer or by the push button) and it’s not possible to distinguish between the two cases using this function.

Maybe that’s a function that’s only available in the ESP32 core.

Pete.

Hi, @Loic22
do you need more help?
I think the simplest solution is using RESET to wake up ESP from deep sleep.

I have a similar project (Doorbell) with ESP-01. In this case I had to limit counts of button pushing and pushing time due to need allow enough time to connect wifi network and Blynk.
The solution was not just a coding trick, but I had to do some hardware development.
I can share it if you interested in.

aquarius