From 91fbcb1c8fa20fc499d23db3f15f3412b246426d Mon Sep 17 00:00:00 2001 From: iosullivan Date: Sun, 12 Jul 2026 19:41:44 +1000 Subject: [PATCH] Update examples/src/main.cpp --- examples/src/main.cpp | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/examples/src/main.cpp b/examples/src/main.cpp index 9c3c121..87d0417 100644 --- a/examples/src/main.cpp +++ b/examples/src/main.cpp @@ -2,6 +2,7 @@ #include #include // Include ArduinoJson library #include +#include const char* ssid = "OSULL"; const char* password = "thisispassword"; @@ -16,9 +17,8 @@ unsigned long lastTime = 0; // unsigned long queryTime = 60*1000 unsigned long queryTime = 5*1000; -// Let's just look at Rosemont for now -unsigned int station_num = 40820; -unsigned int station_eta = 999; +// How far away the train (in seconds) should be from the station to trigger +unsigned int time_to_led = 120; void setup() { Serial.begin(115200); @@ -26,9 +26,7 @@ void setup() { while (WiFi.status() != WL_CONNECTED) { delay(1000); - Serial.print("Connecting to WiFi... SSID:"); - Serial.print(ssid); - Serial.println("..."); + Serial.print("Connecting to WiFi..."); } Serial.println("Connected to WiFi!"); @@ -49,38 +47,43 @@ void loop() { http.useHTTP10(true); http.begin(serverPath.c_str()); int httpResponse = http.GET(); - // Minimum size acquired from pasting raw JSON into here: https://arduinojson.org/v5/assistant/ DynamicJsonDocument location_json(9128); deserializeJson(location_json, http.getStream()); // Store time we queried server - const char* time = location_json["ctatt"]["tmst"]; + const char* time_c = location_json["ctatt"]["tmst"]; + struct tm time_tm; + strptime(time_c, "%Y-%m-%dT%H:%M:%S", &time_tm); + time_t time = mktime(&time_tm); Serial.print("Time of query: "); - Serial.println(time); + Serial.println(time_tm.tm_hour); const char* nextStaID = location_json["ctatt"]["route"][0]["train"][0]["nextStaID"]; Serial.println(nextStaID); // Move through the array to find for (JsonObject train : location_json["ctatt"]["route"][0]["train"].as()) { - const char* nextSta = train["nextStaId"]; - unsigned int arrSta = train["arrT"]; + unsigned int nextSta = train["nextStaId"]; + const char* arrTime = train["arrT"]; + // Annoying multistep process to get ISO time into time_t format + struct tm eta_tm; + strptime(arrTime, "%Y-%m-%dT%H:%M:%S", &eta_tm); + time_t eta = mktime(&eta_tm); - String output = String("Next train arriving at ") + nextSta + " at time " + arrSta; - Serial.println(output); + // Save station name for pretty output + const char* stationName = train["nextStaNm"]; + + double difference = difftime(eta, time); - // Only care about one station for now - if(arrSta == station_num) { - // Found Rosemont - // Calculate time difference - time_t arrSta_t = arrSta - // TODO + if(difference < time_to_led) { + String output = String("Train arriving soon at ") + stationName + " in " + difference + " seconds."; + Serial.println(output); } } lastTime = millis(); } -} +} \ No newline at end of file