Skip to content

Instantly share code, notes, and snippets.

@edwin-bluekite
Created September 6, 2014 21:42
Show Gist options
  • Save edwin-bluekite/3a29bf5921ddc1bcd5a7 to your computer and use it in GitHub Desktop.
Save edwin-bluekite/3a29bf5921ddc1bcd5a7 to your computer and use it in GitHub Desktop.
Electric Imp Led Switch Agent and Device
const COWBELL = "https://agent.electricimp.com/05V8cFjX6eN4";
function cowbell(nullData) {
server.log("Button action");
// http.get(COWBELL).sendsync();
}
device.on("buttonPress", cowbell);
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