According to the ESP8266 Low Power notice (attached) the only way to wake an ESP8266 through a GPIO interrupt is by using light sleep.
Program below manages to bring current consumption down from about 150mA to 1.3mA.
But wake up does not yet work. An interrupt on negative edge is attached to pin 2, yet no wake up.
Serial monitor remains at "sleep started".
What could be the issue?
EDIT: problem solved.
Interrupt on edge does not work. Needs to be replaced with either LOLEVEL or HILEVEL.
Beware also that interrupt must be pulsed. When level is not reset before wifi is restarted, wake-up does not work anymore after back to sleep. This works all GPIO except 0 (keep for bootmode setup).
// https://stackoverflow.com/questions/52767240/light-sleep-with-gpio-wakeup-in-esp8266
// https://www.bountysource.com/issues/29660474-light_sleep-how
// https://github.com/esp8266/Arduino/issues/1381
// https://community.blynk.cc/t/esp8266-light-sleep/13584
#define WAKE_UP_PIN 2
extern "C" {
#include "user_interface.h"
}
extern "C" {
#include "gpio.h"
}
#include <Wire.h>
#include <ESP8266WiFi.h>
const char* ssid = "xxx";
const char* password = "xxx";
WiFiClient client;
void setup()
{
gpio_init();
Serial.begin(115200);
// Force the ESP into client-only mode
WiFi.mode(WIFI_STA);
// Enable light sleep
wifi_set_sleep_type(LIGHT_SLEEP_T);
Serial.println("setup done");
}
void light_sleep() {
Serial.println("sleep started");
wifi_station_disconnect();
wifi_set_opmode(NULL_MODE);
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
wifi_fpm_open();
gpio_pin_wakeup_enable(GPIO_ID_PIN(WAKE_UP_PIN), GPIO_PIN_INTR_NEGEDGE);
wifi_fpm_do_sleep(0xFFFFFFF); // sleep for longest possible time
delay(200);
gpio_pin_wakeup_disable();
Serial.println("wake up / 0xFFFFFFF");
wifi_fpm_close();
wifi_set_opmode(STATION_MODE);
wifi_station_connect();
initWifi();
delay(100);
Serial.println("end sleep routine commands");
}
void initWifi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print(F("Connecting to "));
Serial.println(ssid);
while ((WiFi.status() != WL_CONNECTED)) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
initWifi();
Serial.println("loop started");
delay(200);
light_sleep();
delay(200);
Serial.println("Wake Up Sid");
}
esp8266-low_power_solutions_en_0.pdf (509 KB)