To save power, I put the ESP32 in deep sleep until the next plain minute to wake up and display the new time. My problem here is how to estimate accurately the sleep time?
For the first boot, I connect to a ntp server and set the time of the ESP32. I refresh the display and go to deep sleep.
I have seen that my method is not accurate as I can have up to 15 seconds drift in an hour.
I tried to simplify the code: set the time, go to deep sleep for 5 seconds, wake up and display the time on serial monitor. Even in this simple case, when comparing with the ntp website, I can see some drift. So maybe it's my time management which is wrong?
Here is the code, can anyone help me and explain what is wrong or show me how to keep an accurate time over a few hours?
Thanks for your help
Code: Select all
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
RTC_DATA_ATTR int bootCount = 0;
void setClock() { // This part is copied from someone else
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
Serial.print(F("Waiting for NTP time sync: "));
time_t nowSecs = time(nullptr);
while (nowSecs < 8 * 3600 * 2) {
delay(500);
Serial.print(F("."));
yield();
nowSecs = time(nullptr);
}
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
Serial.print(F("Current time: "));
Serial.print(asctime(&timeinfo));
}
WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
if (bootCount == 0) {
bootCount++;
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("xxx", "yyyyy");
Serial.print("Waiting for WiFi to connect...");
while ((WiFiMulti.run() != WL_CONNECTED)) Serial.print(".");
Serial.println(" connected");
setClock();
}
struct tm timeinfo;
time_t now;
time(&now);
localtime_r(&now, &timeinfo);
Serial.print(F("Current time: "));
Serial.print(asctime(&timeinfo));
delay(200); // leave time to the serial monitor to display
uint64_t sleep_time = 5000000; // 5 seconds
esp_sleep_enable_timer_wakeup(sleep_time);
esp_deep_sleep_start();
}
void loop() {}
EDIT: I had it run all night and it now has 2 min 50 sec drift
The board is a LOLIN 32.