Upload files to "examples/src"
This commit is contained in:
1 parent
742ea3f6c8
commit
91ca4da4ba
1 file changed
+179
-25
+179
-25
@@ -1,28 +1,25 @@
|
|||||||
|
#include "secrets.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#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>
|
#include <time.h>
|
||||||
|
|
||||||
const char* ssid = "<SSID>";
|
|
||||||
const char* password = "<password>";
|
|
||||||
const char* APIKey = "<apikey>";
|
|
||||||
// Use HTTP instead of HTTPS for a bit more simplicity
|
// Use HTTP instead of HTTPS for a bit more simplicity
|
||||||
String serverName = "http://lapi.transitchicago.com/api/1.0/ttpositions.aspx";
|
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
|
// Last run time for the loop, don't need to query too often, stored in milliseconds
|
||||||
unsigned long lastTime = 0;
|
unsigned long lastTime = 0;
|
||||||
// How often to update table (every 60 seconds)
|
// How often to query API server for updates (every 60 seconds)
|
||||||
// unsigned long queryTime = 60*1000
|
unsigned long queryTime = 60*1000;
|
||||||
unsigned long queryTime = 5*1000;
|
|
||||||
|
|
||||||
// How far away the train (in seconds) should be from the station to trigger
|
// How far away the train (in seconds) should be from the station to trigger
|
||||||
unsigned int time_to_led = 120;
|
unsigned int time_to_led = 120;
|
||||||
|
// Current time (last API refresh time)
|
||||||
|
time_t last_api_time;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(SECRET_SSID, SECRET_PASS);
|
||||||
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(1000);
|
delay(1000);
|
||||||
@@ -32,10 +29,163 @@ void setup() {
|
|||||||
Serial.println("Connected to WiFi!");
|
Serial.println("Connected to WiFi!");
|
||||||
Serial.print("IP Address: ");
|
Serial.print("IP Address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
// Setup array of stations and set a starting ETA
|
||||||
|
// This is really horrible in C/C++
|
||||||
|
struct tm def_tm;
|
||||||
|
const char* def_dt = "2199-01-01T01:01:01";
|
||||||
|
strptime(def_dt, "%Y-%m-%dT%H:%M:%S", &def_tm);
|
||||||
|
time_t def_time = mktime(&def_tm);
|
||||||
|
std::unordered_map<int, time_t> stations;
|
||||||
|
stations.insert(std::make_pair(41090, def_time)); //Monroe (Red Line)
|
||||||
|
stations.insert(std::make_pair(40820, def_time)); //Rosemont (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41010, def_time)); //Rockwell (Brown Line)
|
||||||
|
stations.insert(std::make_pair(40470, def_time)); //Racine (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40190, def_time)); //Sox-35th (Red Line)
|
||||||
|
stations.insert(std::make_pair(40020, def_time)); //Harlem/Lake (Green Line)
|
||||||
|
stations.insert(std::make_pair(40900, def_time)); //Howard (Red, Purple & Yellow lines)
|
||||||
|
stations.insert(std::make_pair(41160, def_time)); //Clinton (Green & Pink lines)
|
||||||
|
stations.insert(std::make_pair(40230, def_time)); //Cumberland (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40560, def_time)); //Jackson (Red Line)
|
||||||
|
stations.insert(std::make_pair(41150, def_time)); //Kedzie (Orange Line)
|
||||||
|
stations.insert(std::make_pair(40780, def_time)); //Central Park (Pink Line)
|
||||||
|
stations.insert(std::make_pair(41080, def_time)); //47th (Green Line)
|
||||||
|
stations.insert(std::make_pair(40510, def_time)); //Garfield (Green Line)
|
||||||
|
stations.insert(std::make_pair(41050, def_time)); //Linden (Purple Line)
|
||||||
|
stations.insert(std::make_pair(40120, def_time)); //35th/Archer (Orange Line)
|
||||||
|
stations.insert(std::make_pair(40570, def_time)); //California (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40260, def_time)); //State/Lake (Brown, Green, Orange, Pink & Purple lines)
|
||||||
|
stations.insert(std::make_pair(40130, def_time)); //51st (Green Line)
|
||||||
|
stations.insert(std::make_pair(41700, def_time)); //Washington/Wabash (Brown, Green, Orange, Purple & Pink lines)
|
||||||
|
stations.insert(std::make_pair(41000, def_time)); //Cermak-Chinatown (Red Line)
|
||||||
|
stations.insert(std::make_pair(40340, def_time)); //Berwyn (Red Line)
|
||||||
|
stations.insert(std::make_pair(41690, def_time)); //Cermak-McCormick Place (Green Line)
|
||||||
|
stations.insert(std::make_pair(40490, def_time)); //Grand (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41060, def_time)); //Ashland (Orange Line)
|
||||||
|
stations.insert(std::make_pair(40090, def_time)); //Damen (Brown Line)
|
||||||
|
stations.insert(std::make_pair(41280, def_time)); //Jefferson Park (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40520, def_time)); //Foster (Purple Line)
|
||||||
|
stations.insert(std::make_pair(40330, def_time)); //Grand (Red Line)
|
||||||
|
stations.insert(std::make_pair(40280, def_time)); //Central (Green Line)
|
||||||
|
stations.insert(std::make_pair(40250, def_time)); //Kedzie-Homan (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40870, def_time)); //Francisco (Brown Line)
|
||||||
|
stations.insert(std::make_pair(41240, def_time)); //Addison (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40740, def_time)); //Western (Pink Line)
|
||||||
|
stations.insert(std::make_pair(41360, def_time)); //California (Green Line)
|
||||||
|
stations.insert(std::make_pair(41400, def_time)); //Roosevelt (Red, Orange & Green lines)
|
||||||
|
stations.insert(std::make_pair(41410, def_time)); //Chicago (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40680, def_time)); //Adams/Wabash (Brown, Green, Orange, Pink & Purple lines)
|
||||||
|
stations.insert(std::make_pair(40920, def_time)); //Pulaski (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41210, def_time)); //Wellington (Brown & Purple lines)
|
||||||
|
stations.insert(std::make_pair(41340, def_time)); //LaSalle (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41490, def_time)); //Harrison (Red Line)
|
||||||
|
stations.insert(std::make_pair(41350, def_time)); //Oak Park (Green Line)
|
||||||
|
stations.insert(std::make_pair(40040, def_time)); //Quincy/Wells (Brown, Orange, Purple & Pink lines)
|
||||||
|
stations.insert(std::make_pair(40390, def_time)); //Forest Park (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41130, def_time)); //Halsted (Orange Line)
|
||||||
|
stations.insert(std::make_pair(40960, def_time)); //Pulaski (Orange Line)
|
||||||
|
stations.insert(std::make_pair(40830, def_time)); //18th (Pink Line)
|
||||||
|
stations.insert(std::make_pair(40140, def_time)); //Dempster-Skokie (Yellow Line)
|
||||||
|
stations.insert(std::make_pair(41320, def_time)); //Belmont (Red, Brown & Purple lines)
|
||||||
|
stations.insert(std::make_pair(41660, def_time)); //Lake (Red Line)
|
||||||
|
stations.insert(std::make_pair(40910, def_time)); //63rd (Red Line)
|
||||||
|
stations.insert(std::make_pair(40690, def_time)); //Dempster (Purple Line)
|
||||||
|
stations.insert(std::make_pair(40460, def_time)); //Merchandise Mart (Brown & Purple lines)
|
||||||
|
stations.insert(std::make_pair(40710, def_time)); //Chicago (Brown & Purple lines)
|
||||||
|
stations.insert(std::make_pair(40530, def_time)); //Diversey (Brown & Purple lines)
|
||||||
|
stations.insert(std::make_pair(40850, def_time)); //Harold Washington Library-State/Van Buren (Brown, Orange, Purple & Pink lines)
|
||||||
|
stations.insert(std::make_pair(41020, def_time)); //Logan Square (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41030, def_time)); //Polk (Pink Line)
|
||||||
|
stations.insert(std::make_pair(41300, def_time)); //Loyola (Red Line)
|
||||||
|
stations.insert(std::make_pair(40310, def_time)); //Western (Orange Line)
|
||||||
|
stations.insert(std::make_pair(40430, def_time)); //Clinton (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40320, def_time)); //Division (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41510, def_time)); //Morgan (Green & Pink lines)
|
||||||
|
stations.insert(std::make_pair(40270, def_time)); //Main (Purple Line)
|
||||||
|
stations.insert(std::make_pair(41330, def_time)); //Montrose (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40350, def_time)); //UIC-Halsted (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40940, def_time)); //Halsted (Green Line)
|
||||||
|
stations.insert(std::make_pair(41480, def_time)); //Western (Brown Line)
|
||||||
|
stations.insert(std::make_pair(40030, def_time)); //Pulaski (Green Line)
|
||||||
|
stations.insert(std::make_pair(41190, def_time)); //Jarvis (Red Line)
|
||||||
|
stations.insert(std::make_pair(41170, def_time)); //Garfield (Red Line)
|
||||||
|
stations.insert(std::make_pair(40050, def_time)); //Davis (Purple Line)
|
||||||
|
stations.insert(std::make_pair(40750, def_time)); //Harlem (Blue Line - O'Hare Branch)
|
||||||
|
stations.insert(std::make_pair(41200, def_time)); //Argyle (Red Line)
|
||||||
|
stations.insert(std::make_pair(40440, def_time)); //California (Pink Line)
|
||||||
|
stations.insert(std::make_pair(40360, def_time)); //Southport (Brown Line)
|
||||||
|
stations.insert(std::make_pair(40760, def_time)); //Granville (Red Line)
|
||||||
|
stations.insert(std::make_pair(40480, def_time)); //Cicero (Green Line)
|
||||||
|
stations.insert(std::make_pair(40730, def_time)); //Washington/Wells (Brown, Orange, Purple & Pink lines)
|
||||||
|
stations.insert(std::make_pair(40580, def_time)); //54th/Cermak (Pink Line)
|
||||||
|
stations.insert(std::make_pair(40650, def_time)); //North/Clybourn (Red Line)
|
||||||
|
stations.insert(std::make_pair(40220, def_time)); //Western (Blue Line - Forest Park Branch)
|
||||||
|
stations.insert(std::make_pair(40070, def_time)); //Jackson (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41440, def_time)); //Addison (Brown Line)
|
||||||
|
stations.insert(std::make_pair(41670, def_time)); //Conservatory (Green Line)
|
||||||
|
stations.insert(std::make_pair(40790, def_time)); //Monroe (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40180, def_time)); //Oak Park (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40400, def_time)); //Noyes (Purple Line)
|
||||||
|
stations.insert(std::make_pair(41260, def_time)); //Austin (Green Line)
|
||||||
|
stations.insert(std::make_pair(40600, def_time)); //Kostner (Pink Line)
|
||||||
|
stations.insert(std::make_pair(40670, def_time)); //Western (Blue Line - O'Hare Branch)
|
||||||
|
stations.insert(std::make_pair(40100, def_time)); //Morse (Red Line)
|
||||||
|
stations.insert(std::make_pair(40770, def_time)); //Lawrence (Red Line)
|
||||||
|
stations.insert(std::make_pair(40150, def_time)); //Pulaski (Pink Line)
|
||||||
|
stations.insert(std::make_pair(40660, def_time)); //Armitage (Brown & Purple lines)
|
||||||
|
stations.insert(std::make_pair(40540, def_time)); //Wilson (Red & Purple lines)
|
||||||
|
stations.insert(std::make_pair(40380, def_time)); //Clark/Lake (Blue, Brown, Green, Orange, Purple & Pink lines)
|
||||||
|
stations.insert(std::make_pair(40160, def_time)); //LaSalle/Van Buren (Brown, Orange, Purple & Pink lines)
|
||||||
|
stations.insert(std::make_pair(40700, def_time)); //Laramie (Green Line)
|
||||||
|
stations.insert(std::make_pair(40240, def_time)); //79th (Red Line)
|
||||||
|
stations.insert(std::make_pair(40290, def_time)); //Ashland/63rd (Green Line)
|
||||||
|
stations.insert(std::make_pair(40450, def_time)); //95th/Dan Ryan (Red Line)
|
||||||
|
stations.insert(std::make_pair(40210, def_time)); //Damen (Pink Line)
|
||||||
|
stations.insert(std::make_pair(41140, def_time)); //King Drive (Green Line)
|
||||||
|
stations.insert(std::make_pair(41710, def_time)); //Damen (Green Line)
|
||||||
|
stations.insert(std::make_pair(40980, def_time)); //Harlem (Blue Line - Forest Park Branch)
|
||||||
|
stations.insert(std::make_pair(41270, def_time)); //43rd (Green Line)
|
||||||
|
stations.insert(std::make_pair(40080, def_time)); //Sheridan (Red Line)
|
||||||
|
stations.insert(std::make_pair(40610, def_time)); //Ridgeland (Green Line)
|
||||||
|
stations.insert(std::make_pair(41070, def_time)); //Kedzie (Green Line)
|
||||||
|
stations.insert(std::make_pair(41430, def_time)); //87th (Red Line)
|
||||||
|
stations.insert(std::make_pair(41250, def_time)); //Central (Purple Line)
|
||||||
|
stations.insert(std::make_pair(40880, def_time)); //Thorndale (Red Line)
|
||||||
|
stations.insert(std::make_pair(41680, def_time)); //Oakton-Skokie (Yellow Line)
|
||||||
|
stations.insert(std::make_pair(41120, def_time)); //35th-Bronzeville-IIT (Green Line)
|
||||||
|
stations.insert(std::make_pair(41460, def_time)); //Irving Park (Brown Line)
|
||||||
|
stations.insert(std::make_pair(41220, def_time)); //Fullerton (Red, Brown & Purple lines)
|
||||||
|
stations.insert(std::make_pair(40990, def_time)); //69th (Red Line)
|
||||||
|
stations.insert(std::make_pair(41040, def_time)); //Kedzie (Pink Line)
|
||||||
|
stations.insert(std::make_pair(40800, def_time)); //Sedgwick (Brown & Purple lines)
|
||||||
|
stations.insert(std::make_pair(40060, def_time)); //Belmont (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40840, def_time)); //South Boulevard (Purple Line)
|
||||||
|
stations.insert(std::make_pair(40170, def_time)); //Ashland (Green & Pink lines)
|
||||||
|
stations.insert(std::make_pair(40810, def_time)); //Illinois Medical District (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41230, def_time)); //47th (Red Line)
|
||||||
|
stations.insert(std::make_pair(41500, def_time)); //Montrose (Brown Line)
|
||||||
|
stations.insert(std::make_pair(40930, def_time)); //Midway (Orange Line)
|
||||||
|
stations.insert(std::make_pair(40550, def_time)); //Irving Park (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40370, def_time)); //Washington (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40890, def_time)); //O'Hare (Blue Line)
|
||||||
|
stations.insert(std::make_pair(40630, def_time)); //Clark/Division (Red Line)
|
||||||
|
stations.insert(std::make_pair(41420, def_time)); //Addison (Red Line)
|
||||||
|
stations.insert(std::make_pair(41290, def_time)); //Kimball (Brown Line)
|
||||||
|
stations.insert(std::make_pair(40300, def_time)); //Indiana (Green Line)
|
||||||
|
stations.insert(std::make_pair(40420, def_time)); //Cicero (Pink Line)
|
||||||
|
stations.insert(std::make_pair(40720, def_time)); //Cottage Grove (Green Line)
|
||||||
|
stations.insert(std::make_pair(41380, def_time)); //Bryn Mawr (Red Line)
|
||||||
|
stations.insert(std::make_pair(41450, def_time)); //Chicago (Red Line)
|
||||||
|
stations.insert(std::make_pair(40590, def_time)); //Damen (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41180, def_time)); //Kedzie (Brown Line)
|
||||||
|
stations.insert(std::make_pair(40010, def_time)); //Austin (Blue Line)
|
||||||
|
stations.insert(std::make_pair(41310, def_time)); //Paulina (Brown Line)
|
||||||
|
stations.insert(std::make_pair(40970, def_time)); //Cicero (Blue Line)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// Only run if we haven't run in queryTime
|
// It's been at least 'queryTime' seconds since we last checked the API. Update the arrival times.
|
||||||
if ((millis() - lastTime) > queryTime) {
|
if ((millis() - lastTime) > queryTime) {
|
||||||
// Query the HTTP endpoint with a POST request and parameters, only interested in Blue route for now
|
// Query the HTTP endpoint with a POST request and parameters, only interested in Blue route for now
|
||||||
const char* route = "Blue";
|
const char* route = "Blue";
|
||||||
@@ -43,7 +193,7 @@ void loop() {
|
|||||||
// http object let's us query a webserver
|
// http object let's us query a webserver
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
|
|
||||||
String serverPath = serverName + "?key=" + APIKey + "&rt=" + route + "&outputType=JSON";
|
String serverPath = serverName + "?key=" + SECRET_APIKEY + "&rt=" + route + "&outputType=JSON";
|
||||||
http.useHTTP10(true);
|
http.useHTTP10(true);
|
||||||
http.begin(serverPath.c_str());
|
http.begin(serverPath.c_str());
|
||||||
int httpResponse = http.GET();
|
int httpResponse = http.GET();
|
||||||
@@ -56,34 +206,38 @@ void loop() {
|
|||||||
const char* time_c = location_json["ctatt"]["tmst"];
|
const char* time_c = location_json["ctatt"]["tmst"];
|
||||||
struct tm time_tm;
|
struct tm time_tm;
|
||||||
strptime(time_c, "%Y-%m-%dT%H:%M:%S", &time_tm);
|
strptime(time_c, "%Y-%m-%dT%H:%M:%S", &time_tm);
|
||||||
time_t time = mktime(&time_tm);
|
last_api_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"];
|
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>()) {
|
||||||
unsigned int nextSta = train["nextStaId"];
|
// Save station ID
|
||||||
|
unsigned int station = train["nextStaId"];
|
||||||
const char* arrTime = train["arrT"];
|
const char* arrTime = train["arrT"];
|
||||||
// Annoying multistep process to get ISO time into time_t format
|
// Annoying multistep process to get ISO time into time_t format
|
||||||
struct tm eta_tm;
|
struct tm eta_tm;
|
||||||
strptime(arrTime, "%Y-%m-%dT%H:%M:%S", &eta_tm);
|
strptime(arrTime, "%Y-%m-%dT%H:%M:%S", &eta_tm);
|
||||||
time_t eta = mktime(&eta_tm);
|
|
||||||
|
|
||||||
// Save station name for pretty output
|
// Update next ETA for this station
|
||||||
const char* stationName = train["nextStaNm"];
|
stations[station] = mktime(&eta_tm);
|
||||||
|
|
||||||
double difference = difftime(eta, time);
|
Serial.println(stations[station])
|
||||||
|
|
||||||
if(difference < time_to_led) {
|
|
||||||
String output = String("Train arriving soon at ") + stationName + " in " + difference + " seconds.";
|
|
||||||
Serial.println(output);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lastTime = millis();
|
lastTime = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
double difference = difftime(stations[station], last_api_time);
|
||||||
|
|
||||||
|
if(difference < time_to_led) {
|
||||||
|
String output = String("Train arriving soon at ") + stationName + " in " + difference + " seconds.";
|
||||||
|
Serial.println(output);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
delay(5000)
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user