Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion multicastUDP_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
93 changes: 59 additions & 34 deletions timezonefunctions.ino
Original file line number Diff line number Diff line change
@@ -1,53 +1,87 @@
#include <ESP8266HTTPClient.h>
#include <AceTime.h> // 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<CACHE_SIZE> 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;
}

/**
Expand All @@ -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);
}
}
1 change: 1 addition & 0 deletions wordclock_esp8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ void loop() {
Serial.println("connection lost");
ledmatrix.gridAddPixel(0, 5, colors24bit[1]);
ledmatrix.drawOnMatrixInstant();
delay(1000);
}
}

Expand Down