#include WiFiClient wifiClient; void SetTimeZone(unsigned long currentUtcEpochTime) { const char* timeZoneServer = "api.timezonedb.com"; String timeZoneUrl = "/v2/get-time-zone?key=XXXXXXXXX&format=json&by=zone&zone=Europe/Berlin"; const unsigned long HTTP_TIMEOUT = 10000; // max respone time from server const size_t MAX_CONTENT_SIZE = 2048; // max size of the HTTP response const unsigned long START_YEAR_2036 = 2082754800; //check if time is valid (> 1.1.1970) and before 1.1.2036 (NTP Bug of 7.2.2036) if ((currentUtcEpochTime > (24*3600)) && (currentUtcEpochTime < START_YEAR_2036)) { timeZoneUrl += "&time="; timeZoneUrl += String(currentUtcEpochTime); } if (!wifiClient.connect(timeZoneServer, 80)) { Serial.println("*Wortuhr: Could not connect to time zone server"); wifiClient.stop(); return; } wifiClient.print("GET "); wifiClient.print(timeZoneUrl); wifiClient.println(" HTTP/1.0"); wifiClient.print("Host: "); wifiClient.println(timeZoneServer); wifiClient.println("Connection: close"); wifiClient.println(); // Skip HTTP headers so that we are at the beginning of the response's body // HTTP headers end with an empty line char endOfHeaders[] = "\r\n\r\n"; wifiClient.setTimeout(HTTP_TIMEOUT); bool ok = wifiClient.find(endOfHeaders); if (!ok) { Serial.println("*Wortuhr: No response or invalid response from Timezone server!"); wifiClient.stop(); return; } //Parse JSON response const size_t bufferSize = JSON_OBJECT_SIZE(13) + 260; DynamicJsonBuffer jsonBuffer(bufferSize); //const char* json = "{\"status\":\"OK\",\"message\":\"\",\"countryCode\":\"DE\",\"countryName\":\"Germany\",\"zoneName\":\"Europe/Berlin\",\"abbreviation\":\"CEST\",\"gmtOffset\":7200,\"dst\":\"1\",\"dstStart\":1521939600,\"dstEnd\":1540688399,\"nextAbbreviation\":\"CET\",\"timestamp\":1526505696,\"formatted\":\"2018-05-16 21:21:36\"}"; JsonObject& root = jsonBuffer.parseObject(wifiClient); const char* status = root["status"]; // "OK" //const char* message = root["message"]; // "" //const char* countryCode = root["countryCode"]; // "DE" //const char* countryName = root["countryName"]; // "Germany" //const char* zoneName = root["zoneName"]; // "Europe/Berlin" //const char* abbreviation = root["abbreviation"]; // "CEST" long gmtOffset = root["gmtOffset"]; // 7200 //const char* dst = root["dst"]; // "1" //long dstStart = root["dstStart"]; // 1521939600 long dstEnd = root["dstEnd"]; // 1540688399 //const char* nextAbbreviation = root["nextAbbreviation"]; // "CET" //long timestamp = root["timestamp"]; // 1526505696 //const char* formatted = root["formatted"]; // "2018-05-16 21:21:36" if (String(status) == "OK") { currentTimeZone = gmtOffset; if (dstEnd > 0) timeZoneValidUntil = dstEnd; timeClient.setTimeOffset(currentTimeZone); doTimeRepaint = true; //do a repaint immediately Serial.println("*Wortuhr: Saved time zone: " + String(currentTimeZone) + "s (valid until epoch time: " + String(timeZoneValidUntil) + ")"); } else { Serial.println("*Wortuhr: Timezone json invalid! (Status: '" + String(status) + "'"); } wifiClient.stop(); }