Update examples/src/main.cpp
This commit is contained in:
+22
-19
@@ -2,6 +2,7 @@
|
||||
#include <WiFi.h>
|
||||
#include <ArduinoJson.h> // Include ArduinoJson library
|
||||
#include <HTTPClient.h>
|
||||
#include <time.h>
|
||||
|
||||
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!");
|
||||
@@ -50,34 +48,39 @@ void loop() {
|
||||
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<JsonArray>()) {
|
||||
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"];
|
||||
|
||||
// Only care about one station for now
|
||||
if(arrSta == station_num) {
|
||||
// Found Rosemont
|
||||
// Calculate time difference
|
||||
time_t arrSta_t = arrSta
|
||||
// TODO
|
||||
double difference = difftime(eta, time);
|
||||
|
||||
if(difference < time_to_led) {
|
||||
String output = String("Train arriving soon at ") + stationName + " in " + difference + " seconds.";
|
||||
Serial.println(output);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user