Created
September 6, 2014 21:42
-
-
Save edwin-bluekite/3a29bf5921ddc1bcd5a7 to your computer and use it in GitHub Desktop.
Electric Imp Led Switch Agent and Device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const COWBELL = "https://agent.electricimp.com/05V8cFjX6eN4"; | |
function cowbell(nullData) { | |
server.log("Button action"); | |
// http.get(COWBELL).sendsync(); | |
} | |
device.on("buttonPress", cowbell); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
led <- hardware.pin9; | |
button <- hardware.pin1; | |
ledState <- 0; | |
function toggleLED() { | |
// flip ledState | |
ledState = 1-ledState; | |
if (ledState == 0) { | |
server.log("LED Off"); | |
} else { | |
server.log("LED On"); | |
} | |
led.write(ledState); | |
} | |
function onButtonPress() { | |
local state = button.read(); | |
imp.sleep(0.02); // software debounce | |
if (state == 0) { | |
server.log("Button Pressed"); | |
agent.send("buttonPress", "Button Pressed"); | |
} else { | |
server.log("Button Released"); | |
agent.send("buttonPress", "Button Released"); | |
toggleLED(); | |
} | |
} | |
led.configure(DIGITAL_OUT); | |
button.configure(DIGITAL_IN_PULLUP, onButtonPress); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment