Posts with «mail» label

Tutorial – Send email with the Arduino Yún

Introduction

This is the third in a series of tutorials examining various uses of the Arduino Yún. In this article we’ll examine how your Arduino Yún can send email from a Google email account. Doing so gives you a neat and simple method of sending data captured by the Arduino Yún or other notifications.

Getting Started

If you haven’t already done so, ensure your Arduino Yún can connect to your network via WiFi or cable – and get a Temboo account (we run through this here). And you need (at the time of writing) IDE version 1.5.4 which can be downloaded from the Arduino website.

Finally, you will need a Google account to send email from, so if you don’t have one – sign up here. You might want to give your Arduino Yún an email address of its very own.

Testing the Arduino Yún-Gmail connection

In this first example we’ll run through the sketch provided by Temboo so you can confirm everything works as it should. This will send a simple email from your Arduino Yún to another email address. First, copy the following sketch into the IDE but don’t upload it yet:

/*
  SendAnEmail

  Demonstrates sending an email via a Google Gmail account using the Temboo Arduino Yun SDK.

  This example code is in the public domain.
*/

#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information

/*** SUBSTITUTE YOUR VALUES BELOW: ***/

// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.

// your Gmail username, formatted as a complete email address, eg "bob.smith@gmail.com"
const String GMAIL_USER_NAME = "sender@gmail.com";

// your Gmail password
const String GMAIL_PASSWORD = "gmailpassword";

// the email address you want to send the email to, eg "jane.doe@temboo.com"
const String TO_EMAIL_ADDRESS = "recipient@email.com";

boolean success = false; // a flag to indicate whether we've sent the email yet or not

void setup() {
  Serial.begin(9600);

  // for debugging, wait until a serial console is connected
  delay(4000);
  while(!Serial);

  Bridge.begin();
}

void loop()
{
  // only try to send the email if we haven't already sent it successfully
  if (!success) {

    Serial.println("Running SendAnEmail...");

    TembooChoreo SendEmailChoreo;

    // invoke the Temboo client
    // NOTE that the client must be reinvoked, and repopulated with
    // appropriate arguments, each time its run() method is called.
    SendEmailChoreo.begin();

    // set Temboo account credentials
    SendEmailChoreo.setAccountName(TEMBOO_ACCOUNT);
    SendEmailChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    SendEmailChoreo.setAppKey(TEMBOO_APP_KEY);

    // identify the Temboo Library choreo to run (Google > Gmail > SendEmail)
    SendEmailChoreo.setChoreo("/Library/Google/Gmail/SendEmail");

    // set the required choreo inputs
    // see https://www.temboo.com/library/Library/Google/Gmail/SendEmail/ 
    // for complete details about the inputs for this Choreo

    // the first input is your Gmail email address
    SendEmailChoreo.addInput("Username", GMAIL_USER_NAME);
    // next is your Gmail password.
    SendEmailChoreo.addInput("Password", GMAIL_PASSWORD);
    // who to send the email to
    SendEmailChoreo.addInput("ToAddress", TO_EMAIL_ADDRESS);
    // then a subject line
    SendEmailChoreo.addInput("Subject", "Email subject line here");

     // next comes the message body, the main content of the email   
    SendEmailChoreo.addInput("MessageBody", "Email content");

    // tell the Choreo to run and wait for the results. The 
    // return code (returnCode) will tell us whether the Temboo client 
    // was able to send our request to the Temboo servers
    unsigned int returnCode = SendEmailChoreo.run();

    // a return code of zero (0) means everything worked
    if (returnCode == 0) {
        Serial.println("Success! Email sent!");
        success = true;
    } else {
      // a non-zero return code means there was an error
      // read and print the error message
      while (SendEmailChoreo.available()) {
        char c = SendEmailChoreo.read();
        Serial.print(c);
      }
    } 
    SendEmailChoreo.close();

    // do nothing for the next 60 seconds
    delay(60000);
  }
}

Before uploading you need to enter five parameters – the email address to send the email with, the password for that account, the recipient’s email address, and the email’s subject line and content. These can be found in the following lines in the sketch – for example:

const String GMAIL_USER_NAME = "sender@gmail.com";
const String GMAIL_PASSWORD = "emailpassword";
const String TO_EMAIL_ADDRESS = "recipient@email.com";
SendEmailChoreo.addInput("Subject", "This is the subject line of the email");
SendEmailChoreo.addInput("MessageBody", "And this is the content of the email");

So enter the required data in the fields above. If you’re sending from a Google Apps account instead of a Gmail account – that’s ok, just enter in the sending email address as normal. Temboo and Google will take care of the rest.

Finally, create your header file by copying the the header file data from here (after logging to Temboo) into a text file and saving it with the name TembooAccount.h in the same folder as your sketch from above. You know this has been successful when opening the sketch, as you will see the header file in a second tab, for example:

Now you can upload the sketch, and after a few moments check the recipient’s email account. If all goes well you will be informed by the IDE serial monitor as well (if your Yún is connected via USB). It’s satisfying to see an email come from your Arduino Yún, for example in this short video.

If your email is not coming through, connect your Arduino Yún via USB (if not already done so) and open the serial monitor. It will let you know if there’s a problem in relatively plain English – for example:

Error
A Step Error has occurred: “An SMTP error has occurred. Make sure that your credentials are correct and that you’ve provided a fully qualified Gmail
username (e.g., john.smith@gmail.com) for the Username input. When using Google 2-Step Verification, make sure to
provide an application-specific password. If this problem persists, Google may be restricting access to your account, and you’ll need to
explicitly allow access via gmail.com.”. The error occurred in the Stop (Authentication error) step.
HTTP_CODE
500


So if this happens, check your email account details in the sketch, and try again.

Sending email with customisable subject and content data

The example sketch above is fine if you want to send a fixed message. However what if you need to send some data? That can be easily done. For our example we’ll generate some random numbers, and integrate them into the email subject line and content. This will give you the framework to add your own sensor data to emails from your Arduino Yún. Consider the following sketch:

/*
  SendAnEmail

  Demonstrates sending an email via a Google Gmail account using the Temboo Arduino Yun SDK.

  This example code is in the public domain.
*/

#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information

/*** SUBSTITUTE YOUR VALUES BELOW: ***/

// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.

// your Gmail username, formatted as a complete email address, eg "bob.smith@gmail.com"
const String GMAIL_USER_NAME = "sender@gmail.com";

// your Gmail password
const String GMAIL_PASSWORD = "gmailpassword";

// the email address you want to send the email to, eg "jane.doe@temboo.com"
const String TO_EMAIL_ADDRESS = "recipient@email.com";

int a,b; // used to store our random numbers
boolean success = false; // a flag to indicate whether we've sent the email yet or not

void setup() 
{
  Serial.begin(9600);
  // for debugging, wait until a serial console is connected
  delay(4000);
  while(!Serial);
  Bridge.begin();
  randomSeed(analogRead(0)); // fire up random number generation
}

void loop()
{
  // generate some random numbers to send in the email
  a = random(1000);
  b = random(1000);
  // compose email subject line into a String called "emailSubject"
  String emailSubject("The random value of a is: ");
  emailSubject += a;
  emailSubject += " and b is: ";
  emailSubject += b;  
  // compose email content into a String called "emailContent"
  String emailContent("This is an automated email from your Arduino Yun. The random value of a is: ");
  emailContent += a;
  emailContent += " and b is: ";
  emailContent += b;  
  emailContent += ". I hope that was of some use for you. Bye for now.";  

  // only try to send the email if we haven't already sent it successfully
  if (!success) {

    Serial.println("Running SendAnEmail...");

    TembooChoreo SendEmailChoreo;

    // invoke the Temboo client
    // NOTE that the client must be reinvoked, and repopulated with
    // appropriate arguments, each time its run() method is called.
    SendEmailChoreo.begin();

    // set Temboo account credentials
    SendEmailChoreo.setAccountName(TEMBOO_ACCOUNT);
    SendEmailChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    SendEmailChoreo.setAppKey(TEMBOO_APP_KEY);

    // identify the Temboo Library choreo to run (Google > Gmail > SendEmail)
    SendEmailChoreo.setChoreo("/Library/Google/Gmail/SendEmail");

    // set the required choreo inputs
    // see https://www.temboo.com/library/Library/Google/Gmail/SendEmail/ 
    // for complete details about the inputs for this Choreo

    // the first input is your Gmail email address
    SendEmailChoreo.addInput("Username", GMAIL_USER_NAME);
    // next is your Gmail password.
    SendEmailChoreo.addInput("Password", GMAIL_PASSWORD);
    // who to send the email to
    SendEmailChoreo.addInput("ToAddress", TO_EMAIL_ADDRESS);
    // then a subject line
    SendEmailChoreo.addInput("Subject", emailSubject); // here we send the emailSubject string as the email subject

     // next comes the message body, the main content of the email   
    SendEmailChoreo.addInput("MessageBody", emailContent); // and here we send the emailContent string

    // tell the Choreo to run and wait for the results. The 
    // return code (returnCode) will tell us whether the Temboo client 
    // was able to send our request to the Temboo servers
    unsigned int returnCode = SendEmailChoreo.run();

    // a return code of zero (0) means everything worked
    if (returnCode == 0) {
        Serial.println("Success! Email sent!");
        success = true;
    } else {
      // a non-zero return code means there was an error
      // read and print the error message
      while (SendEmailChoreo.available()) {
        char c = SendEmailChoreo.read();
        Serial.print(c);
      }
    } 
    SendEmailChoreo.close();

    // do nothing for the next 60 seconds
    delay(60000);
  }
}

Review the first section at the start of void loop(). We have generated two random numbers, and then appended some text and the numbers into two Strings – emailContent and emailSubject.

These are then inserted into the SendEmailChoreo.addInput lines to be the email subject and content. With a little effort you can make a neat email notification, such as shown in this video and the following image from a mobile phone:

Conclusion

It’s no secret that the Yún isn’t the cheapest development board around, however the ease of use as demonstrated in this tutorial shows that the time saved in setup and application is more than worth the purchase price of the board and extra Temboo credits if required.

And if you’re interested in learning more about Arduino, or want to introduce someone else to the interesting world of Arduino – check out my book (now in a third printing!) “Arduino Workshop” from No Starch Press.

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The post Tutorial – Send email with the Arduino Yún appeared first on tronixstuff.

Tronixstuff 23 Nov 01:40
arduino  email  gmail  google  iot  lesson  mail  review  send  temboo  tronixstuff  tutorial  wifi  yún