Skip to content

Instantly share code, notes, and snippets.

@cat-haines
Last active August 29, 2015 14:10
Show Gist options
  • Save cat-haines/c8debaa58bf1bc194c57 to your computer and use it in GitHub Desktop.
Save cat-haines/c8debaa58bf1bc194c57 to your computer and use it in GitHub Desktop.
Mailgun API code
/******************** Constants ********************/
// Your MailGun settings
const API_KEY = "key-a1b2c3...";
const SUBDOMAIN = "sandbox123abc.mailgun.org";
// Define what email to notify
const EMAIL = "you@example.com";
/******************** LIBRARY CODE ********************/
function SendEmail(to, subject, text) {
local url = "https://api.mailgun.net/v2/" + SUBDOMAIN + "/messages";
local fromEmail = "alarm@" + SUBDOMAIN;
local headers = { "Authorization": "Basic " + http.base64encode("api:" + API_KEY) };
local data = http.urlencode({
to = to,
from = fromEmail,
subject = subject,
text = text
});
http.post(url, headers, data).sendasync(function(resp) {
if (resp.statuscode != 200 && resp.statuscode != 0) {
server.log(resp.statuscode + " - " + resp.body);
}
});
}
/******************** Electric Imp CODE ********************/
device.on("buttonPress", function(data) {
// When we get a “buttonPress” message from the device, send and email:
SendEmail(EMAIL, "Test Email", "Hello! I send this email from my Electric Imp!");
});
/********* DEVICE CODE **********/
// create a variable called button and configure it
button <- hardware.pin1;
button.configure(DIGITAL_IN_PULLDOWN, function() {
// "debounce"
imp.sleep(0.05);
// read the button
local state = button.read();
// if the button was pressed
if (state == 0) {
agent.send("buttonPress", state);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment