Skip to content

Instantly share code, notes, and snippets.

@dt
Last active November 23, 2020 19:43
Show Gist options
  • Save dt/e1a666ed344b8bad0fa3 to your computer and use it in GitHub Desktop.
Save dt/e1a666ed344b8bad0fa3 to your computer and use it in GitHub Desktop.
Coffee Monitor Imp
light <- hardware.pin8;
light.configure(ANALOG_IN);
was_brewing <- false;
brew_light <- 45000;
poll_time <- 30;
skip_updates <- 10;
until_heartbeat <- 0;
imp.setpowersave(true);
function notify(brewing, level) {
until_heartbeat = skip_updates;
agent.send("status", {
"brewing": brewing,
"light": level,
"power": hardware.voltage()
});
}
function check() {
local lvl = light.read();
local brewing = false;
if (lvl < brew_light) {
brewing = true;
}
if (until_heartbeat < 1 || brewing != was_brewing)
notify(brewing, lvl);
else
until_heartbeat--;
was_brewing = brewing;
imp.wakeup(poll_time, check);
}
imp.onidle(function() {
server.log("sleeping");
server.expectonlinein(poll_time * skip_updates);
});
check();
was_brewing <- false;
slack_url <- "https://hooks.slack.com/services/blahblah";
function slack(msg) {
server.log(msg);
local body = "{\"text\": \""+msg+"\", \"username\": \"Coffeebot\", \"icon_emoji\": \":coffee:\"}";
local resp = http.post(slack_url, {}, body).sendsync();
server.log("Slack says ("+resp.statuscode + "): " + resp.body)
}
function spreadsheet() {
local url = "https://docs.google.com/forms/blahblah/formResponse"
local resp = http.post(url, {}, "ifq&submit=Submit").sendsync();
server.log("Google says "+resp.statuscode)
}
function done() {
slack("Coffee is ready!");
}
device.on("status", function (info) {
server.log(format("brewing: %s (light: %d). voltage: %.3fv", info["brewing"].tostring(), 0xffff - info["light"], info["power"]));
if (info["power"] < 3.25) {
slack("Uh-oh, batteries might be getting low: " + info["power"].tostring());
}
if (info["brewing"]) { //brewing!
if (!was_brewing) {
slack("Coffee is brewing...");
spreadsheet();
}
was_brewing = true;
} else {
if (was_brewing) {
server.log("finished brewing, waiting for drip...");
imp.wakeup(60, done);
}
was_brewing = false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment