Update examples/src/main.cpp

This commit is contained in:
2026-07-12 18:31:31 +10:00
parent 2c8a4664a4
commit 26e166c8dc
+86
View File
@@ -0,0 +1,86 @@
#include <Arduino.h>
#include <WiFi.h>
#include <ArduinoJson.h> // Include ArduinoJson library
#include <HTTPClient.h>
const char* ssid = "OSULL";
const char* password = "thisispassword";
const char* APIKey = "f4b7aff25779483bba8cac91900f6efc";
// Use HTTP instead of HTTPS for a bit more simplicity
String serverName = "http://lapi.transitchicago.com/api/1.0/ttpositions.aspx";
// Last run time for the loop, don't need to query too often, stored in milliseconds
unsigned long lastTime = 0;
// How often to update table (every 60 seconds)
// 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;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print("Connecting to WiFi... SSID:");
Serial.print(ssid);
Serial.println("...");
}
Serial.println("Connected to WiFi!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Only run if we haven't run in queryTime
if ((millis() - lastTime) > queryTime) {
// Query the HTTP endpoint with a POST request and parameters, only interested in Blue route for now
const char* route = "Blue";
// http object let's us query a webserver
HTTPClient http;
String serverPath = serverName + "?key=" + APIKey + "&rt=" + route + "&outputType=JSON";
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"];
Serial.print("Time of query: ");
Serial.println(time);
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<JsonArray>()) {
const char* nextSta = train["nextStaId"];
unsigned int arrSta = train["arrT"];
String output = String("Next train arriving at ") + nextSta + " at time " + arrSta;
Serial.println(output);
// Only care about one station for now
if(arrSta == station_num) {
// Found Rosemont
// Calculate time difference
time_t arrSta_t = arrSta
// TODO
}
}
lastTime = millis();
}
}