I’m simply trying to turn an LED on or off from a webpage without being transferred to a new page.
I’ve found several tutorials and code examples but I’m having a hard time getting it to work correctly. It works with window.location (so I know that the “?led=0” is handled correctly by the imp), but that redirects to a new page. I don’t want anything visually to happen when it sends the instruction to the Imp.
I think this means I want to use a jQuery AJAX post, but for some reason I can’t get this to work. I’ve tried probably a dozen different formats for this request, but none of them seem to work.
So, how can I send “led=0” or “led=1” to my imp from a callback without doing anything else?
Below is my full code that works for turning the LED off (while redirecting to that page) but not on.
To get you started, you can download my example zip file that uses JQuery and PHP. Unzip and upload to your website. The file “send.php” will need to have your imp URL programmed in. Also, the variable that is being sent, as found in the “index.php” file. I use the name “data”, you are using “led”, so change that too, and the appropriate value. You’ll need to play around, but you’ll get the idea of how it works.
Now realize that my demo only sends data to the imp. You will need to know if the LED is currently on or off, so a PHP script will be needed to query, or ask the agent if the LED is on or off. The agent asks the imp if it’s on or off, the imp answers, and the agent responds back to the PHP script with the answer. That requires more scripting in all 3 (the imp, the agent, your PHP script).
Thanks for the help mlseim. I spent a few hours with that code and unfortunately couldn’t get it to work. I found another post on these forums (which you were also helpful on) that mentioned a key point I was missing: including the http.urldecode in the imps agent code. I didn’t realize that ajax sends data to the agent in a different format than a simple request embedded in a URL, so that’s why the window.location method was working for me but not the ajax.
Anyway, for anyone having difficulty and looking for a barebones solution that works, here’s my agent code:
`function requestHandler(request, response) {
server.log("got a request for " + request.body);
if (request.method == “POST”){
local data = http.urldecode(request.body);
if ("led" in data) {
if ((data.led == "1") || (data.led == "0"))
{
local ledStatus = data.led.tointeger();
device.send("led", ledStatus);
}
}
}
Glad you got it working. Now you need to know if the LED is currently on or off when the webpage displays. You could have the imp periodically tell the agent what the LED status is, like once every few seconds. When the webpage needs to know, it will query the agent and the agent will send back the status. If only one person is using the webpage at a time, you only need to query once so you know whether it’s on or off. If you have 10 people all using it at the same time, then JQuery will need to constantly query the agent because you won’t know if anyone else turned it on or off.
That scenario sounds silly for one LED, but you could have a project that involves many I/O controls and dozens of people or scripts accessing it at the same time.