/*
Forum: https://forum.arduino.cc/t/old-working-code-no-longer-compiles/1448406
Wokwi: https://wokwi.com/projects/466814814856110081
Definition of struct tm:
Member Type Meaning Range
tm_sec int seconds after the minute 0-61*
tm_min int minutes after the hour 0-59
tm_hour int hours since midnight 0-23
tm_mday int day of the month 1-31
tm_mon int months since January 0-11
tm_year int years since 1900
tm_wday int days since Sunday 0-6
tm_yday int days since January 1 0-365
tm_isdst int Daylight Saving Time flag
(source: https://github.com/SensorsIot/NTP-time-for-ESP8266-and-ESP32/blob/master/NTP_Example/NTP_Example.ino)
Simple Example how to synchronize the ESP32 time with a NTP Server
2026/06/14
ec2021
*/
#include <WiFi.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
/** includes, constants and variables for NTP **/
#include "time.h"
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600 * 2;
const int daylightOffset_sec = 3600 * 0;
struct tm timeinfo;
/**********************************************/
int lastSecond = -1;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("\nConnecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("\nConnected to the WiFi network");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
/** Setup for NTP Server **/
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
/********************************************************/
}
void loop() {
if (getLocalTime(&timeinfo)) {
if (timeinfo.tm_sec != lastSecond) {
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
lastSecond = timeinfo.tm_sec;
}
} else {
Serial.println("Failed to obtain time");
};
}