Arduino Email Checker

In this video we will use an Arduino Yun (or leonardo with a wifi shield) to check your email account (or chosen label) and return the number of unread emails as an integer to be used in another project or (in this case) to make an email checking clock. This is a good one for anyone just beginning with the internet of Arduino.



//Email Checking Code


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

#include <SoftwareSerial.h>
SoftwareSerial Geno(7,8); // Rx , Tx


const char* settings_file = "/root/gmail_settings\0"; /* This is the settings file */

char labelbuffer[256]; /* We will need a buffer for getting the label parameter via Bridge's REST APIs */
String label;

/* GMAIL SETTINGS */

const String username = "**************@gmail.com"; //username@gmail.com
const String password = "************"; //password



void setup() {

  Serial.begin(9600);

  Bridge.begin();   /* Initialize the Bridge */
  
  FileSystem.begin();
  
}

void loop() {

    label = "arduino"; // label name goes here!!!
    File settings = FileSystem.open(settings_file, FILE_READ);
    while (settings.available() > 0){
      char c = settings.read();
      label += c;
    }
    settings.close();
  
  Serial.println("label:" + label);

  Process p;

   delay(5000);

  /* This command checks for a specified label and returns the number of messages */
  p.runShellCommand("curl -u " + username + ":" + password + " \"https://mail.google.com/mail/feed/atom/" + label + "\" -k --silent |grep -o \"[0-9]*\" |grep -o \"[0-9]*\"");
  while(p.running());  /* do nothing until the process finishes, so you get the whole output */


  /* Read command output. runShellCommand() should have passed an integer number" */
  int result = p.parseInt();  /* look for an integer */
  Serial.print("Result:"); /* Some serial debugging */
  Serial.println(result);
  Serial.print("Label:"); /* Some serial debugging */
  Serial.println(label);

  p.flush();

 
switch (result) {
    case 0:
      Serial.println("You have 0 unread emails");
      break;
    case 1:
      Serial.println("You have 1 unread emails");
      break;
    case 2:
      Serial.println("You have 2 unread emails");
      break;
    case 3:
      Serial.println("You have 3 unread emails");
      break;
    case 4:
      Serial.println("You have 4 unread emails");
      break;
    case 5:
      Serial.println("You have 5 unread emails");
      break;
    case 6:
      Serial.println("You have 6 unread emails");
      break;
    case 7:
      Serial.println("You have 7 unread emails");
      break;
    case 8:
      Serial.println("You have 8 unread emails");
      break;
    case 9:
     Serial.println("You have 9 unread emails");
      break;     
    default: 
     Serial.println("You have 10 or more Emails to read..."); 
    break;
  } 
   

delay(3000); // wait 3 seconds before you do it again

Serial.println("Done checking! ");

}






//Servo Test Code

#include<Servo.h>

Servo myServo;

void setup() {
  
myServo.attach(9);

}

void loop() {


  myServo.write(0);
  delay(2000);
  myServo.write(180);
  delay(2000);
  
}





//Email Clock Code

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

const char* settings_file = "/root/gmail_settings\0"; /* This is the settings file */

char labelbuffer[256]; /* We will need a buffer for getting the label parameter via Bridge's REST APIs */
String label;

/* GMAIL SETTINGS */

const String username = "***********@gmail.com"; //username@gmail.com
const String password = "**********"; //password

Servo clockServo;


void setup() {

  Serial.begin(9600);

  Bridge.begin();   /* Initialize the Bridge */
  
  FileSystem.begin();

  clockServo.attach(9);
  
}

void loop() {

    label = "arduino"; // insert label name here
    File settings = FileSystem.open(settings_file, FILE_READ);
    while (settings.available() > 0){
      char c = settings.read();
      label += c;
    }
    settings.close();
  
  Serial.println("label:" + label);

  Process p;

   delay(5000);

  /* This command checks for a specified label and returns the number of messages */
  p.runShellCommand("curl -u " + username + ":" + password + " \"https://mail.google.com/mail/feed/atom/" + label + "\" -k --silent |grep -o \"[0-9]*\" |grep -o \"[0-9]*\"");
  while(p.running());  /* do nothing until the process finishes, so you get the whole output */


  /* Read command output. runShellCommand() should have passed an integer number" */
  int result = p.parseInt();  /* look for an integer */
  Serial.print("Result:"); /* Some serial debugging */
  Serial.println(result);
  Serial.print("Label:"); /* Some serial debugging */
  Serial.println(label);

  p.flush();

 
switch (result) {
    case 0:
      Serial.println("You have 0 unread emails");
      clockServo.write(180);
      break;
    case 1:
      Serial.println("You have 1 unread emails");
      clockServo.write(150);
      break;
    case 2:
      Serial.println("You have 2 unread emails");
      clockServo.write(120);
      break;
    case 3:
      Serial.println("You have 3 unread emails");
      clockServo.write(90);
      break;
    case 4:
      Serial.println("You have 4 unread emails");
      clockServo.write(60);
      break;
    case 5:
      Serial.println("You have 5 unread emails");
      clockServo.write(30);
      break;
    case 6:
      Serial.println("You have 6 unread emails");
      clockServo.write(0);
      break;
    default: 
     Serial.println("You have 7 or more Emails to read..."); 
     clockServo.write(0);
    break;
  } 
   

delay(1000); // wait 1 seconds before you do it again

Serial.println("Done checking! ");

}




Parts

Hey !!!

about us (me).

My name is Matthew, I am 26, and I attend the University of Pittsurgh. Currently I am a junior, going for a bachelors in Information Science. I am always looking for someone that would like to embark on a new project or startup. I work as a food server when I am not at school, but would love something a little more technical, if anyone is hiring.

  • Pittsburgh, PA
  • MatthewHallberg@gmail.com

Drop us a line