Compare commits

6 Commits

Author SHA1 Message Date
iosullivan 91fbcb1c8f Update examples/src/main.cpp 2026-07-12 19:41:44 +10:00
iosullivan dfd8fbb976 Update README.md 2026-07-12 19:15:19 +10:00
iosullivan 26e166c8dc Update examples/src/main.cpp 2026-07-12 18:31:31 +10:00
iosullivan 2c8a4664a4 Update README.md 2026-07-12 17:36:53 +10:00
iosullivan 0bf2880d36 Update README.md 2026-07-12 17:07:59 +10:00
iosullivan 745445edeb Add examples/src/main.cpp 2026-07-12 16:44:37 +10:00
2 changed files with 95 additions and 5 deletions
+6 -5
View File
@@ -13,7 +13,7 @@ The CTA API can be fairly trivially queries to get a JSON response with the requ
To query XML: https://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=<KEYID>&mapid=<mapID> To query XML: https://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=<KEYID>&mapid=<mapID>
To query JSON: https://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=<KEYID>&mapid=40380&outputType=JSON To query JSON: https://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=<KEYID>&mapid=40380&outputType=JSON
Simple HTTP POST queries use the '?' to define start of parameters, and each parameter is seperated by an '&' Simple HTTP queries use the '?' to define start of parameters, and each parameter is seperated by an '&'
In the above, we are querying the 'ttarrivals' endpoint, used for the Arrivals API In the above, we are querying the 'ttarrivals' endpoint, used for the Arrivals API
KEYID is your API key KEYID is your API key
MAPID is the ID of a station on the CTA, such as "40380". These station numbers are available on the ttdocs website. MAPID is the ID of a station on the CTA, such as "40380". These station numbers are available on the ttdocs website.
@@ -106,9 +106,10 @@ print(stations_eta[40390])
``` ```
### ESP32 Implementation ### ESP32 Implementation
- Micropython appears to still be poorly documented for ESP32s (especially the aliexpress clone boards). - Micropython, Arduino IDE and PlatformIO are the three best options
- Recommendation is to use either the Arduino IDE (for quickly getting going) or using PlatformIO with VS Code. - Easier programming - go with MicroPython
- Simpler setup, use either the Arduino IDE (for quickly getting going) or using PlatformIO with VS Code.
- Both of these are just fancy wrappers around C++ language, so plenty of documentation to google. - Both of these are just fancy wrappers around C++ language, so plenty of documentation to google.
- The latter is harder to initially get going (mostly cause of VS Code), but it's a much nicer environment to code in than Arduino IDE. - The latter is harder to initially get going (mostly cause of VS Code weirdness), but it's a much nicer environment to code in than Arduino IDE.
- PlatformIO is completely compatable with Arduino code, so can always start in Arduino IDE and move from there. - PlatformIO is completely compatable with Arduino code, so can always start in Arduino IDE and move from there.
- Example PlatformIO code for the API is available in example/src/ - Example Arduino IDE code for the API is available in /example/src/
+89
View File
@@ -0,0 +1,89 @@
#include <Arduino.h>
#include <WiFi.h>
#include <ArduinoJson.h> // Include ArduinoJson library
#include <HTTPClient.h>
#include <time.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;
// 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);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print("Connecting to WiFi...");
}
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_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_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<JsonArray>()) {
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);
// Save station name for pretty output
const char* stationName = train["nextStaNm"];
double difference = difftime(eta, time);
if(difference < time_to_led) {
String output = String("Train arriving soon at ") + stationName + " in " + difference + " seconds.";
Serial.println(output);
}
}
lastTime = millis();
}
}