Email from imp

Is there a way for my imp to email me with the status of a switch?

I thought there was some MailGun code but I don’t see any. Give me a bit this morning and I’ll write some up for you.

http://captain-slow.dk/2014/01/07/using-mailgun-with-electric-imp/

Ahh, I knew there was code somewhere. I wrote it up a little bit differently and added device code. The device code will work with anything that pulls pin 9 high… a switch, a PIR motion sensor, button, ect.

Mailgun API code

Here is a really simple project that I set up using a 4AA battery holder, tiny breadboard, Aria breakout and a Parallax PIR sensor. You can use the code for a switch, button, sensor… anything that will drive one of the pins high.

omg, sweet, i will check it out when i get off work tonight

Make Deck,
Thanks for posting this example. I just started with the imp today and your post was super helpful. I’m trying to alter it so my imp polls an analog value every 20 seconds and puts that value in the subject line from mailgun. I’m having trouble figuring out where to store that value on the agent end, I think. Any help would be awesome!

device:
`hardware.pin5.configure(ANALOG_IN);

// Function Definitions //
//////////////////////////

// sendPins reads the pin, and sends that to the agent.
// It calls itself every 20 seconds.
function sendPins()
{
pin5 = hardware.pin5.read(),

// Send it out to the agent with "impValue"
// as the identifier.
agent.send("impValue", pin5);

// Schedule a wakeup in 20secs, with a callback to this function.
imp.wakeup(20.0, sendPins);

}

sendPins(); // Call sendPins once, and let it do the rest of the work.`

agent:
`const API_KEY = “key-mykey”;
const MAILGUN_URL = “api.mailgun.net/v2/”;
const MAILGUN_DOMAIN = “mydomain.mailgun.org”;
const FROM = “First Last my@email.com”;
const TO = “First Last my@email.com”;
const EMAIL_MESSAGE = “I am sending email through MailGun from an Electric Imp!”;

function send_mailgun_email() {
local url = format(“https://api:%s@%s%s/messages”, API_KEY, MAILGUN_URL, MAILGUN_DOMAIN);
local headers = {“Content-Type”: “application/x-www-form-urlencoded”};
local body = http.urlencode({from=FROM, to=TO, subject=pin5, text=EMAIL_MESSAGE});
http.post(url,headers,body).sendasync(function(res) {
if (res.statuscode != 200) {
server.error("Mailgun error: " + res.statuscode + " => " + res.body);
} else {
try {
server.log(res.body);
} catch (e) {
server.error("Mailgun error: " + e)
}
}
})
}

// Use this with example device code
// Uncomment the three lines below
device.on(“impValue”, function(data) {
pin5 = data.pin5;
send_mailgun_email();
});`

Some minor tweaks and you should be good to go. The device.on function receives the value, so your reference to data.pin5 is not correct. Here’s an update to your agent code that will get the pin5 value into your subject line. Also note that the code gets interpreted and formatted as HTML by the forum so you will need to replace &quot in the local url with the quote character " in your code.

`const API_KEY = “key-mykey”;
const MAILGUN_URL = “api.mailgun.net/v2/”;
const MAILGUN_DOMAIN = “mydomain.mailgun.org”;
const FROM = “First Last my@email.com”;
const TO = “First Last my@email.com”;
const EMAIL_MESSAGE = “I am sending email through MailGun from an Electric Imp!”;

function send_mailgun_email( pin5Value ) {
local url = format(“https://api:%s@%s%s/messages”, API_KEY, MAILGUN_URL, MAILGUN_DOMAIN);
local headers = {“Content-Type”: “application/x-www-form-urlencoded”};
local body = http.urlencode({from=FROM, to=TO, subject=pin5Value, text=EMAIL_MESSAGE});
server.log( body );

http.post(url,headers,body).sendasync(function(res) {
    if (res.statuscode != 200) {
        server.error("Mailgun error: " + res.statuscode + " => " + res.body);
    } else {
        try {
            server.log(res.body);
        } catch (e) {
            server.error("Mailgun error: " + e)
        }
    }
})

}

// Use this with example device code
// Uncomment the three lines below
device.on(“impValue”, function(data) {

send_mailgun_email( data );

});

`

  • edit… @deonsmt beat me to it… now you have a couple of examples! :slight_smile:

@ZachDunHam Your close… data actually is the value of pin5, and you don’t have a table, so you can just use it.

Lets make EMAIL_MESSAGE a global so we can change it:
EMAIL_MESSAGE <- "";

Then change device.on.
device.on("event", function(data) { EMAIL_MESSAGE = format("Pin5 value: %d", data); send_mailgun_email(); });

@beardedinventor can problem suggest some more elegant code, but that should work.

Thanks for the help! I still had some issues on my device end with index errors, though I think I’ve got it somewhat sorted out :slight_smile:

So, we didn’t actually address the part about you wanting an analog read. You need to configure the pin like:

hardware.pin5.configure(ANALOG_IN);

That will return a value from 0-65,535. So you can divide your read()/65,535 * 3.3V to get the voltage.

What do you change the &quot to I dont understand this