YUN reading GMail

Hello all,

I have copied the GMail example from the Arduino Blog and I trimmed it down to a single LED. Making a Gmail Lamp with Arduino Yún | Arduino Blog
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:
label:
Result:1
.
label:
Result:1
.

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.

Gmail - Inbox for youremail@gmail.com New messages in your Gmail Inbox 0 2013-09-18T00:17:13Z

*/

#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;

/* GMAIL SETTINGS */

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 "" + String(email_addr) + ""|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 "[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);
p.flush();

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

Serial.println(".");

delay(10000); // wait 30 seconds before you do it again

digitalWrite(ledPin, LOW);
}

Great work!

Hi

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);

Thanks