Saturday, July 19, 2014

Use Arduino Yun to send emails (simple instructions for new users)

To send emails for your projects in Arduino Yun, you can install SSMTP. SSMTP allows you to send emails directly from the yun and is a package you can install from the browser or over ssh.

I'll create a simple project to demonstrate pushing a button to sending an email. First, we need to set up the Yun. Make sure it's connected to wifi and you can connect to it over SSH.

Using putty (or your favorite terminal) log into the Yun as root and update your packages:
opkg update

Install SSMTP 
opkg install ssmtp

Once installed, make a backup of the current ssmtp conf file
mv /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.bkp

Create a new conf file
vi /etc/ssmtp/ssmtp.conf

(for new vi users press "I" here to enter insert mode)

Paste this in and update your email, password and username (works with Google Hosted email accounts as well)
# The user that gets all the mails (UID < 1000, usually the admin)

# The mail server (where the mail is sent to), both port 465 or 587 should be acceptable
# See also

# The address where the mail appears to come from for user authentication.

# The full hostname

# Use SSL/TLS before starting negotiation

# Username/Password

# Email 'From header's can override the default domain?

(again, for new vi users save the file by pressing Escape and typing :wq)

Update the security for your conf file since your password is in plain text
chmod 640 /etc/ssmtp/ssmtp.conf

Go back to /root
cd /root

Create a new test file called test.txt with "Hello world"
echo "Hello world" >  test.txt

Now we can test this out by sending it to an email
cat test.txt | ssmtp

If it works you should get an email from "root" with no subject and "Hello world" in the body

Once this works, we can create a simple push button project to send that email whenever a button is pressed.

Make sure your Arduino app is installed and you have selected the board Arduino Yun and the port is set to the correct IP. 
  • Tools > Board > Arduino Yun
  • Tools > Port > YUN at 192.168.x.x (Arduino Yún)
Clear out the blank sketch and paste this

#include <FileIO.h>
#include <Process.h>

int pushButton = 2;

void setup() {

void loop() {

 // read the pin number 2
 int buttonState = digitalRead(pushButton);

Process p;
if (buttonState > 0)
  p.runShellCommand("cat /root/test.txt | ssmtp");
  delay(5);  // wait 5 milliseconds before you do it again

Unplug the Yun and wire it up like this. I'm using a 10K ohm resistor.

Power it up and wait for it to connect to wifi. Now you should be able to press the button and get an email.

Some notes on this setup.

The code is very small so it has some shortcomings. The delay is set to 5 milliseconds, which should be good for a quick press... but if you hold it down too long you will get multiple emails. There are ways around this using (millis) and other tricks, but the purpose of this example is for a simple code to use so you can build on.

Also, you can modify the test.txt file to include a subject, or even insert values. vi the file again and use this template instead:

From:arduino yun
Subject: put subject here
put message body here

No comments:

Post a Comment