Arduino LED Notification for Gmail on Linux

02 december 2011 | Classified in: Electronic, Linux | Tags: Arduino, RGB, LED, Gmail,

This is a small project to display new mail notification with a RGB LED connected to an Arduino.
Every few minutes, the computer will check a Gmail inbox for unread emails. If one or more unread emails are found, it will tell the Arduino to turn a light on.

This article is not complete yet. I'm going to had more details later...

Electrical Diagram:

arduino_gmail_checker_electrical diagram

Bash Script:

On the computer, a bash script is running to regularly check for unread emails on a Gmail account. Depending on the result, a number will be send to the Arduino in order to display a notification.

  • 0: No new mail.
  • 1: Red LED, error while fetching the number of unread mail.
  • 2: Green Led, new emails.
  • 3: Blue Led, new emails.

#!/bin/bash
#SCRIPT_NAME="Arduino Gmail Checker"
#SCRIPT_VERSION="0.1"
#figlet $SCRIPT_NAME $SCRIPT_VERSION
#########################################################################################
# Description: Check Gmail for unread email and switch RGB LED on Arduino in accordance #
# Dependencies: sed, curl,                                                              #
# Author: Virtualmix                                                                    #
# http://blog.trollmaker.com/article10/arduino-led-notification-for-gmail-on-linux      #
# License: CC BY                                                                        #
#########################################################################################
# Enter Gmail username and password below (Warning: Unsafe storage):
USERID=yourUsernameHere
PASSWORD=yourPasswordHere
# Enter number of seconds between email verification:
WAIT=300
#########################################################################################

# Loop to check for new mail every X minutes:
while [ "1" -eq "1" ]; do

    # Command line to fetch the number of unread emails:
    MAILCOUNTER=`curl -u $USERID:$PASSWORD --silent "https://mail.google.com/mail/feed/atom" | sed -n 's|<fullcount>\(.*\)</fullcount>|\1|p'`

    if [[ "$MAILCOUNTER" = "" ]]; then
        echo "ERROR: The program coulndn't fetch the account for user \"$USERID\"."
        echo "- Are you connected to the Internet?"
        echo -e "- Is the userid and password correct for \"$USERID\"?\n"
        echo 111 > /dev/ttyUSB0 # Turn Red Led on
    elif [[ "$MAILCOUNTER" -eq "0" ]]; then
        echo "* There is 0 new email for user $USERID."
        echo 000 > /dev/ttyUSB0 # Turn all leds off
    elif [[ "$MAILCOUNTER" -gt "0" ]]; then
        echo "* There is $MAILCOUNTER new email for user $USERID."
        echo 232 > /dev/ttyUSB0 # Turn blue and then green led on
    fi

    echo "* Waiting $WAIT seconds before checking for emails again."
    echo "* (^C to quit the program)"
    sleep 300

done

Arduino Code:

The program running on the Arduino is very simple. All it does is wait to receive a number sent via serial communication from the computer.

  • 0: The LED will is turned off.
  • 1: Red LED is switched on.
  • 2: Green LED is switched on.
  • 3: Blue LED is switched on.

/*****************
RGB LED connected to Arduino with 330 ohm resistor to GND
Read value from serial and set LED high or low depending on the number received
0: All off
1: Red LED on
2: Green LED on
3: Blue LED on
******************/

int redLed = 11;
int greenLed = 10;
int blueLed = 9;

void setup() {
  Serial.begin(9600);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
}

void loop () {
  int value = Serial.read();

  if (value == '0') {
    digitalWrite(redLed,LOW);
    digitalWrite(greenLed,LOW);
    digitalWrite(blueLed,LOW);
  }

  if (value == '1') {
    digitalWrite(redLed,HIGH);
    digitalWrite(greenLed,LOW);
    digitalWrite(blueLed,LOW);
  }

  if (value == '2') {
    digitalWrite(redLed,LOW);
    digitalWrite(greenLed,HIGH);
    digitalWrite(blueLed,LOW);
  }

  if (value == '3') {
    digitalWrite(redLed,LOW);
    digitalWrite(greenLed,LOW);
    digitalWrite(blueLed,HIGH);
  }

  delay(1000);
}

Download:

Bash Script and Arduino Code:

Let me know if you ever make something cool with it!

8 comments

Avatar Gravatar Fred said: transparent#1 [wednesday 18 january 2012 at 16:13]

How can i make the led stay ON as long as there is new mail?
at the moment it just blinks one time...

Avatar Gravatar Fred said: transparent#2 [wednesday 18 january 2012 at 21:47]

.. I also noticed that when i read the unread-mail in Gmail, Arduino won't stop blinking although the Python script announces that "There is 0 new mail for user ... "

Avatar Gravatar Virtualmix said: transparent#3 [thursday 19 january 2012 at 09:34]

@Fred: Did you try to open the serial monitor and send some values to see if the LED lights up properly?

Try to manually send 0,1,2,3 from the Arduino IDE and then try the same but from the command line in Linux. The LED should light each colour.

If it doesn't work, then I believe the problem comes from the "auto-reset feature" present on recent Arduino. This means your Arduino reset every time it receives data.

I recommend you have a look here and use a resistor or a capacitor as explained to disable auto reset.

I will try to complet the article to talk about it.

Avatar Gravatar Fred said: transparent#4 [thursday 19 january 2012 at 17:48]

hey,
I got it working using this code (only one LED):

#define LED 13 //must be in the code
int input = 0; //this saves last led state

void setup()
{
pinMode(LED,OUTPUT);
Serial.begin(9600);
}

void loop ()
{
input = Serial.read();
if (input == '1' ) {digitalWrite(LED,HIGH);}
if (input == '0'){digitalWrite(LED, LOW);}
}

The LED stays on as long as there is a new mail. And the commands that i send in serial are numbers 1 and 0. And no resistor trick needed (using Arduino Duemilanove)

Python script works fine. I must start learning Python. Seems very interesting.

Avatar Gravatar Virtualmix said: transparent#5 [friday 20 january 2012 at 00:57]

Glad it's working ;-)
The script is made in Bash, not Python. It's very easy to learn (easier than Python).
I would recommend this website for a good start with Bash.

Avatar Gravatar Fred said: transparent#6 [wednesday 25 january 2012 at 14:32]

thanks for the page. Some years ago I used to play with Windows Batch (*.BAT) files. Really simple and fun. Linux Bash seems similar.

Avatar Gravatar Alon said: transparent#7 [saturday 26 january 2013 at 21:34]

Hi,

I noticed you used curl to check for new mail, can you elaborate on the command parameters.

I want to try it with an exchange server (xxx.webmail.com), do you think it can work?

Thanks,

Avatar Gravatar virtualmix said: transparent#8 [monday 28 january 2013 at 10:05]

@Alon: It's easy to do with Gmail because Google provide an atom feed at the address "https://mail.google.com/mail/feed/atom" but not all webmail actually offer an atom feed.
The idea is to fetch the page with Curl and then use a regex to extract the number of unread emails.
More info than I could ever provide on curl in the man page: http://man.cx/curl
Also, have a look on http://www.commandlinefu.com/ and you may find something ready to use.
Hope this helps :-)

Write a comment

What is the second letter of the word tpfhsm? : 

Categories

Last articles

Last comments