Update examples/src/main.cpp

This commit is contained in:
2026-07-12 19:41:44 +10:00
parent dfd8fbb976
commit 91fbcb1c8f
+22 -19
View File
@@ -2,6 +2,7 @@
#include <WiFi.h> #include <WiFi.h>
#include <ArduinoJson.h> // Include ArduinoJson library #include <ArduinoJson.h> // Include ArduinoJson library
#include <HTTPClient.h> #include <HTTPClient.h>
#include <time.h>
const char* ssid = "OSULL"; const char* ssid = "OSULL";
const char* password = "thisispassword"; const char* password = "thisispassword";
@@ -16,9 +17,8 @@ unsigned long lastTime = 0;
// unsigned long queryTime = 60*1000 // unsigned long queryTime = 60*1000
unsigned long queryTime = 5*1000; unsigned long queryTime = 5*1000;
// Let's just look at Rosemont for now // How far away the train (in seconds) should be from the station to trigger
unsigned int station_num = 40820; unsigned int time_to_led = 120;
unsigned int station_eta = 999;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
@@ -26,9 +26,7 @@ void setup() {
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(1000); delay(1000);
Serial.print("Connecting to WiFi... SSID:"); Serial.print("Connecting to WiFi...");
Serial.print(ssid);
Serial.println("...");
} }
Serial.println("Connected to WiFi!"); Serial.println("Connected to WiFi!");
@@ -50,34 +48,39 @@ void loop() {
http.begin(serverPath.c_str()); http.begin(serverPath.c_str());
int httpResponse = http.GET(); int httpResponse = http.GET();
// Minimum size acquired from pasting raw JSON into here: https://arduinojson.org/v5/assistant/ // Minimum size acquired from pasting raw JSON into here: https://arduinojson.org/v5/assistant/
DynamicJsonDocument location_json(9128); DynamicJsonDocument location_json(9128);
deserializeJson(location_json, http.getStream()); deserializeJson(location_json, http.getStream());
// Store time we queried server // 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.print("Time of query: ");
Serial.println(time); Serial.println(time_tm.tm_hour);
const char* nextStaID = location_json["ctatt"]["route"][0]["train"][0]["nextStaID"]; const char* nextStaID = location_json["ctatt"]["route"][0]["train"][0]["nextStaID"];
Serial.println(nextStaID); Serial.println(nextStaID);
// Move through the array to find // Move through the array to find
for (JsonObject train : location_json["ctatt"]["route"][0]["train"].as<JsonArray>()) { for (JsonObject train : location_json["ctatt"]["route"][0]["train"].as<JsonArray>()) {
const char* nextSta = train["nextStaId"]; unsigned int nextSta = train["nextStaId"];
unsigned int arrSta = train["arrT"]; 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; // Save station name for pretty output
Serial.println(output); const char* stationName = train["nextStaNm"];
// Only care about one station for now double difference = difftime(eta, time);
if(arrSta == station_num) {
// Found Rosemont if(difference < time_to_led) {
// Calculate time difference String output = String("Train arriving soon at ") + stationName + " in " + difference + " seconds.";
time_t arrSta_t = arrSta Serial.println(output);
// TODO
} }
} }