Hello, I’m trying out to turn on/off multiple leds using Electric Imp through Agent but I can’t get it work . I can turn on/off one led but when I try to do with multiple it’s not working at all…any idea what is wrong here, I’m very new to Electric Imp so don’t know how to make it work. please have a look (code below) and let me know what is wrong and how can i fix it. Thanks.
AGENT
`
// Log the URLs we need
server.log("Turn LED On: " + http.agenturl() + “?ledGreen=1”);
server.log("Turn LED Off: " + http.agenturl() + “?ledGreen=0”);
server.log("Turn LED On: " + http.agenturl() + “?ledRed=1”);
server.log("Turn LED Off: " + http.agenturl() + “?ledRed=0”);
function requestHandler(requestGreen, responseGreen) {
try {
// Check if the user sent led as a query parameter
if (“led” in requestGreen.query) {
// If they did, and led=1… set our variable to 1
if (requestGreen.query.ledGreen == “1” || requestGreen.query.ledGreen == “0”) {
// Convert the led query parameter to an integer
local ledStateGreen = requestGreen.query.ledGreen.tointeger();
// Send "set.led" message to device, and send ledState as the data
device.send("ch1", ledStateGreen);
}
}
function requestHandler(requestRed, responseRed) {
try {
// Check if the user sent led as a query parameter
if ("led" in requestRed.query) {
// If they did, and led=1.. set our variable to 1
if (requestRed.query.ledRed == "1" || requestRed.query.ledRed == "0") {
// Convert the led query parameter to an integer
local ledStateRed = requestRed.query.ledRed.tointeger();
// Send "set.led" message to device, and send ledState as the data
device.send("ch2", ledStateRed);
}
}
// Send a response back to the browser saying everything was OK.
responseGreen.send(200, "Active");
} catch (ex) {
responseGreen.send(500, "Internal Server Error: " + ex);
}
}
// Send a response back to the browser saying everything was OK.
responseRed.send(200, "Active");
} catch (ex) {
responseRed.send(500, "Internal Server Error: " + ex);
}
}
// Register the HTTP handler to begin watching for HTTP requests from your browser
http.onrequestGreen(requestHandler);
http.onrequestRed(requestHandler);
`
Device
`// Create a global variabled called ‘led’ and assign the ‘pin9’ object to it
// The <- is Squirrel’s way of creating a global variable and assigning its initial value
ledGreen <- hardware.pin5;
ledRed <- hardware.pin7;
// Configure ‘led’ to be a digital output with a starting value of digital 0 (low, 0V)
ledGreen.configure(DIGITAL_OUT, 0);
ledRed.configure(DIGITAL_OUT, 0);
// Function called to turn the LED on or off
// according to the value passed in (1 = on, 0 = off)
function setLedState(stateGreen) {
server.log("Set LED to state: " + stateGreen);
ledGreen.write(stateGreen);
}
function setLedState(stateRed) {
server.log("Set LED to state: " + stateRed);
ledRed.write(stateRed);
}
// Register a handler for incoming “set.led” messages from the agent
agent.on(“ch1”, setLedStateGreen);
agent.on(“ch2”, setLedStateRed);`