Http.get() Help

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:
`

#jsondata# `

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”, [], []);
`

Can you post the result of curling the coldfusion URL? Would be good to see what it’s actually sending back.

ie post the result of curl -v http://yourserver.com

I got it working using:

<cfhttp method="Post" url="https://agent.electricimp.com/XXXXXXXX"> <cfhttpparam type="formField" value="#is_red#" name="data"> </cfhttp>

It appears I’m still not doing something right with the GET function. This is what I get back in log when I output the GET:

`
Fri May 03 2013 10:02:53 GMT-0400 (Eastern Daylight Time): Agent incoming:

Update Status Current Status (serializedJSON): 40.0 `

It looks like it’s working because the cfhttp is posting to the http.onrequest function in the agent. I believe the problem is on the ColdFusion server side; however, this getStatus() function is working:

`
function getStatus() {
server.log(“getStatus Function Called”);
response <- http.get(“http://XXXXXXXXX”).sendsync();
server.log(“GET request send to CF server”);
}

function update(){
server.log(“update Function Called”);
getStatus();
imp.wakeup(300.0,update);
}
update();
`

Yeah, so your server is sending back HTML to the imp and not json… that’d be your problem!