1 contributor
#ifndef _MY_NTP_H
#define _MY_NTP_H
#define TIMEZONE_OFFSET 8 // CET
#define DST_OFFSET 0 // CEST
#define UPDATE_CYCLE (1 * 1000) // every second
static char acTimeString[32];
static char acTimeStringTZ[38];
const char* getTimeString(void) {
time_t now = time(nullptr);
struct tm *ts;
ts = localtime(&now);
strftime(acTimeString, sizeof(acTimeString), "%Y-%m-%d %H:%M:%S", ts);
strftime(acTimeStringTZ, sizeof(acTimeStringTZ), "%Y-%m-%d %H:%M:%S+0800", ts);
return acTimeString;
}
void setClock(void) {
configTime((TIMEZONE_OFFSET * 3600), (DST_OFFSET * 3600), "time.google.com", "pool.ntp.org", "time.nist.gov");
int count = 0;
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr); // Secs since 01.01.1970 (when uninitalized starts with (8 * 3600 = 28800)
while (now < 8 * 3600 * 2) { // Wait for realistic value
delay(500);
Serial.print(".");
now = time(nullptr);
if(count>20) break;
count++;
}
Serial.printf("\nCurrent time: %s\n", getTimeString());
}
#endif //_MY_NTP_H