/* Electric Imp Web-controlled LEDs by: Jim Lindblom SparkFun Electronics date: November 1, 2013 license: Beerware. Please use, reuse, and modify this code. If you find it useful, buy me a beer some day! Modified by Michael Morken */ /////////////// // Pin Setup // /////////////// // Setup reference variables for our pins: ledPin <- hardware.pin9; // Lonely red LED // Configure our pins: ledPin.configure(DIGITAL_OUT); // Simple digital output ///////////////////////////////// // Agent Function Declarations // ///////////////////////////////// // setLed will turn the lonely red LED on or off. // This function will be called by the agent. function setLed(ledState) { ledPin.write(ledState); } // setUser will print out to the log the name of the LED changer // This function will be called by the agent. function setUser(suspect) { server.log(suspect + " set the Lights."); } // setTimer will turn the LEDs off after a specified number of seconds // This function will be called by the agent. function setTimer(time) { if (time != 0) imp.wakeup(time, ledsOff); // Call ledsOff in 'time' seconds. } /////////////////////////////////// // Important Agent Handler Stuff // /////////////////////////////////// // Each object that the agent can send us needs a handler, which we define with // the agent.on function. The first parameter in agent.on is an identifier // string which must be matched by the sending agent. The second parameter is // the name of a function to be called. These functions are already defined up // above. agent.on("led", setLed); agent.on("user", setUser); agent.on("timer", setTimer); ////////////////////// // Helper Functions // ////////////////////// // ledsOff just turns all LEDs off. function ledsOff() { ledPin.write(0); }