Saturday, 7 January 2012

Gmail Notifier

4 Years ago Tom Paton posted this and I asked him about his code, thus prompting him to post this. 4 Years later, now finally equipped with some know how and a little bit more experience with microcontrollers and programming, I thought this a fairly ideal project to have a bash at.

I adapted his python code to something a little more appropriate for my usage:

#!/usr/bin/python2
# Author: Will Price
# Date: Jan 2011
# Import statements:
# - Feedparser - needed to extract mailcount
# - Serial - needed to send command to Arduino
# - Time - waiting for gmail to be read
import feedparser, serial, time
# Variables
checkPeriod = 60 # Time between check in seconds
username = "username"
password = "pass"
label = " " #Label of folder to check no. of unread messages
# Define 'msgCount' function, returns number of unread messages (CREDIT TO: Tom Paton)
def msgCount(uid, pwd, filter):
inbox = feedparser.parse("https://%s:%s@gmail.google.com/gmail/feed/atom%s" % (uid, pwd, filter))
return len(inbox["entries"])
# Loop
while True:
# Serial port setup
com = serial.Serial("/dev/ttyUSB0",9600,timeout=0.25)
msgs = msgCount(username, password, label)
if (msgs > 0): # If number of unread messages is greater than 0, then tell the arduino
com.write("%c" % (msgs) )
# print "You have %d messages to read" % msgs # For debugging purposes
com.close()
while (msgs > 0):
msgs = msgCount(username, password, label)
time.sleep(10)
com = serial.Serial("/dev/ttyUSB0",9600,timeout=0.25)
com.write("%c" % 0)
# print "Inbox is empty" # For debugging purposes
com.close()
time.sleep(checkPeriod)
view raw gistfile1.py hosted with ❤ by GitHub
(replace username and password with your username and password. You might also need to fiddle with the serial port I've got mine setup as /dev/ttyUSB1)

Here's the Arduino code:

// Gmail Notifier
// Author: Will Price
// Date: Jan 2011
// Website: willprice94.blogspot.com
// Description:
//
// A simple sketch that waits for the number of unread messages to come over serial
// Use 'gmail_daemon' inconjunction for a physical mail notification system
// Macros
#include <Servo.h>
#define ServoPin 9 // The pin that the servo is connected to
// Variables
int msgs = 0; // Number of messages unread
//Setup Servo
Servo myservo;
//Initial Setup
void setup() {
pinMode(ServoPin, OUTPUT);
myservo.write(90); // Down position
myservo.attach(ServoPin);
Serial.begin(9600);
}
//Loop
if (Serial.available() > 0) { // Waiting to read from serial, if the buffer > 0 BYTES then execute code below
int msgs = Serial.read();
if (msgs == 1) {
myservo.write(0); // Up position
}
else if (msgs == 0){
myservo.write(90); // Down position
}
}
}
view raw gistfile1.ino hosted with ❤ by GitHub
I'll be updating this post as I go, with pictures and videos. I've currently put a servo in a little mailbox made out of card and when I have an email the flag is raised. I'm working on getting the right sized enclosure and am currently using a Arduino Pro Mini which has no USB interface and I have to work out how to remedy this. Admittedly, this is a total waste of an Arduino as it's overkill for this project, but at the moment I don't have enough time to learn how to write C code for AVR micros, but when I do, I'll make this a little less wasteful and hopefully use a micro with USB support built in. But for the time being -- Arduino and USB to UART Adapter.

2 comments:

  1. Hi fatmi,
    thanks for the comments. The python code is a bit shoddy, if you unplug the Arduino it crashes so I need to fiddle around and start adding error exceptions to handle these events.
    At the moment I'm busy with exams hence I have very little time to work on my hobbies!

    ReplyDelete