I want to run the device from a battery & so need to use the deepsleep function to obtain longevity (the device will be used once or twice per day & potentially away from a power supply for months).
My understanding is that the ESP can decipher a timed wake out of deep sleep from a manual reset (by linking the rst pin to gnd) as itemised on page 5 (2/4) of this document: https://espressif.com/sites/default/files/documentation/esp8266_reset_causes_and_common_fatal_exception_causes_en.pdf.
I have a simple built a simple circuit to hand this:
I have correspondingly written the following code:
#include <ESP8266WiFi.h>
extern "C" {
#include <user_interface.h>
}
const char* ssid = "[MY SSID]"; // This is the name or your wireless network
const char* password = "53¢r£t"; // This is your wireless network password
int connectionCount;
void setup() {
rst_info *resetInfo;
resetInfo = ESP.getResetInfoPtr();
Serial.begin(115200);
Serial.println(" ");
delay(100);
Serial.println("I'm awake!");
Serial.println("Reset Reason is: ");
Serial.println(resetInfo->reason);
// Intermittent wake
if (resetInfo->reason == 5) {
Serial.println("Intermittent Wake");
Serial.println("I've woken up because I can only stay asleep for 35 minutes. I'm preparing to go back to sleep now ...");
};
// Push button wake
if (resetInfo->reason == 6) {
Serial.println("Push Button Pressed");
Serial.println("You woke me. I'm connecting to the WIFI to do stuff!");
connectToWifi();
Serial.println("WIFI related stuff will happen here in the future");
};
Serial.println("Do nothing for 10 seconds"); // This makes it easier to re-flash the chip later.
delay(10000);
Serial.println("Off To Sleep");
delay(100);
ESP.deepSleep(2100000000, WAKE_RF_DEFAULT); //35 minutes (2100 seconds)
Serial.println("Should be asleep but not!");
}
void loop() {
// put your main code here, to run repeatedly:
}
// use the ssid and password to connect to the customer's WiFi
void connectToWifi() {
Serial.print("Connecting to ");
Serial.println(ssid);
// Disable AP_SSID publication in Client mode
WiFi.mode(WIFI_STA);
// Starts WiFi Connection
WiFi.begin(ssid, password);
connectionCount = 30;
Serial.println("");
Serial.println("Connection timeout in: ");
// loop to check if connected to the wi fi network
while (WiFi.status() != WL_CONNECTED && connectionCount > 0) {
delay(450);
digitalWrite(LED_WIFI_GOOD, HIGH);
digitalWrite(BUZZER, HIGH);
delay(50);
digitalWrite(LED_WIFI_GOOD, LOW);
digitalWrite(BUZZER, LOW);
connectionCount--;
Serial.print(connectionCount);
Serial.print(" Current WiFi Status = ");
Serial.print(WiFi.status());
Serial.println(", ");
}
// we're connected to the wi fi network
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Setup Complete");
}
else { // else we never connected to the network, inform the user of the error using the BUZZER and LED
Serial.println("");
Serial.println("Not Connected To WiFi");
}
}
Using ESP.getResetInfoPtr(); I am expecting to see a code of 5 when the device intermittently wakes itself up (I understand that it can only stay asleep for 35 minutes) & a code of 6 when I press the button which initiates a hard reset.
- I am finding that I get a code of 5 each time the device manually wakes from sleep so that works fine.
- I am finding that when the device is not in deep sleep & I reset the device manually I get a code of 6 so that works fine
- HERES MY ISSUE: I find that when the device is in deep sleep & I reset the device, I get a response of 5, not 6 which is what I would expect.
Any thoughts on how to fix this?