Sending Agent Query response to device

I am having trouble with my simple imp code.

Basically I want to have a query of either 1 or 0 in a url then switch a pin based on that query.

Seems simple enough and I am somewhat close.

I just cannot figure out how to pass the query from the Agent to the device.

Below is my agent code and my device code.

Any tips would be great.

Thanks.

Phil

`

// agent code:

function httpHandler(request, response) {

    device.send("zapper", true);

response.send(200, "OK");

}
http.onrequest(httpHandler);

device.on(“state”,function(zapState){

local buttonAgent = "https://agent.electricimp.com/vMS5fBDUClT8";

local url = buttonAgent + "?state=" + zapState;
local request = http.get(url);
request.sendasync(function(resp){

});

})
`

`

// device code

zapper <- hardware.pin9;
zapper.configure(DIGITAL_OUT);
zapper.write(0);
zapState <- 0;

function toggleZapper(state) {

    zapper.write(zapState); // how do I get the correct value here?
    agent.send("state", zapState);

}

agent.on(“zapper”, toggleZapper);
`

Replace the code before // how do I get the correct value here with

zapper.write(state)

You’ll have to make sure, of course, that the data coming from the agent is in a form the line above can use, ie. and integer of value 1 or 0. So you’ll need to modify your agent code:

device.send("zapper", 1)

As it stands, your code doesn’t check the incoming request whether the user wants to state on or off, so I’d add the above line this way:

`function httpHandler(request, response)
{
if (“switch” in request.body)
{
if (switch =“1”)
{
device.send(“zapper”, 1)
}
else
{
device.send(“zapper”, 0
}
}

response.send(200, "OK")

}`

the URL to send them becomes https://[agent URL]?switch=1. ‘Switch’ will always be a string. You could use switch.tointeger() to convert it to the integer value the device code requires, but the code above gives you the chance to check the value first.

Small edit to Tony’s code:

if (switch ="1")

should be

if (switch == "1")

Well spotted, @beardedinventor. This is my most common syntax error. Too many years programming in Basic, where = equals == as well as = (indeed == didn’t exist)

“where equals equals equals as well as equals” :slight_smile:

So all things are equal after all

I am finally getting round to testing the code and I get an error in the code that smityone kindly provided.

The error is “expression expected” and is on the line.

if (switch == “1”)

Here is my Agent code…

Any ideas?

Thanks.

Phil

`

// agent code:

http.onrequest(httpHandler);

device.on(“state”,function(zapState){

local buttonAgent = "https://agent.electricimp.com/vMS5fBDUClT8";

local url = buttonAgent + "?state=" + zapState;
local request = http.get(url);
request.sendasync(function(resp){

});

})

function httpHandler(request, response)
{
if (“switch” in request.body)
{
if (switch == “1”) // I get an error on this line
{
device.send(“zapper”, 1)
}
else
{
device.send(“zapper”, 0
}
}

response.send(200, "OK")

}

`

switch is a reserved keyword in squirrel, try this instead:

if (request.body["switch"] == "1")

Thanks, I no longer get the error if I modify the code as suggested by philmy.

But, nothing is triggering correctly via my URL.

I think it’s because my code is looking in the body of the webpage using this command.

if (“switch” in request.body)

But really, the switch is in the URL such as.

https://agent.electricimp.com/vMS5fBDUClT8?state=1

Here is my agent code.

Thanks again for your help.

`// agent code:
function httpHandler(request, response)
{
if (“switch” in request.body)
{
if (request.body[“switch”] == “1”)
{
server.log(“ON”)
device.send(“zapper”, 1)
}
else
{
device.send(“zapper”, 0)
server.log(“OFF”)
}
}

response.send(200, "OK")

}

http.onrequest(httpHandler);

device.on(“state”,function(zapState){

local buttonAgent = "https://agent.electricimp.com/vMS5fBDUClT8";

local url = buttonAgent + "?state=" + zapState;
local request = http.get(url);
request.sendasync(function(resp){

});

})`

