Skip to content

Instantly share code, notes, and snippets.

@mitchellgu

mitchellgu/Agent

Created Nov 16, 2014
Embed
What would you like to do?
Falangafone Electric Imp Code
http.onrequest(function(req, resp) {
if (req.path=="/update"){
if ("n" in req.query) {
local currentSong = req.query["n"].tointeger();
device.send("led0", currentSong % 2);
device.send("led1", currentSong/2 % 2);
device.send("led2", currentSong/4 % 2);
device.send("led3", currentSong/8 % 2);
device.send("led4", currentSong/16 % 2);
}
}
resp.send(200, "OK")
});
device.on("button", function(state) {
http.get("http://131a46b7.ngrok.com/toggle").sendasync(function(resp) {
server.log(resp.statuscode);
});
});
led1 <- hardware.pin1;
led1.configure(DIGITAL_OUT, 0);
led2 <- hardware.pin2;
led2.configure(DIGITAL_OUT, 0);
led3 <- hardware.pin5;
led3.configure(DIGITAL_OUT, 0);
led4 <- hardware.pin7;
led4.configure(DIGITAL_OUT, 0);
led5 <- hardware.pin8;
led5.configure(DIGITAL_OUT, 0);
agent.on("led0", function(state) {
led1.write(state);
});
agent.on("led1", function(state) {
led2.write(state);
});
agent.on("led2", function(state) {
led3.write(state);
});
agent.on("led3", function(state) {
led4.write(state);
});
agent.on("led4", function(state) {
led5.write(state);
});
btn <- hardware.pin9;
btn.configure(DIGITAL_IN_PULLUP, function() {
local state = btn.read();
if (state==0) {
agent.send("button", state);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.