Monday 23 December 2013

Python, Gmail notification


In this post I will start exploring Python. I will obviously start off with a simple example (which I sort of borrowed)

Before using python on a Raspberry pi, python will need to be installed:


sudo apt-get install python python-dev python-pip
sudo easy_install -U distribute  

More than likely you will use python in conjunction with your Pi's GPIO, in which case you will need the rpi.gpio python module. The command below will alsoload the feedparser which alows RSS and atom feeds to be parsed into the python script.


sudo pip install feedparser RPi.GPIO
Downloading/unpacking feedparser
  Downloading feedparser-5.1.3.tar.bz2 (202Kb): 202Kb downloaded

Please note, that the script below is based on python 2.7. I found this a scripta good start to play around with python and GPIO at the same time. What thescript does, is check for the email account for new emails and if a new unread item is in the inbox, it will turn on the LED on pin 12.
   





First of all, the RPi.GPIO and feedparser modules are loaded





import RPi.GPIO as GPIO, feedparser    <load GPIO and feedparser module
USERNAME="username@gmail.com"
PASSWORD="password"
GPIO_PIN=12     <set the string
GPIO.setmode(GPIO.BOARD)
GPIO.setup(GPIO_PIN, GPIO.OUT)
newmails = int(feedparser.parse("https://" + USERNAME + ":" + PASSWORD + "@mail.google.com/gmail/feed/atom")["feed"]["fullcount"])
set the integer "newmails"
if newmails > 0: 
 GPIO.output(GPIO_PIN, True) <LED on pin 12 on
else: 
 GPIO.output(GPIO_PIN, False)<LED on pin 12 off


















source:  
http://mitchtech.net/raspberry-pi-physical-gmail-notifier/

I have found that when running the script and running it's python debug, I initially got an error message suggesting to run the script as root. In order to do this, I start up IDLE (Or IDLE 3 for Python 3.3 users) from a terminal or root terminal and just enter sudo idle/idle3 or idle/idle3 respectively.

oh yeah, almost forgot, here is the bread board pin out.



Merry Xmas!