Created
May 20, 2019 20:01
-
-
Save mrzachxu/e566d7bf3ecdc19c090e55ef6eeba4c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Particle IDE. | |
#include <Adafruit_HX8357.h> | |
Adafruit_HX8357 tft = Adafruit_HX8357(D6, D7, D5, D3, -1, D4); | |
// called once on startup | |
void setup() { | |
// For simplicity, we'll format our data as text, and pipe it to serial. | |
// but you could just as easily display it in a webpage or pass the data to another system. | |
// Learn more about the serial commands at https://docs.particle.io/reference/firmware/photon/#serial | |
// for the Photon, or https://docs.particle.io/reference/firmware/core/#serial for the Core | |
// You can also watch what's sent over serial with the particle cli with | |
// particle serial monitor | |
//Serial.begin(115200); | |
tft.begin(HX8357D); | |
tft.setRotation(1); | |
// RGB.control(true); | |
// pinMode(D1, OUTPUT); | |
// Lets listen for the hook response | |
Particle.subscribe("hook-response/get_bartdepart", gotBartData, MY_DEVICES); | |
// Lets give ourselves 10 seconds before we actually start the program. | |
// That will just give us a chance to open the serial monitor before the program sends the request | |
for(int i=0;i<5;i++) { | |
tft.println("waiting " + String(5-i) + " seconds before we publish"); | |
delay(1000); | |
} | |
for(int i=0;i<5;i++) { | |
Serial.println("waiting " + String(5-i) + " seconds before we publish"); | |
tft.fillScreen(HX8357_BLACK); | |
tft.setCursor(0, 0); | |
tft.setTextSize(2); | |
tft.println("waiting " + String(5-i) + " seconds before we publish"); | |
delay(1000); | |
} | |
} | |
// called forever really fast | |
void loop() { | |
for(int i=0;i<5;i++) { | |
tft.fillScreen(HX8357_BLACK); | |
tft.setCursor(0, 0); | |
tft.setTextSize(2); | |
delay(1000); | |
} | |
// Let's request the train data, but no more than once every 60 seconds. | |
tft.println("Requesting Millbrae Bart Northbound Departure Status!"); | |
// publish the event that will trigger our Webhook | |
Particle.publish("get_bartdepart"); | |
// and wait at least 60 seconds before doing it again | |
delay(60000); | |
} | |
// This function will get called when train data comes in | |
void gotBartData(const char *name, const char *data) { | |
// Important note! -- Right now the response comes in 512 byte chunks. | |
// This code assumes we're getting the response in large chunks, and this | |
// assumption breaks down if a line happens to be split across response chunks. | |
String str = String(data); | |
String minStr = tryExtractString(str, "<minutes>" ,"</minutes>" ); //extract the min before a train leaves | |
tft.println("Min to depart: " + minStr); | |
tft.println(); | |
} | |
z | |
// Returns any text found between a start and end string inside 'str' | |
// example: startfooend -> returns foo | |
String tryExtractString(String str, const char* start, const char* end) { | |
if (str == NULL) { | |
return ""; | |
} | |
int idx = str.indexOf(start); | |
if (idx < 0) { | |
return ""; | |
} | |
int endIdx = str.indexOf(end); | |
if (endIdx < 0) { | |
return ""; | |
} | |
return str.substring(idx + strlen(start), endIdx); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment