Skip to content

Instantly share code, notes, and snippets.

@cat-haines
Last active August 29, 2015 14:03
Show Gist options
  • Save cat-haines/434e63d6e248081c603d to your computer and use it in GitHub Desktop.
Save cat-haines/434e63d6e248081c603d to your computer and use it in GitHub Desktop.
isHacking <- null;
message <- "";
device.on("hacking", function(data) {
isHacking = data.state;
message = format("%s (as of %i) - via an @electricimp agent", (data.state == 0) ? "NOT HACKING" : "HACKING", time())
if (!data.boot) {
twitter.tweet(message);
}
});
const HTML = @"<html>
<head>
<title>Hackaday Hacking Indicator</title>
</head>
<body>
<div style='font-size:72px'>%s</div>
</body>
</html>
";
http.onrequest(function(req, resp) {
resp.send(200, format(HTML, message));
});
/******************** Library Code ********************/
API_KEY <- "";
API_SECRET <- "";
AUTH_TOKEN <- "";
TOKEN_SECRET <- "";
class Twitter { ... }
/******************** Application Code ********************/
twitter <- Twitter(API_KEY, API_SECRET, AUTH_TOKEN, TOKEN_SECRET);
// configure LED
led <- hardware.pin9;
led.configure(DIGITAL_OUT)
// set and send initial state
isHacking <- 0;
led.write(isHacking);
agent.send("hacking", { state=isHacking, boot=true });
// configure the button
button <- hardware.pin1;
button.configure(DIGITAL_IN_PULLUP, function() {
imp.sleep(0.05); // software debounce
local state = button.read();
// button down
if (state == 1) {
// invert the state
isHacking = 1 - isHacking;
// set LED, then send to agent
led.write(isHacking);
agent.send("hacking", {state=isHacking, boot=false });
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment