topamax once a day

Raspberry Pi Physical Gmail Notifier

  

This tutorial will demonstrate how to easily turn your Raspberry Pi into a physical Gmail notifier, in only 10 lines of python! If the configured Gmail account has unread messages, the LED will be illuminated, otherwise dim.  The project was inspired by the Arduino/Mac version by J4mie adapted for use on the Raspberry Pi.

Here is a diagram of the wiring of the LED with a 330 ohm resistor in series (created with Fritzing):

raspi_gmail_led

The python script uses the feedparser module to simplify interaction with Gmail and the RPi.GPIO module to control the GPIO pins. The easiest way to install these is using the python pip package manager. If you don’t have it installed, you can install the pip package manager using apt-get. In a terminal on the Pi:

EDIT: For 2012-07-15-wheezy-raspbian.zip and newer, the Python development headers (python2.7-dev) are also required:

sudo apt-get install python-pip python2.7-dev

Next, for pip to work correctly you will need to update to a newer version of distribute using easy_install:

sudo easy_install -U distribute

Then install the feedparser and GPIO modules with pip:

sudo pip install feedparser RPi.GPIO

Once the pre-requisites have been installed, download , or copy and paste the following Python script to the Raspberry Pi:

The final step is to configure the script to run every minute as a cron job. To do so, open the global crontab for editing:

sudo crontab -e

Then add this line to the end of the file (adjust to the location of the python script):

* * * * * python /home/pi/raspi_gmail.py

That’s it! From now on, cron will execute the script once every minute.  If you have unread messages, the GPIO pin will be pulled high, lighting the LED, otherwise, it will be disabled, dimming the LED.

 

40 Comments

  1. Followed your instructions exactly, and I’m getting “SyntaxError: invalid syntax” at the colon at the end of “if newmails > 0: ”

    I’ve tried removing the colon, and then it SyntaxError’s again at the next line at the end of GPIO in “GPIO.output(GPIO_PIN, True)”…

    frustrating, it’d be great if I could get this to work, looks great!

    • I suspect your problem is in the line above the if statement… The colon is needed for valid Python syntax. Try adding print newmails above the if statement to check the return value from the feedparser function call.

  2. meirm@riunx.com

    ource/py_gpio.c:23:20: fatal error: Python.h: No such file or directory

    compilation terminated.

    error: command ‘gcc’ failed with exit status 1

    • Looks like you are missing the Python development headers… its seems they are not included by default in the newest Raspbian release. Install them like this:
      sudo apt-get install python-dev
      Then re-run the pip installer:
      sudo pip install feedparser RPi.GPIO
      Thanks for catching the error… I have updated the tutorial to reflect the changes!

      • Minor clarification: I had the same problem, but I had to fix it by installing python2.7-dev (not python-dev).

        Also, in order for pip to function properly (requires >=0.6.27), I had to run “easy_install -U distribute” before the above commands to update it.

        Great RasPi posts, Michael!

  3. Thank you very much for this article — there are a number of similar articles that are wrong, but this worked great for me!

  4. Just FYI, on the latest release it looks like the GPIO states are not persistent. So as soon as the python script exits, the light goes out. This took me a while to figure out, but hopefully will help others!

  5. Hi everyone,
    Can someone please help me : I’ve got this when trying to debug : “ImportError : No module named feedparser”
    I’m real beginner in the world of RPi and I’m french, so please excuse my poor english!!
    Thank you in advance!

  6. Jack Langstone

    When i enter ‘$ sudo python raspi-gmail.py’
    it says

    File “raspi-gmail.py”, line 5, in
    GPIO.setmode(GPIO.BOARD)
    NameError: name ‘GPIO’ is not defined

    please help :)

    • Hi Jack, are you importing the RPi.GPIO as GPIO? If your import statement uses the default naming (ie import RPi.GPIO instead of import RPi.GPIO as GPIO) this would cause your NameError above.

  7. I whant to know how you connected the wires. because in the picture I see no resistor.
    A scheme it would be very helpful. Thanks!

  8. Hi Michael.
    I took your great starting point to build a notifier which announces the senders of unread emails using Morse Code.

    Good fun mixing leading- and trailing-edge technologies!

    Read about it here: http://m0xpd.blogspot.co.uk/2012/12/morse-code-gmail-notifier.html

    m0xpd

  9. How do you connect the LED ? Directly without resistance ?

  10. Hi chatainsm, the LED is connected to the Pi with a 330 ohm resistor in series. I added a Fritizing diagram to the article for clarification.

  11. Tommy Hill

    As soon as I add the Python Script to the Raspberry Pi, and run it, I get a syntax error as follows: “SyntaxError: invalid character in identitfier”. It also highlights the dot in RPi.GPIO. What is wrong here??

  12. I am getting the following error:
    AttributeError: ‘module’ object has no attribute ‘parse’

  13. Does the resistor have to be 300 ohm or will it work with 200ohm

  14. Praveen

    Hi Michael :) firstly thanks for all the guides. Very helpful. However, i’m faced with a error.

    syntax error near unexpected token `GPIO_PIN,

    This appears when i paste Huge chunk of python script. What does this mean and what do it do now?

  15. Hey, I like the project but when i easy_install, it doesnt work. Im not sure howto fix this.

  16. jimbo

    Got this working with GPIO but it doesn’t find the shell script when I run it with ‘shell’ or ‘bash’. Should the scripts be in the same directory as the program (raspiBot.py) or in the /home/pi directory? I don’t find them in either place.

  17. Hi,

    Could u post the code in c for email notifier project on raspberry pi?

    Thanks&Regards,

    K.Arungopal

  18. Great! Sounds great! One small problem: I have followed all the steps, but it says ImportError: No module named RPi.GPIO

  19. Richard Thompson

    A beginner here: upon trying “sudo easy_install -U distribute”, I get serveral lines of error message ending with: ” raise ImportError(“Entry point %r not found” ” ((group, name),) ImportError: Entry point (‘console_scripts’, ‘easy_install’) not found
    I looked in /usr/local/bin and both easy_install and easy_install2.7 are shown as present. What does this mean? Thanks

  20. Great tutorial. Do you know how we would use feedparser to find the email subject. I want to run a command when I email a new gmail inbox I’ve set up. This way, I can run the script from anywhere.

  21. Yongseen Kim

    I dont know why cron job doesnt work…
    no response at all…
    except that everything works without a problem

  22. Trevor ford

    Is there a way to make this work with hotmail?

  23. rustybikes

    Thanks for this! I’d been playing with Adafruit’s code that runs in a While loop that checks every minute. For whatever reason, it would only run for a few hours and then stop working. Granted, remoting into the Pi to restart the Python script is trivial, but annoying. So, I started by asking Google about how to troubleshoot it. I’d figured I’d have to put a watchdog in cron to make sure it was always running. When I saw you running the Python code itself from cron, I had a pretty hard facepalm moment. That was.. uh.. fun? Ha.

    I’ve built on the idea (Gmail checker), and have it not only checking, but also telling me how many unread messages are in my inbox, using a 7-segment display (also from Adafruit). Here’s a brief clip showing it in action: http://youtu.be/Gq6wY9A1pbU – it’s running once a minute via cron, based on your example.

    Thanks again!

  24. Spatimouth01

    Is it possible to setup a encrypted password in the python script?

  25. Alex Singleton

    Hi,
    I was jsut wondering if you had any idea of how you would go about coding the Raspberry Pi to send a SMS message to your phone if you recieve an ‘Out of Office’ after sending an email to someone? Or getting the LED to turn on if getting that same ‘Out of Office’ response?

    • DigitalBlueeye

      I can help with that, Im working on a similar program and after a lot of thought, i figured out the easiest and free way to do it is to use a sender/reciever setup thru python on the py, sending to your phone via its email , the way i figured this out is i sent a text to my email ( just insert email instaid of number, no smartphone needed) and then used the sender address ( ex 7401231234@txt.att.net) and it works fine, just as fast as sms phone to phone

      so use a email setup in python such at smmtp to text your phone if something happens that you list.

  26. DigitalBlueeye

    hey i tried this code out from this website and also adafruits identical tutorial, when i run the code i get a import error on feedparser, i checked spelling, updated the pi, etc, so far no suscess, im planning to use it to control a python program and servo, but this is in the way so if anyone can help i would much appriciate it! even just refer me to a similar module to use or something, spent at least two hours on this issue with no luck…
    thankyou

  27. Ian Hetherington

    Works a treat thanks, I have the pi doing something when an email lands in a certain folder. But…… can you get it to mark the mail as read, otherwise it just keeps repeating. any help greatly appreciated
    Thanks

Trackbacks/Pingbacks

  1. how to email snapshot from raspberry pi with a webcam | Rants, grunts and chants - [...] source: http://mitchtech.net/raspberry-pi-physical-gmail-notifier/ [...]
  2. Morse Code Gmail Notifier #piday #raspberrypi @Raspberry_Pi « adafruit industries blog - [...] on from the inspiration provided by Michael at MitchTech, who described a Physical Email Notifier using the RPi, latterly …
  3. LearnGrow LLC - [...] that lots of people are already talking about how to do this and there are some great examples.Michael over …
  4. Ten pimped-out projects for the Raspberry Pi | AyeITech - [...] for the constant, soul-crushing guilt that you’re behind on email when you could have a Raspberry Pi-controlled LED light make your …
  5. Raspberry Pi E-mail Notifier Using LEDs | linuxtor耳朵 - [...] that lots of people are already talking about how to do this and there are some great examples. Michael over …
  6. Contrôle d’un dispositif basse tension via Raspberry Pi | MesPotesGeek.fr - [...] lancer le script il faut être ROOT et donc utiliser sudo sudo ./test.py Vous pouvez vous faire un notifier de …
  7. Notificador de Emails | Carlos Latorre - […] Fuentes: http://mitchtech.net/raspberry-pi-physical-gmail-notifier/ […]

Leave a Reply