Ah sorry, I didn’t look beyond the syntax error. I think you probably want request.query instead of request.body. It’s basically the same as the http.onrequest example code.

To match your quoted URL, you want

if ("state" in request.query) { if (request.query["state"] == "1") . . .

Hmmm, still not right… I must be missing something.

I put a server.log in my function so I can see what is in request.query and it returns a table.

[Agent] (table : 0x7f16852bd000)

Any ideas?

Thanks.

Phil

`// agent code:
function httpHandler(request, response)
{

server.log(request.query)
if ("switch" in request.query)
{ 
    if (request.query["state"] == "1")
    {
        server.log("ON")
        device.send("zapper", 1)
    }
    else
    {
        device.send("zapper", 0)
        server.log("OFF")
    }
}

response.send(200, "OK")
server.log("Requested")

}

http.onrequest(httpHandler);

device.on(“state”,function(zapState){

local buttonAgent = "https://agent.electricimp.com/xpfY20KUQTNc";

local url = buttonAgent + "?state=" + zapState;
local request = http.get(url);
request.sendasync(function(resp){

});

})

`

To log the contents of the table:

foreach(i,v in request.query) server.log(i+"="+v)

Ok, that totally worked, I modified the code so that I loop through the table to check the variables I and V but now I get a weird issue where my code does detect the “1” but then immediately detects a “0”

I have attached my updated code and my agent log.

`// agent code:
function httpHandler(request, response)
{

foreach(i,v in request.query) 

{server.log(i+"="+v)

if (“state” in i)

server.log(“found the state”)
{
if (v == “1”)
{
server.log(“ON”)
device.send(“zapper”, 1)
}
if (v == “0”)
{
device.send(“zapper”, 0)
server.log(“OFF”)
}
}

response.send(200, "OK")

} // loop through requested table

}//end function

http.onrequest(httpHandler);

device.on(“state”,function(zapState){

local buttonAgent = "https://agent.electricimp.com/xpfY20KUQTNc";

local url = buttonAgent + "?state=" + zapState;
local request = http.get(url);
request.sendasync(function(resp){

});

})

`

`
2014-10-15 13:53:07 UTC-7 [Agent] state=1
2014-10-15 13:53:07 UTC-7 [Agent] ON
2014-10-15 13:53:07 UTC-7 [Agent] Requested
2014-10-15 13:53:08 UTC-7 [Agent] state=0
2014-10-15 13:53:08 UTC-7 [Agent] OFF
2014-10-15 13:53:08 UTC-7 [Agent] Requested
2014-10-15 13:53:08 UTC-7 [Agent] state=0
2014-10-15 13:53:08 UTC-7 [Agent] OFF
2014-10-15 13:53:08 UTC-7 [Agent] Requested
2014-10-15 13:53:08 UTC-7 [Agent] state=0
2014-10-15 13:53:08 UTC-7 [Agent] OFF
2014-10-15 13:53:08 UTC-7 [Agent] Requested
2014-10-15 13:53:08 UTC-7 [Agent] state=0
2014-10-15 13:53:08 UTC-7 [Agent] OFF
2014-10-15 13:53:08 UTC-7 [Agent] Requested
2014-10-15 13:53:08 UTC-7 [Agent] state=0
2014-10-15 13:53:08 UTC-7 [Agent] OFF
2014-10-15 13:53:08 UTC-7 [Agent] Requested
2014-10-15 13:53:08 UTC-7 [Agent] state=0
2014-10-15 13:53:08 UTC-7 [Agent] OFF
2014-10-15 13:53:08 UTC-7 [Agent] Requested
2014-10-15 13:53:08 UTC-7 [Agent] state=0
2014-10-15 13:53:08 UTC-7 [Agent] OFF
2014-10-15 13:53:08 UTC-7 [Agent] Requested
2014-10-15 13:53:08 UTC-7 [Agent] state=0
2014-10-15 13:53:08 UTC-7 [Agent] OFF
2014-10-15 13:53:08 UTC-7 [Agent] Requested
2014-10-15 13:53:09 UTC-7 [Agent] state=0
2014-10-15 13:53:09 UTC-7 [Agent] OFF
2014-10-15 13:53:09 UTC-7 [Agent] Requested
2014-10-15 13:53:09 UTC-7 [Agent] state=0
2014-10-15 13:53:09 UTC-7 [Agent] OFF
2014-10-15 13:53:09 UTC-7 [Agent] Requested
2014-10-15 13:53:09 UTC-7 [Agent] state=0
2014-10-15 13:53:09 UTC-7 [Agent] OFF
2014-10-15 13:53:09 UTC-7 [Agent] Requested
2014-10-15 13:53:09 UTC-7 [Agent] state=0
2014-10-15 13:53:09 UTC-7 [Agent] OFF
2014-10-15 13:53:09 UTC-7 [Agent] Requested
2014-10-15 13:53:09 UTC-7 [Agent] state=0
2014-10-15 13:53:09 UTC-7 [Agent] OFF
2014-10-15 13:53:09 UTC-7 [Agent] Requested
2014-10-15 13:53:09 UTC-7 [Agent] state=0
2014-10-15 13:53:09 UTC-7 [Agent] OFF
2014-10-15 13:53:09 UTC-7 [Agent] Requested
2014-10-15 13:53:09 UTC-7 [Agent] state=0
2014-10-15 13:53:09 UTC-7 [Agent] OFF
2014-10-15 13:53:09 UTC-7 [Agent] Requested
2014-10-15 13:53:09 UTC-7 [Agent] state=0
2014-10-15 13:53:09 UTC-7 [Agent] OFF
2014-10-15 13:53:09 UTC-7 [Agent] Requested
2014-10-15 13:53:10 UTC-7 [Agent] state=0
2014-10-15 13:53:10 UTC-7 [Agent] OFF
2014-10-15 13:53:10 UTC-7 [Agent] Requested
2014-10-15 13:53:10 UTC-7 [Agent] state=0
2014-10-15 13:53:10 UTC-7 [Agent] OFF
2014-10-15 13:53:10 UTC-7 [Agent] Requested
2014-10-15 13:53:10 UTC-7 [Agent] state=0
2014-10-15 13:53:10 UTC-7 [Agent] OFF
2014-10-15 13:53:10 UTC-7 [Agent] Requested
2014-10-15 13:53:10 UTC-7 [Agent] state=0
2014-10-15 13:53:10 UTC-7 [Agent] OFF
2014-10-15 13:53:10 UTC-7 [Agent] Requested
2014-10-15 13:53:10 UTC-7 [Agent] state=0
2014-10-15 13:53:10 UTC-7 [Agent] OFF
2014-10-15 13:53:10 UTC-7 [Agent] Requested
2014-10-15 13:53:10 UTC-7 [Agent] state=0
2014-10-15 13:53:10 UTC-7 [Agent] OFF
2014-10-15 13:53:10 UTC-7 [Agent] Requested
2014-10-15 13:53:10 UTC-7 [Agent] state=0
2014-10-15 13:53:10 UTC-7 [Agent] OFF
2014-10-15 13:53:10 UTC-7 [Agent] Requested
2014-10-15 13:53:11 UTC-7 [Agent] state=0
2014-10-15 13:53:11 UTC-7 [Agent] OFF
2014-10-15 13:53:11 UTC-7 [Agent] Requested
2014-10-15 13:53:11 UTC-7 [Agent] state=0
2014-10-15 13:53:11 UTC-7 [Agent] OFF
2014-10-15 13:53:11 UTC-7 [Agent] Requested
2014-10-15 13:53:11 UTC-7 [Agent] state=0
2014-10-15 13:53:11 UTC-7 [Agent] OFF
2014-10-15 13:53:11 UTC-7 [Agent] Requested
2014-10-15 13:53:11 UTC-7 [Agent] state=0
2014-10-15 13:53:11 UTC-7 [Agent] OFF
2014-10-15 13:53:11 UTC-7 [Agent] Requested
2014-10-15 13:53:11 UTC-7 [Agent] state=0
2014-10-15 13:53:11 UTC-7 [Agent] OFF
2014-10-15 13:53:11 UTC-7 [Agent] Requested
2014-10-15 13:53:11 UTC-7 [Agent] state=0
2014-10-15 13:53:11 UTC-7 [Agent] OFF
2014-10-15 13:53:11 UTC-7 [Agent] Requested
2014-10-15 13:53:11 UTC-7 [Agent] state=0
2014-10-15 13:53:11 UTC-7 [Agent] OFF
2014-10-15 13:53:11 UTC-7 [Agent] Requested
2014-10-15 13:53:12 UTC-7 [Agent] state=0
2014-10-15 13:53:12 UTC-7 [Agent] OFF
2014-10-15 13:53:12 UTC-7 [Agent] Requested
2014-10-15 13:53:12 UTC-7 [Agent] state=0
2014-10-15 13:53:12 UTC-7 [Agent] OFF
2014-10-15 13:53:12 UTC-7 [Agent] Requested
2014-10-15 13:53:12 UTC-7 [Agent] state=0
2014-10-15 13:53:12 UTC-7 [Agent] OFF
2014-10-15 13:53:12 UTC-7 [Agent] Requested
2014-10-15 13:53:12 UTC-7 [Agent] state=0
2014-10-15 13:53:12 UTC-7 [Agent] OFF
2014-10-15 13:53:12 UTC-7 [Agent] Requested
2014-10-15 13:53:12 UTC-7 [Agent] state=0
2014-10-15 13:53:12 UTC-7 [Agent] OFF
2014-10-15 13:53:12 UTC-7 [Agent] Requested
2014-10-15 13:53:12 UTC-7 [Agent] state=0
2014-10-15 13:53:12 UTC-7 [Agent] OFF
2014-10-15 13:53:12 UTC-7 [Agent] Requested
2014-10-15 13:53:12 UTC-7 [Agent] state=0
2014-10-15 13:53:12 UTC-7 [Agent] OFF
2014-10-15 13:53:12 UTC-7 [Agent] Requested
2014-10-15 13:53:13 UTC-7 [Agent] state=0
2014-10-15 13:53:13 UTC-7 [Agent] OFF
2014-10-15 13:53:13 UTC-7 [Agent] Requested
2014-10-15 13:53:13 UTC-7 [Agent] state=0
2014-10-15 13:53:13 UTC-7 [Agent] OFF
2014-10-15 13:53:13 UTC-7 [Agent] Requested
2014-10-15 13:53:13 UTC-7 [Agent] state=0
2014-10-15 13:53:13 UTC-7 [Agent] OFF
2014-10-15 13:53:13 UTC-7 [Agent] Requested
2014-10-15 13:53:13 UTC-7 [Agent] state=0
2014-10-15 13:53:13 UTC-7 [Agent] OFF
2014-10-15 13:53:13 UTC-7 [Agent] Requested
2014-10-15 13:53:13 UTC-7 [Agent] state=0
2014-10-15 13:53:13 UTC-7 [Agent] OFF
2014-10-15 13:53:13 UTC-7 [Agent] Requested
2014-10-15 13:53:13 UTC-7 [Agent] state=0
2014-10-15 13:53:13 UTC-7 [Agent] OFF
2014-10-15 13:53:13 UTC-7 [Agent] Requested
2014-10-15 13:53:13 UTC-7 [Agent] state=0
2014-10-15 13:53:13 UTC-7 [Agent] OFF
2014-10-15 13:53:13 UTC-7 [Agent] Requested
2014-10-15 13:53:13 UTC-7 [Agent] state=0
2014-10-15 13:53:13 UTC-7 [Agent] OFF
2014-10-15 13:53:13 UTC-7 [Agent] Requested
2014-10-15 13:53:14 UTC-7 [Agent] state=0
2014-10-15 13:53:14 UTC-7 [Agent] OFF
2014-10-15 13:53:14 UTC-7 [Agent] Requested
2014-10-15 13:53:14 UTC-7 [Agent] state=0

`

You only need the foreach snippet for debugging, and you have some misplaced curly braces in that last post.

`function httpHandler(request, response)
{
server.log(“Here are all the query parameters:”)
foreach(i,v in request.query) {
server.log(i+"="+v)
} // loop through requested table

if (“state” in request.query) {
server.log(“found the state”)
if (request.query[“state”] == “1”)
{
server.log(“ON”)
device.send(“zapper”, 1)
}
if (request.query[“state”] == “0”)
{
device.send(“zapper”, 0)
server.log(“OFF”)
}
}
response.send(200, “OK”)
}//end function`

Thanks Philmy, when I run the code, I still have the issue where it detects a “1” then immediately detects “0”

Here is my log.

`2014-10-15 15:48:23 UTC-7 [Agent] Here are all the query parameters:
2014-10-15 15:48:23 UTC-7 [Agent] state=1
2014-10-15 15:48:23 UTC-7 [Agent] found the state
2014-10-15 15:48:23 UTC-7 [Agent] ON
2014-10-15 15:48:23 UTC-7 [Agent] Here are all the query parameters:
2014-10-15 15:48:23 UTC-7 [Agent] state=0
2014-10-15 15:48:23 UTC-7 [Agent] found the state
2014-10-15 15:48:23 UTC-7 [Agent] OFF
2014-10-15 15:48:23 UTC-7 [Agent] Here are all the query parameters:
2014-10-15 15:48:23 UTC-7 [Agent] state=0
2014-10-15 15:48:23 UTC-7 [Agent] found the state
2014-10-15 15:48:23 UTC-7 [Agent] OFF
2014-10-15 15:48:23 UTC-7 [Agent] Here are all the query parameters:
2014-10-15 15:48:23 UTC-7 [Agent] state=0
2014-10-15 15:48:23 UTC-7 [Agent] found the state
2014-10-15 15:48:23 UTC-7 [Agent] OFF
2014-10-15 15:48:23 UTC-7 [Agent] Here are all the query parameters:
2014-10-15 15:48:23 UTC-7 [Agent] state=0
2014-10-15 15:48:23 UTC-7 [Agent] found the state
2014-10-15 15:48:23 UTC-7 [Agent] OFF

etc

`

It would appear there are multiple requests being sent to your agent.

How are you issuing the request? Sometimes browsers cache and will issue requests when you don’t expect it…

I am just pasting the url into Chrome on OSX, I just tried Safari and get the same issue.

I also tried putting the browser into private mode too but no luck.

Phil

Well, the “Here are all the query parameters” print is only printed when a new request comes in.

I did notice that you are hitting an agent URL in your code though. You’re not hitting your own agent are you? Maybe pasting your entire agent could would clear this up.

ahhh, Hugo you may have found the issue, I was recycling code from a previous project. The agent URL in my code is the same as my IMP so I’m sure that’s what is going on.

I wasn’t 100% what the latter part of the code was doing so I left it in there (I should have tried to figure out what it did, I know !!)

Here is my full agent code.

Basically all i am trying to do is bring a pin high with a url and low with a different url (using ?state=).

Thanks.

Phil

`// agent code:
function httpHandler(request, response)
{
server.log(“Here are all the query parameters:”)
foreach(i,v in request.query) {
server.log(i+"="+v)
} // loop through requested table

if (“state” in request.query) {
server.log(“found the state”)
if (request.query[“state”] == “1”)
{
server.log(“ON”)
device.send(“zapper”, 1)
}
if (request.query[“state”] == “0”)
{
device.send(“zapper”, 0)
server.log(“OFF”)
}
}
response.send(200, “OK”)
}//end function

http.onrequest(httpHandler);

device.on(“state”,function(zapState){

local buttonAgent = "https://agent.electricimp.com/xpfY20KUQTNc";

local url = buttonAgent + "?state=" + zapState;
local request = http.get(url);
request.sendasync(function(resp){

});

})
`