I am using an Imp to turns on one of three LEDs based on a percentage. I’m using a ColdFusion server to post the percentage to http.onrequest(). Everything works using this method; however, I want to update the status on a define interval using a function call. I’ve tried using http.get() without luck. Can someone give me a suggestion on how to use http.get(), or another method, to update the device automatically? Running the below http.get() code returns “Invalid JSON” in the server log.
ColdFusion Code:
`
Agent Code:
`
// agent code
responses <- [];
lastResponse <- [];
http.onrequest(function(request,res){
server.log(format(“HTTP incoming: %s”, request.body));
t <- http.urldecode(request.body);
responses.push(res);
device.send(“data”,t.data);
res.send(200, “Status Updated”);
lastResponse = lastResponse;
responses = [];
});
function getStatus() {
response <- http.get(“http://XXXXXXXXXX”).sendsync();
t <- http.jsondecode(response.body);
server.log(t);
device.send(“data”,t.data);
lastResponse = lastResponse;
responses = [];
}
function update(){
getStatus();
imp.wakeup(60.0,update);
}
//update();
//getStatus();
`
Device Code:
`
//Configure GPIO Pins
hardware.pin5.configure(DIGITAL_OUT); //powerPin
hardware.pin7.configure(DIGITAL_OUT); //greenLED
hardware.pin8.configure(DIGITAL_OUT); //blueLED
hardware.pin9.configure(DIGITAL_OUT); //redLED
//show device is starting up and all LEDs work
hardware.pin5.write(1); //power
hardware.pin7.write(1); //greenLED
imp.sleep(1.0);
hardware.pin7.write(0); //greenLED
imp.sleep(1.0);
hardware.pin8.write(1); //blueLED
imp.sleep(1.0);
hardware.pin8.write(0); //blueLED
imp.sleep(1.0);
hardware.pin9.write(1); //redLED
imp.sleep(1.0);
hardware.pin9.write(0); //redLED
hardware.pin5.write(0); //power
agent.on(“data”,function(msg){
server.log(format(“Agent incoming: %s”, msg));
//if status is >= 50% turn on redLED, power and make sure all other LEDs are off
if(msg >= "50"){
//make sure other LEDs are off
hardware.pin7.write(0); //greenLED
hardware.pin8.write(0); //blueLED
//make sure LED power pin is on
hardware.pin5.write(1); //power
//flash LED show we know there was a change
hardware.pin9.write(1); //redLED
imp.sleep(0.01);
hardware.pin9.write(0); //redLED
imp.sleep(0.01);
hardware.pin9.write(1); //redLED
//Update server log and planner with current status
server.log("Status: RED");
server.show("Status: RED");
}
//if status is >= 36% turn on blueLED, power and make sure all other LEDs are off
else if(msg >= "36"){
//make sure other LEDs are off
hardware.pin7.write(0); //greenLED
hardware.pin9.write(0); //redLED
//make sure LED power pin is on
hardware.pin5.write(1); //power
//flash LED show we know there was a change
hardware.pin8.write(1); //blueLED
imp.sleep(0.2);
hardware.pin8.write(0); //blueLED
imp.sleep(0.1);
hardware.pin8.write(1); //blueLED
//Update server log and planner with current status
server.log("Status: BLUE");
server.show("Status: BLUE");
}
//if status is <= 35% turn on greenLED, power and make sure all other LEDs are off
else if(msg <= "35"){
//make sure other LEDs are off
hardware.pin9.write(0); //redLED
hardware.pin8.write(0); //blueLED
//make sure LED power pin is on
hardware.pin5.write(1); //power
//flash LED show we know there was a change
hardware.pin7.write(1); //greenLED
imp.sleep(0.1);
hardware.pin7.write(0); //greenLED
imp.sleep(0.1);
hardware.pin7.write(1); //greenLED
//Update server log and planner with current status
server.log("Status: GREEN");
server.show("Status: GREEN");
}
});
server.show(“Imp Configured”)
imp.configure(“EMS Status”, [], []);
`