Request.query.check

I have this code running on the agent:
// Log the URLs we need
server.log("check: " + http.agenturl() + “?check”);
function requestHandler(request, response) {

try {
// convert the check query parameter to an integer
local chkdoor = request.query.check;

// send “check” message to device, and send check as the data
device.send(“check”, chkdoor);
response.header(“Status”, state);
//}
// }
// send a response back saying everything was OK.
response.send(200, “OK 200”);
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);

function dealwithit(data){
local state = data;
server.log(data);
return state;
}

device.on(“status”, dealwithit);

and this code running on the device:
sensor <- hardware.pin9;

function displayStatus () {

local state = sensor.read();
if (state == 1){
    local now = date();
    server.log("Opened!");
}
else if(state == 0){
    server.log("Closed!");
}
agent.send("status", state);

}

sensor.configure(DIGITAL_IN_PULLDOWN, displayStatus)

I want to call a get request through php and display the state on a html page. Here is the php code:

<?php $response = get_web_page("http://agent.electricimp.com/9gij0qZlzR-c"); $resArr = array(); $resArr = json_decode($response); echo " ``` "; print_r($response); echo " ``` "; function get_web_page($url) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_MAXREDIRS => 10, // stop after 10 redirects CURLOPT_ENCODING => "", // handle compressed CURLOPT_USERAGENT => "test", // name of client CURLOPT_AUTOREFERER => true, // set referrer on redirect CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect CURLOPT_TIMEOUT => 120, // time-out on response ); $ch = curl_init($url); curl_setopt_array($ch, $options); $content = curl_exec($ch); curl_close($ch); return $content; } ?>

When I run this code i get the following error on the html page: Internal Server Error: the index ‘check’ does not exist (line 7).
Any idea as to why?

Is “check” one of your query parameters? If not, this will certainly fail. You can test for the presence of “check” with:

local chkdoor = (“check” in request.query)?request.query.check:null;

This will set chkdoor to null if “check” is not a query parameter.

There is another reason why your code will not work.

device.send() is not synchronous. There could be many seconds before your device receives the message and responds to it.

After you execute device.send you need to save the http response object until you get an update from the device. Your handler “dealwithit” can’t return a value, but you can send the status using your saved response object.

Check out this page for more help.
Have a look at the many examples on the Electric Imp website to see how messaging between the agent and device is handled. Making it fail-safe means more code, to deal with all the permutations, but that’s not necessary to get the simplest thing working.

Okay, Sorry I am a newbie at the imp. How do I save the response object? And apparently check was not one of the parameters because it is null when I test for the presence using your line of code.

Just kidding I figured out how to save it! Sorry, its been a long night. This is my code for the agent now:

// Log the URLs we need
server.log("check: " + http.agenturl() + “?check”);
function requestHandler(request, response) {

try {
// convert the check query parameter to an integer
local chkdoor = (“check” in request.query)?request.query.check:null;
//local chkdoor = request.query.check;

// send “check” message to device, and send check as the data
device.send(“check”, chkdoor);
local response = response;
server.log(“ahhhh” +response);

//}
// }
response.header(“Status”, response);
// send a response back saying everything was OK.
response.send(200, “OK 200”);
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);

function dealwithit(data){
local state = data;
return state;
}

device.on(“status”, dealwithit);

My html code prints out the response from the request so I got OK 200. But what i really want it to try and access the header. response.header does not accomplish this. Any thoughts?

This code should work. You can’t call response.send from within requestHandler if you haven’t yet received a response from the device. Thus, put response.send in your handler (dealwithit) which is called when you hear back from the device.

`
// Log the URLs we need
server.log("check: " + http.agenturl() + “?check”);

// this will hold the HTTP response until we have heard from the device
savedResponse <- null;

function requestHandler(request, response) {

try {
// convert the check query parameter to an integer
local chkdoor = (“check” in request.query)?request.query.check:null;

savedResponse = response;

// send "check" message to device, and send check as the data
device.send("check", chkdoor); 

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);

function dealwithit(data){
savedResponse.header(“Status”,data);
savedResponse.send(200,“OK 200”);
}

device.on(“status”, dealwithit);
`

BTW, you can display your code better using the code markup. Click on the button labelled ‘C’ above the edit window.

Are you ready to learn about a really cool library!!

We made this framework called Bullwinkle that’s designed to help with asynchronous agent/device communication and handles things like replies, timeouts, and more!!

Here’s what the code looks like using Bullwinkle… you’ll need to fill in the gaps on the device side obviously :slight_smile:

Device Code:

`
#require "bullwinkle.class.nut:2.0.0"
bull <- Bullwinkle();

function check() {
// Replace this code with however you do the check…

// This value will be passed back to the agent, and returned in the HTTP Response
local result = true;

return result;

}

bull.on(“check”, function(msg, reply) {
// Do the check
local result = check();
// Pass the result back to the agent
reply(result);
});

server.log(“Started”);`

Agent Code:

`
#require "bullwinkle.class.nut:2.0.0"
bull <- Bullwinkle();

function requestHandler(request, response) {
try {
// convert the check query parameter to an integer
local chkdoor = (“check” in request.query) ? request.query.check : null;

// send "check" message to device, and send check as the data
bull.send("check", chkdoor).onReply(function(message) {
    response.header("Status", message);
    response.send(200, "OK 200);
});

} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

// register the HTTP handler
http.onrequest(requestHandler);
`

We’ve published around 40 libraries (and we’ve got a bunch more queued up ready to go) - take a look at our Library Directory for more information… there’s a chance that if you’re working with a web service or IC, we already have some pre-baked code for you :slight_smile: