Monday, 16 April 2012

Physical Priority GMail notifier and retro mailbox with display, sound and flag

A brief history of GMail retro mailbox.


[from the previous post] While I am taking a little break last week for mid sem break, I decided to do a physical Gmail notifier , as if all notifications from my gtalk , phone and tablet are not enough . 
Well , the catch is this will notify me only for the important messages . So I can turn off notifications from other gmail clients notifying me of unwanted mails.
The design is simple ,using gmail's priority inbox feature)I fetch the important mail from gmail rss feed (https://mail.google.com/gmail/feed/atom/important) using python script and send data to arduino using pyserial .

What it does

The incoming important mail will raise the mail flag, display the mail title in LCD (scrolling) , light up notifier LED and play melody.


Watch a short demo video: 








The followings are photos of mail in priority inbox or lack of that.




Yahoo! You've got a mail. 
Woohoo! You've no mail






Tutorial : How to 

You will need :
Software list:


Hardware list
  • Arduino Uno or duemilanove board
  • LCD
  • Servo motor
  • Piezo speaker
  • LED 
  • 100 Ohm resistor
  • variable resistor
  • Mini breadboard
  • paper and sessior to make your own box

Step 1. Fetch Software

 Install software if you do not have them already.
Pyserial is python module for serial communication. Feedparser is module for parsing the emails. Pyserial installation is easy since it is exe file (if you use windows). However, feedparser installation could be tricky if you are a newbie to python like me. You need to add python directory to Windows' "PATH" envionment variables. Then use the command prompt to install feedparser as mentioned in feedparser documentation page. (Hit me up in the comments for any problem).


Step 2: Get Connected



Connect hardware components as follows.



Image is produced by using Fritzing.



 



Step 3: Get codes.


 Upload Arduino sketch file. You can change melody as you like. You can compose a new one all the notes are in pitches.h. Mine is a simple alarm melody that I like . simple piano score  B,G,A,D, , D,A,B,G.

/*
 * This code reads serial port for email.
 * If mail is present, it will turn on LED, display on LCD and scroll the display,
 * raise flag and play defined melody
 *copyleft by Aung Thiha
 *created in April 12 
 *last modified April 15 
 *This code is in public domain
 *www.engineeringthecure.blogspot.com
 */
#include <LiquidCrystal.h>
#include "pitches.h"
#include <Servo.h>
#define MAX 45     // servo maximum degree. I am making it turn counterclockwise so 0 degree to 45 degreee. It depends on your servo position and configuration
#define MIN 0      // servo minium degree

LiquidCrystal lcd(12,11,5,4,3,2);
int ledPin =13;
char ser;
String email ="";
int servoPin = 9;
// notes in the melody: you can write any melody you like
// this is a tune I like. so I coverted the piano note 
int melody[] = {NOTE_B5,NOTE_G4,NOTE_A5,NOTE_D4,0,NOTE_D4,NOTE_A5,NOTE_B5,NOTE_G4,0};

// note durations: duration for a note to play. 4 = quarter note, 8 = eighth note, etc.:
// Not important for this tune, but u need to change it if you want to run other other tunes
int noteDurations[] = { 4,4,4,4,4,4,4,4,4,4};
Servo flag;

void setup(){
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode(ledPin,OUTPUT);
  flag.attach(servoPin);
}

void loop(){
  
  lcd.clear();
  lcd.setCursor(0,0);
  if (Serial.available()) {
         lcd.print("connected");
          lcd.setCursor(0,1);
   ser = Serial.read();     //read the serial port
          //if incoming character is 0, no mail, LED is low 
         if (ser == '0'){
             digitalWrite(ledPin,LOW);
             flag.write(MIN); 
             //lcd.write("You got no mail!");           
          }
          //if other characters, servo flag turns, Lcd display and play sound"
         else if(ser > '0') {
           flag.write(MAX);
           lcd.clear();
           email += ser;
           digitalWrite(ledPin,HIGH);
           getEmail();  //parse email function ,code block is below
           printEmail(); //print email ,code block is below
           playSound(); // play melody
           email ="";  // reset email
           
         }
       
 
    }

 
  delay(1000);
 
    
}
/*
 *parse  the email from incoming characters.
 *adding each character to email string
 */

void getEmail(){
    while(Serial.available()>0){
      char nextChar = Serial.read(); 
      email += nextChar;
    }
}
/*
 * print email and scroll it
 *
 */
void printEmail(){
  int emailLength = email.length();
  lcd.setCursor(0,0);
  lcd.print(email);
  delay(500);
  //scroll it to left until the final char of email is displayed on LCD
  for (int positionCounter = 0; positionCounter <emailLength-12; positionCounter++) {
    lcd.scrollDisplayLeft(); 
    delay(500);
  }
  lcd.clear();
  lcd.print(email);
  
}
/*This code play tune from melody array . 
* literally from public domain code
* from arduino Tone tutorial http://arduino.cc/en/Tutorial/Tone
*/
void playSound(){
 
   // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 10; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 2000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
    
}



Then save the following codes as pitches.h
 
/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

Then get python script. Open new Window >> paste codes >> saved it under any name 
with .py extension. Put in your email, password, change serial port that is same as port on
arduino.I pointed the path to "important" label to get priority mails. You can change that 
to specific label just by pointing to your label instead of important. You can set your own 
filters and label as explained in the link.Reminder about python: index is very important in
python. Copy exactly as follow.





import serial, sys, feedparser,time
#Settings - Change these to match your account details
USERNAME="youremail@gmail.com"
PASSWORD="yourpassword"
PROTO="https://"
SERVER="mail.google.com"
PATH="/gmail/feed/atom/important"
mySerial= serial.Serial('COM5',9600) #change port to  your setting.

while True:
        
        gmail = feedparser.parse(PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH)
        unread = len(gmail.entries)
        #"print ("Unread:{0}".format(unread)) #uncomment if you want it to dispaly on your desktop
        if unread ==0:
                mySerial.write(0)
                time.sleep(10)
                #print("no mail")
        elif unread>0:
                gmail_latest = gmail.entries[0]
                title = gmail_latest.title
                author = gmail_latest.author
                #print (title)
                #print (author)
   
                mySerial.write("[{0}] {1} ".format(unread,title))
                time.sleep(20)
        mySerial.flushOutput()





Step 4. Test it



Run python script by using F5.


You can always disable some function to suit your need. e.g You might not want sound or display the message at certain time. Then you just need to disable that part of code on arduino. I wrote it in code block exactly to do just that.
If something is wrong , check the connection.


Step 5. Put genie in the box :)



Create the box as creative as you can. I create a cube just because I love the cube design. (not because of Steve Jobs btw.)


Yahoo! You've no mail :)

You have got a mail :(




References.
There are links that give me inspiration and some codes :)


1)http://j4mie.org/blog/how-to-make-a-physical-gmail-notifier/   I got feedparser python code
2)http://blog.tinyenormous.com/2008/12/08/arduino-lcd-gmail-subject/ I got LCD title display idea. but link for download is not working . so I need to write mine.
3) Arduino.cc for all the resources