Turning ON/OFF Multiple LEDs Help!

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);`

You are setting up two functions setLedStateGreen and setLedStateRed to respond to messages “ch1” and “ch2”, but you have actually defined two different functions in your agent, both called SetLedState. If you use the right names for the functions you might get better luck:)

Yeah, the http object has an onrequest() method, not onrequestGreen() or onrequestRed(), so Squirrel will through an error with these. What you need is a single request handler which parses incoming request and triggers the appropriate device.send().

So clear your agent code after the server.log()s and add this code:

`function requestHandler(request, response) {
    try {
        // Check if the user sent led as a query parameter
        if ("ledRed" in request.query) {
            // If they did, and led=1.. set our variable to 1
            if (request.query.ledRed == "1" || request.query.ledRed == "0") {
                // Convert the led query parameter to an integer
                local ledStateRed = request.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.
        		response.send(200, "Set or unset Red");
        		return;
            }
        }
        
        if ("ledGreen" in request.query) {
            if (request.query.ledGreen == "1" || request.query.ledGreen == "0") {
                local ledStateGreen = request.query.ledGreen.tointeger();
		device.send("ch1", ledStateGreen);
                response.send(200, "Set or unset Green");
        		return; 
            }
        }
    
    	response.send(200, "Command not recognized";
    	
    } catch (ex) {
        response.send(500, "Internal Server Error: " + ex);
    }
}

// Register the HTTP handler to begin watching for HTTP requests from your browser
http.onrequest(requestHandler);`

Hello, I tried out your code but it didn’t work = /

Yep. Missed an couple of request names – try it again now

Wow awseome man worked this time! :D…can you recommend any sites i can look into for water liquid sensors and temperature sensor to turn led on when the temp and water level is too high ?..thanks :slight_smile:

How accurate does the temp need to be? And do you need to know the actual real-time level of the water, or only if it hits a certain level, like a high level switch?

temp need to show real-time temp and if it hits high enough level then it will send me a message but for the water level i needs to hit certain level before it sends me alert message.

For the temp part you need a thermocouple that can handle being in the water. Since there are so many K-Type thermocouples to choose from, something like this would be an option:

It uses the SPI interface and can run on 3.3V

There are stainless steel K-Type thermocouples available online from many places.
Search on eBay for: stainless k-type thermocouple

The water sensor part could be as simple as a float switch, or you could purchase two K-type thermocouples and keep one in the air. When water hits it, you’ll know it by the temperature change. Added benefit of knowing the ambient air temp.

Using SPI, I think you can connect both in series and address them? You’ll then have other open Imp pins for other things. If not, you’ll have to use 2nd set of pins for SPI. I’m not an expert with SPI, but there are some documents and of course, Imp experts.