// RTC demo for ESP32, that includes TZ and DST adjustments
// Get the POSIX style TZ format string from https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
// Created by Hardy Maxa
// Complete project details at: https://RandomNerdTutorials.com/esp32-ntp-timezones-daylight-saving/
#include <WiFi.h>
#include "time.h"
const char * ssid="Wokwi-GUEST";
const char * wifipw="";
void initTime(){
struct tm timeinfo;
Serial.println("Setting up time");
configTime(0, 0, "pool.ntp.org"); // First connect to NTP server, with 0 TZ offset
if(!getLocalTime(&timeinfo)){
Serial.println(" Failed to obtain time");
return;
}
Serial.println(" Got the time from NTP");
setenv("TZ","EET-2EEST,M3.5.0/3,M10.5.0/4",1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
tzset();
}
void printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time 1");
return;
}
Serial.println(String(timeinfo.tm_mday)+"/"+String(timeinfo.tm_mon+1)+"/"+String(timeinfo.tm_year-123));
Serial.println(String(timeinfo.tm_hour)+":"+String(timeinfo.tm_min)+":"+String(timeinfo.tm_sec));
}
void startWifi(){
WiFi.begin(ssid, wifipw);
Serial.println("Connecting Wifi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("Wifi RSSI=");
Serial.println(WiFi.RSSI());
}
void setup(){
Serial.begin(115200);
Serial.setDebugOutput(true);
startWifi();
initTime();
printLocalTime();
}
void loop() {
int i;
// put your main code here, to run repeatedly:
Serial.println("Lets show the time for a bit. ");
for(i=0; i<10; i++){
delay(1000);
printLocalTime();
}
Serial.println();
}