Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include "JsonParserGeneratorRK.h"

JsonParser parser;

int led1 = D0;


// setup() runs once, when the device is first turned on.
void setup() {
  // Put initialization like pinMode and begin functions here.

  
  ledrun(8,led1);
  

}


void getPAdata(){
    String sData = String(10);
    Particle.publish("bustime",sData,PRIVATE);                              //trigger the WEBHOOK
    Particle.subscribe("hook-response/bustime", myHandler, MY_DEVICES);     //get response
}

bool* makeLedPattern(int numberled, int interv, int val){    //generate a led pattern array
    bool* ledPattern;
    ledPattern = new bool[numberled];
    
    for (int i =0; i < sizeof(ledPattern); i++){   // fill array with false
        ledPattern[i] = false;
    }
    
    if (val > 0){                           //if val > 0, generate a meaningful pattern; else generate an empty pattern
        int nled = val/interv;
        if (nled < numberled){
            ledPattern[nled] = true;
        }
    }
    return ledPattern;
}

bool* sumLedPattern(bool* first, bool* second){           //overlap two led pattern
    for (int i = 0; i< sizeof(first); i++){
        if (first[i] == true || second[i] == true){
            first[i] = true;
        } 
    }
    return first;
}

void processLed(bool* Pattern, int sLedPin){           //pull led on in pattern; Pattern = the led pattern; sLedPin = the first led pin
    for (int i = 0; i< sizeof(Pattern); i++ ){
        pinMode(sLedPin+i,OUTPUT);
        if (Pattern[i] == true){
            digitalWrite(sLedPin+i,HIGH);
        } else {
            digitalWrite(sLedPin+i,LOW);
        }
    }    
}

void processData() {
    
//Particle.publish("showbustime",printJson(jsdata),PRIVATE);

  int nBus = parser.getReference().key("bustime-response").key("prd").size();
  String snBus = String (nBus);
  snBus = "Number of Bus:"+snBus;
  Particle.publish("debugbustime",snBus,PRIVATE);
  //delay(300);
  
  bool* resltLedPattern;
  resltLedPattern = makeLedPattern(8,5,-1); // generated an emplty pattern;
  
  if (nBus > 0) {                               // when number of bus > 0
  for(int i = 0; i < nBus; i++){
    String rtBus = parser.getReference().key("bustime-response").key("prd").index(i).key("rt").valueString();
    int prdctdnBus = parser.getReference().key("bustime-response").key("prd").index(i).key("prdctdn").valueInt();
    
    Particle.publish("showbustime","rtBus:"+rtBus+"; prdBus:"+String(prdctdnBus)+";",PRIVATE);
    
    bool* businfo;                                                          //generate pattern bus by bus
    businfo = makeLedPattern(8,5,prdctdnBus);
    resltLedPattern = sumLedPattern(resltLedPattern,businfo);               //overlay all pattern
  //  delay(300);
  }
  }
  processLed(resltLedPattern,led1);                 //light LED
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
  int responseIndex = 0;

//    Particle.publish("debugbustime","Event Name: "+String(event),PRIVATE);
    String sevent = String(event);                  
    String sresponseIndex = sevent.substring(sevent.lastIndexOf("/")+1);
    responseIndex = sresponseIndex.toInt();
    
//	Particle.publish("debugbustime","Current Chunk Number: "+String(responseIndex),PRIVATE);

	if (responseIndex == 0) {
		parser.clear();
	}
	parser.addString(data);

	if (parser.parse()) {
		// Looks valid (we received all parts)
		// This printing thing is just for testing purposes, you should use the commands to
		// process data
		
		Particle.publish("debugbustime","Total Chunk Number: "+String(responseIndex+1),PRIVATE);
		processData();
	}
}

void ledrun(int numLeds, int sLed){  //for led test
    
    
    for(int i=0;i<numLeds;i++){
        pinMode(sLed+i, OUTPUT);
        digitalWrite(sLed+i,HIGH);
        delay(150);
        digitalWrite(sLed+i,LOW);
    }
    
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
    getPAdata();
    delay(1*60*1000); //refresh data per 3 min
    
//    ledrun();
  // The core of your code will likely live here.

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0