Periodic or interrupt wakeup

esp8266 sleep wakeup

Do something periodically on wake up after deep sleep or by pressing the button on-board.

On WeMos board , the pin REST to 10kΩ pulled up to +3V3 is already there. Remember to include it back with the ESP8266 module.

Code

Download code
#include <ESP8266WiFi.h>
#define USERBUTTON 12 // GPIO012 on ESP or D6 on WeMos

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(2000);
  int userButtonValue = digitalRead(USERBUTTON);

  while(!Serial) { }

  Serial.println("Wake up!");
  
  Serial.print("User button value: ");
  Serial.println(userButtonValue);

  if (userButtonValue == 0) {
    Serial.print("User long pressed button: ");
  }

  Serial.println("Do task");
  Serial.println("Going into deep sleep for 10s, unless button pressed...");
  ESP.deepSleep(10e6);
}

void loop() {
}

Arduino IDE settings

Ensure the following IDE settings before flashing.

Periodic or interrupt wakeup Arduino IDE settings

Serial console

Serial output from the firmware.

Periodic or interrupt wakeup serial console

Prototype

A photo of the actual setup.

Periodic or interrupt wakeup prototype

Schematic

Wire up the hardware accordingly

Periodic or interrupt wakeup schematic

References