Go Down

Topic: YUN reading GMail (Read 2796 times) previous topic - next topic


Hello all,

I have copied the GMail example from the Arduino Blog and I trimmed it down to a single LED.  http://blog.arduino.cc/2013/09/13/making-a-gmail-lamp-with-arduino-yun/
The LED will light up when I receive email and turn off when there is no email, after I delete it.  I runs in a 10 second loop.

Don't be too critical of the code, I'm very excited to use my new board, so I'm just slapping code together.

It works!

Here is the serial monitor output:

Here is the code:

/* example output from https://mail.google.com/mail/feed/atom/

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<feed xmlns="http://purl.org/atom/ns#" version="0.3">
<title>Gmail - Inbox for youremail@gmail.com</title>
<tagline>New messages in your Gmail Inbox</tagline>
<link rel="alternate" href="http://mail.google.com/mail" type="text/html"/>


#include <Process.h>

const int ledPin =  13; /* Led on Pin 13 */
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;


const String username = "USERNAME";
const String password = "PASSWORD";

unsigned long delaytime=250; /* we always wait a bit between updates of the display */

void setup() {
  pinMode(ledPin, OUTPUT); /* Initialize the LED Pin */

  Bridge.begin();    /* Initialize the Bridge */
  Serial.begin(9600);    /* Initialize the Serial for debugging */

void loop() {
/* Checks if a label has been passed via webservices
  the call is like http://arduino.local/data/put/label/LABEL
  Bridge.get("label", labelbuffer, 256);

    Checks if a label has been specified via webservices and stores it on a configuration file
   If a label has not been specified, it retrieves the last on from the configuration file

  Serial.println("label:" + label);

  Process p;

   This command checks for a specified sender and returns the number of messages,
   i changed it to check for a label because I thought the label was more flexible as I could configure it from Gmail,
   but I leave it here for your pleasure
   p.runShellCommand("curl -u USERNAME:PASSWORD \"https://mail.google.com/mail/feed/atom\" -k --silent |grep -o \"<email>" + String(email_addr) + "</email>\"|wc -l");

  /* 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 \"<fullcount>[0-9]*</fullcount>\" |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 */

  if (result > 0)
    digitalWrite(ledPin, HIGH); /* If I got messages, then I turn on the LED */
    digitalWrite(ledPin, LOW); /* No messages, so I turn the LED off */


  delay(10000);  // wait 30 seconds before you do it again
  digitalWrite(ledPin, LOW);




With this code I can only get the total of unread messages but can't filter by label.

Inserting this line after the Bridge.get, we will get the results only for the specified label.

  Bridge.get("label", labelbuffer, 256);
  label = String(labelbuffer);


Go Up