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.
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)
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
}
}
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.
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){
});
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
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){
});