#include #include #include // ====== CHANGE THESE ====== const char* ssid = "Bertovic"; const char* password = "18072019"; // Time / NTP const char* ntpServer = "pool.ntp.org"; // Timezone: Croatia (Europe/Zagreb) is usually UTC+1, and UTC+2 in DST. // Simplest fixed offset: set UTC+1 (3600). If you want DST auto, tell me and I’ll give that version. const long gmtOffset_sec = 3600; // UTC+1 const int daylightOffset_sec = 0; // Set 3600 if you want to force DST (not automatic) // Web server WebServer server(80); String formatTimeNow() { struct tm timeinfo; if (!getLocalTime(&timeinfo)) { return String("Time not available (NTP not synced yet)"); } char buf[32]; // YYYY-MM-DD HH:MM:SS strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &timeinfo); return String(buf); } void handleRoot() { // Simple HTML page that fetches /time every second String html = R"rawliteral( ESP32 Time
Current time:
Loading...
Updates every second
)rawliteral"; server.send(200, "text/html", html); } void handleTime() { server.send(200, "text/plain", formatTimeNow()); } void setup() { Serial.begin(115200); // Connect Wi-Fi WiFi.begin(ssid, password); Serial.print("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected!"); Serial.print("ESP32 IP address: "); Serial.println(WiFi.localIP()); // NTP time init configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); // Optional: wait a bit for first sync Serial.println("Syncing time..."); for (int i = 0; i < 15; i++) { if (formatTimeNow().indexOf("not available") == -1) break; delay(500); } Serial.println("Current time: " + formatTimeNow()); // Web routes server.on("/", handleRoot); server.on("/time", handleTime); server.begin(); Serial.println("Web server started. Open the IP in your browser."); } void loop() { server.handleClient(); }