// 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"
#include <ArduinoJson.h>
#include <HTTPClient.h>
const char * ssid="REPLACE_WITH_YOUR_SSID";
const char * wifipw="REPLACE_WITH_YOUR_PASSWORD";
int thehourtz;
int thehourgmt;
int sunrisehour;
int sunsethour;
struct tm tsunrise = {0};
struct tm tsunset = {0};
struct tm timeinfo;
void setTimezone(String timezonestr){
Serial.printf(" Setting Timezone to %s\n",timezonestr.c_str());
setenv("TZ",timezonestr.c_str(),1); // Now adjust the TZ.
tzset();
//Serial.print(timezone);
//printf("Timezone offset from UTC: %ld seconds\n", timezone);
}
void initTime(String timezonestr){
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");
// Now we can set the real timezone
setTimezone(timezonestr);
}
void printLocalTime(){
struct tm timeinfo;
configTime(0, 0, "pool.ntp.org");
setTimezone("EST5EDT,M3.2.0,M11.1.0");
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time 1");
return;
}
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");
thehourtz = timeinfo.tm_hour;
//Serial.print(timeinfo.tm_gmtoff);
Serial.println(thehourtz);
setTimezone("GMT0");
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time 1");
return;
}
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");
thehourgmt = timeinfo.tm_hour;
//Serial.print(timeinfo.tm_gmtoff);
Serial.println(thehourgmt);
}
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);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
//setTimezone("EST5EDT,M3.2.0,M11.1.0"); // Set for New York/AU
HTTPClient http;
String serverPath = "https://api.sunrise-sunset.org/json?lat=42.186630&lng=-71.305450&formatted=0";
// Your Domain name with URL path or IP address with path
http.begin(serverPath.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
DynamicJsonDocument doc(1024);
DeserializationError err = deserializeJson(doc, payload);
if (err) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(err.f_str());
}else{
//2023-03-16T22:53:48+00:00
const char* sunrise = doc["results"]["sunrise"];
strptime(sunrise,"%Y-%m-%dT%H:%M:%S%z",&tsunrise);
Serial.println(sunrise);
Serial.println(tsunrise.tm_hour);
const char* sunset = doc["results"]["sunset"];
strptime(sunset,"%Y-%m-%dT%H:%M:%S%z",&tsunset);
Serial.println(sunset);
Serial.println(tsunset.tm_hour);
}
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
printLocalTime();
}
void loop() {
int i;
// put your main code here, to run repeatedly:
//delay(1500);
//printLocalTime();
// Now lets watch the time and see how long it takes for NTP to fix the clock
}