Send an email with Arduino Yun

Hi guys, I am trying to understand if I can send an email with Arduino Yun WITHOUT using Tamboo. OpenWRT can send email and I can run commands from the Arduino side to the Linux side of the board so sending an email in theory should be a trivial operation. Unfortunately I've not seen any scketch on the web about this but I think that many of us are interested to do this simple operation. Can someone describe here if is possible and how to do it?

Hi check this out: http://wiki.openwrt.org/doc/howto/smtp.client

Thanks but I would like to have more details.. How I can connect to Openwrt to setup my mail account? Wich command I have to run to send an email with an integer argument?

I’ve never used this mini-sendmail but I guess you’d need to use Process with its runShellCommand
That command would be something like

echo 'my very long text with an integer' | mini_sendmail -ffrom-user@example.com to-user@example.com

You can make a python script that send mail, and then call it from your sketch with an argument

http://forum.arduino.cc//index.php?topic=187319.msg1414629#msg1414629

Another option is to use ssmtp:

opkg update
opkg install ssmtp

Configure ssmtp to use Gmail:

http://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/

Now you can send mail from the commaandline:

cat email.txt | ssmtp you@yourmail.dk

In the file email.txt you can specify additional headers:

From: you@gmail.com
To: you@yourmail.dk
Subject: Subject testing

Body after one blank line
more body

And finally this is a modified shellcommands sketch sending a mail with sensorvalue

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

int sensorValue=345;

void setup() {
  Bridge.begin();	// Initialize the Bridge
  Serial.begin(9600);	// Initialize the Serial
  FileSystem.begin();
  // Wait until a Serial Monitor is connected.
  while(!Serial);
}

void loop() {
  Process p;
  // This command line runs the WifiStatus script, (/usr/bin/pretty-wifi-info.lua), then 
  // sends the result to the grep command to look for a line containing the word
  // "Signal:"  the result is passed to this sketch:
  // p.begin();
  String dataString;
  dataString+="From: you@gmail.com\n";
  dataString+="To:you@yourmail.dk\n";
  dataString+="Subject: Sensorvalue from Yun\n\n";
  dataString+="This is a message from Yun\n";
  dataString+="Sensor value=";
  dataString+=sensorValue;
  File dataFile = FileSystem.open("/mnt/sd/mail.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {

    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 


  p.runShellCommand("cat /mnt/sd/mail.txt | ssmtp you@yourmail.dk  2>&1");

  while(p.running());  

  // Read command output. runShellCommand() should have passed "Signal: xx&":

  while (p.available()>0) {
    char c = p.read();
    Serial.print(c);
  }

  // Ensure the last bit of data is sent.
  Serial.flush();
  Serial.println("end");
  delay(50000);  // wait 50 seconds before you do it again
}

Thanks Erni, that was exactly what I needed. Now I can send email without using Temboo and not having to write a Python script. It also saves me a few bytes, which is important as I keep on coming up against memory limit on the Arduino side.

I followed you instructions and ran your sketch example and things came together very easily.

Thanks Ernie, I still hadn't the chance to test it but seems to be very nice. But a question is why I need to use gmail? Should I use a linux mail server like sendmail inserting my gmail address as the sending address instead? I need only send an email to myself to trigger an event. Thanks B.

I am not sure I understand your question:

But a question is why I need to use gmail?

You are just using gmail's smtp server. You can ofcourse send mails to whoever you want including yourself. It is just easier than setting up your own mailserver.

Erni I tested your sketch. It works perfectely!! Thanks a lot for your support.

On my yun (newer yuns?) the sd card is sda1 The script is useful, thanks!

Erni:

Thank you. This is a really net piece of code!!!

Whats going on in this part is mostly a mystery to me:

Process p;
  // This command line runs the WifiStatus script, (/usr/bin/pretty-wifi-info.lua), then 
  // sends the result to the grep command to look for a line containing the word
  // "Signal:"  the result is passed to this sketch:
  // p.begin();

I see I have lots of home work to do to be more comfortable with Linux and Wifi control.

But the good news is that it does work and will provide a basis for several sketches I am planning that require an email to be send when conditions require it. Best news is that it does not require an external web host (Tremboo, et. al) to implement!!!!!

David Garrison

It turns out that the reason I was confused about the following section of code is that the comments are incorrect for this sketch. They are from another sketch that that was cut and pasted into this example sketch! The only part that is correct, in the referenced section is the "Process.p" part.

Process p;
  // This command line runs the WifiStatus script, (/usr/bin/pretty-wifi-info.lua), then 
  // sends the result to the grep command to look for a line containing the word
  // "Signal:"  the result is passed to this sketch:
  // p.begin();

Some of the other comments are also incorrect and I suspect for the same reason.

I have researched and now understand all the code used in this application. It has been a most satisfying experience.

I have now written a new sketch, based on this code, that monitors temperature and humidity and sends an email if either rise or fall from some set range limit ( it also sends an email if and when the temp or humidity return to the normal range . It works great, It has been up and running for two days now. I breath on the sensor every once in awhile to see if everything is still working. This a copy of the body of an email that was sent to me after I blew on the sensor:

Office Temperature and Humidity from David1 Yun Temperature: 79.88 Humidity: 92.40

David Garrison