Making the ESP8266 sleep for longer than 72 minutes (sort of)

The max sleptime for the ESP8266 is limited to about 71-72 minutes. If for whatever reason you make it want to sleep longer between making contact with WiFi, or before it does other tasks, one can set a counter. So suppose one sets wishes the ESP8266 to have 4 hours of ‘sleep’, set the sleep time to 1 hour and on wake up, the only thing the ESP8266 then does is to increase a counter in RTC.
A skeleton program that can do that looks like this.:

#include <ESP8266WiFi.h>

const char* ssid = "yourSSID";
const char* password = "YourPW";

// Set the sleep time to 1 hour (in microseconds)
const uint64_t sleepTime = 1 * 60 * 60 * 1000000ULL;

// RTC memory address for the counter
int rtcCounterAddress = 0;

void setup() {
Serial.begin(115200);

// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");

// Read the counter from RTC memory
int counter = ESP.rtcUserMemoryRead(rtcCounterAddress, &counter, sizeof(counter));
Serial.printf("Counter: %d\n", counter);

// Increase the counter
counter++;

// If the counter reaches 4, reset the counter and make contact with WiFi
if (counter >= 4) {
counter = 0;
Serial.println("Making contact with WiFi...");

// Your code for making contact with WiFi goes here

// For example, you can make an HTTP request or perform any other necessary operation

} else {
Serial.println("Not yet time to make contact with WiFi");
}

// Save the updated counter to RTC memory
ESP.rtcUserMemoryWrite(rtcCounterAddress, &counter, sizeof(counter));

// Disconnect from Wi-Fi
WiFi.disconnect(true);

// Enter deep sleep mode
Serial.printf("Going to sleep for %lld microseconds\n", sleepTime);
ESP.deepSleep(sleepTime);
}

void loop() {
// Nothing to do here
}

As said, this is just a skeleton program and you can insert the activity you wish for, e.g. make contact with Internet, or perform another operation

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.