BeOliver

JSON API POST - Oliver

Aug 9th, 2018
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SparkJson.h>
  2. #include <LiquidCrystal.h>
  3.  
  4. LiquidCrystal lcd(5, 4, 3, 2, 1, 0);
  5. unsigned long lastTime = 0;
  6.  
  7. void setup() {
  8.     Serial.begin(9600);
  9.     Particle.subscribe("hook-response/PubTrans", parseData, MY_DEVICES);
  10.     lcd.begin(20, 4);
  11. }
  12.  
  13. void parseData(const char *event, const char *data) {
  14.     lcd.clear();
  15.     lcd.setCursor(0, 0);
  16.     lcd.print("Next buses:");
  17.    
  18.     char *mutableCopy = strdup(data);
  19.  
  20.     int numFound = 0;
  21.  
  22.     char *record = strtok(mutableCopy, ";");
  23.     while(record) {
  24.         if (strncmp(record, "2,", 2) == 0) {
  25.             // Begins with 2,
  26.             if (++numFound <= 3) {
  27.                 Serial.println(&record[2]);
  28.                 lcd.setCursor(0, numFound);
  29.                 lcd.print(&record[2]);
  30.             }
  31.         }
  32.         record = strtok(NULL, ";");
  33.     }
  34.  
  35.     free(mutableCopy); //not showing all 3 times but just the next time??
  36. }
  37.  
  38. void loop() {
  39. unsigned long now = millis();
  40.     if ((now - lastTime) >= 60000) {
  41.         String data = String(10);
  42.  
  43.         Particle.publish("PubTrans", data, PRIVATE);
  44.        
  45.         lcd.setCursor(15, 3);
  46.         Serial.println(Time.now());
  47.        
  48.         lastTime = now;
  49.   } // End 1-minute
  50. }   // End Loop
RAW Paste Data Copied