diff --git a/README.md b/README.md index 9025eb1..e5f163a 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,6 @@ Please download all these libraries as ZIP from GitHub, and extract them in the - https://github.com/adafruit/Adafruit_NeoPixel - https://github.com/tzapu/WiFiManager - https://github.com/adafruit/Adafruit_BusIO -- https://github.com/bxparks/AceTime You can als install these libraries via the library manager in the Arduino IDE. diff --git a/multicastUDP_receiver.py b/multicastUDP_receiver.py index a5fea57..5941e7c 100644 --- a/multicastUDP_receiver.py +++ b/multicastUDP_receiver.py @@ -39,11 +39,11 @@ def start(filters=None): data, address = sock.recvfrom(1024) data_str = data.decode("utf-8").strip() timestamped_data = f"[{address[0]} - {datetime.now().strftime('%b-%d-%Y_%H:%M:%S')}] {data_str}" - print(timestamped_data) # Check each filter and process data accordingly for filter_val in filters: if filter_val in data_str: + print(timestamped_data) buffers[filter_val].put(timestamped_data) if buffers[filter_val].full(): buffers[filter_val].get() diff --git a/timezonefunctions.ino b/timezonefunctions.ino index c2d6422..cce7b51 100644 --- a/timezonefunctions.ino +++ b/timezonefunctions.ino @@ -1,53 +1,87 @@ #include -#include // https://github.com/bxparks/AceTime #include "ntp_client_plus.h" #include "udplogger.h" -using namespace ace_time; - -static const int CACHE_SIZE = 1; -static ExtendedZoneProcessorCache zoneProcessorCache; -static ExtendedZoneManager manager( - zonedbx::kZoneAndLinkRegistrySize, - zonedbx::kZoneAndLinkRegistry, - zoneProcessorCache); +int api_offset = 0; +String api_timezone = ""; +float api_lat = 0.0; +float api_lon = 0.0; /** - * @brief Request the timezone as string from the IP-API + * @brief Request the timezone and other data from the IP-API * * @param logger UDPLogger object to log messages - * @return String timezone string + * @return bool true if the api request was successful */ -String getTimeZoneString(UDPLogger &logger) { +bool requestAPIData(UDPLogger &logger) { WiFiClient client; HTTPClient http; + bool res = false; logger.logString("[HTTP] Requesting timezone from IP-API"); - if (http.begin(client, "http://ip-api.com/json/")) { + // see API documentation on https://ip-api.com/docs/api:json to see which fields are available + if (http.begin(client, "http://ip-api.com/json/?fields=status,message,country,countryCode,region,regionName,city,zip,lat,lon,timezone,offset,query")) { int httpCode = http.GET(); if (httpCode > 0) { if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { String payload = http.getString(); - int tzIndex = payload.indexOf("\"timezone\":\""); - if (tzIndex != -1) { - int tzStart = tzIndex + 12; - int tzEnd = payload.indexOf("\"", tzStart); - String timezone = payload.substring(tzStart, tzEnd); - logger.logString("[HTTP] Received timezone: " + timezone); - return timezone; - } + api_timezone = getJsonParameterValue(payload, "timezone", true); + logger.logString("[HTTP] Received timezone: " + api_timezone); + + String offsetString = getJsonParameterValue(payload, "offset", false); + api_offset = offsetString.toInt() / 60; + logger.logString("[HTTP] Received offset (min): " + String(api_offset)); + + String latString = getJsonParameterValue(payload, "lat", false); + api_lat = latString.toFloat(); + logger.logString("[HTTP] Received latitude: " + String(api_lat)); + + String lonString = getJsonParameterValue(payload, "lon", false); + api_lon = lonString.toFloat(); + logger.logString("[HTTP] Received longitude: " + String(api_lon)); + } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + res = false; } http.end(); // Close connection } else { logger.logString("[HTTP] Unable to connect"); + res = false; } - return ""; + return res; +} + +/** + * @brief Get the Json Parameter Value object + * + * @param json + * @param parameter + * @return String + */ +String getJsonParameterValue(String json, String parameter, bool isString) { + String value = ""; + if(isString) { + int index = json.indexOf("\"" + parameter + "\":\""); + if (index != -1) { + int start = index + parameter.length() + 4; + int end = json.indexOf("\"", start); + value = json.substring(start, end); + } + } + else { + int index = json.indexOf("\"" + parameter + "\":"); + if (index != -1) { + int start = index + parameter.length() + 3; + int end = json.indexOf(",", start); + value = json.substring(start, end); + } + } + return value; } /** @@ -58,17 +92,8 @@ String getTimeZoneString(UDPLogger &logger) { * @return int */ void updateUTCOffsetFromTimezoneAPI(UDPLogger &logger, NTPClientPlus &ntp) { - String timezone = getTimeZoneString(logger); - if (timezone.length() > 0) { - uint16_t zone_index = manager.indexForZoneName(timezone.c_str()); - if (zone_index != ZoneManager::kInvalidIndex) { - ExtendedZone zone = manager.getZoneForIndex(zone_index); - int offset = zone.stdOffset().toMinutes(); - logger.logString("[ZoneManager] Timezone offset (min): " + String(offset)); - ntp.setUTCOffset(offset); - } - else { - logger.logString("[ZoneManager] Error: Could not find time_zone value in DB. Use hardcoded UTC offset."); - } + bool res = requestAPIData(logger); + if (res) { + ntp.setUTCOffset(api_offset); } } \ No newline at end of file diff --git a/wordclock_esp8266.ino b/wordclock_esp8266.ino index 12d0dd9..5369da9 100644 --- a/wordclock_esp8266.ino +++ b/wordclock_esp8266.ino @@ -417,6 +417,7 @@ void loop() { Serial.println("connection lost"); ledmatrix.gridAddPixel(0, 5, colors24bit[1]); ledmatrix.drawOnMatrixInstant(); + delay(1000); } }