You are on page 1of 11

Raspberry Pi E-mail Notifier Using LEDs

Created by Mikey Sklar

Guide Contents Guide Contents Overview Remote SSH Prepare Python Wire LEDs Python Script Adafruits Cobbler Breakout Kit 2 3 4 5 7 9 11

Adafruit Industries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds

Page 2 of 11

Overview

Raspberry Pis popularity make things so easy that it is almost scary. I set forth on a simple starter project of having the raspberry pi show me when new gmail messages arrive. After some searching it seems that lots of people are already talking about how to do this and there are some great examples. Michael over at MitchTech (http://adafru.it/aJG) had the most ready to go code which I pilfered from. Adafruits Cobbler Breakout Kit (http://adafru.it/914) makes the bread board experience even easier with the clearly labeled pins for each of raspis GPIOs.

These are the things I had to setup in order to get a working raspberry pi + gmail + adafruit cobbler. You can probably get yourself up and running with this same setup in less than 30 minutes!

Adafruit Industries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds

Page 3 of 11

Remote SSH
The first step is to get the Raspberry Pi ready to SSH in. This allows us to do all the typing on a bigger, more comfortable computer. Its not essential but we found it pretty darn handy!

Setting up the pi to allow remote log-ins only has to be done once! Be sure to have a SD card with a working Linux distribution (we wont cover this here) Be sure to have a micro USB cable to power the Pi Connect a monitor (Television or HDMI), USB keyboard, and working Ethernet cable to the Pi Power up the Pi. Make sure you can start up the Pi, see the white loading text and the lo gin: prompt. Login with user name pi and password raspberry Then type in the following commands in order 1. sudo bash 2. ssh-keygen -t rsa (hit enter twice when prompted) 3. mv /bo o t/bo o t_enable_ssh.rc /bo o t/bo o t.rc Then reboot by typing in rebo o t. Now look for the IP address on boot up. Its circled in red above. Now you can use an SSH client on a windows/mac/linux computer (there are lots of free ones out there) and log in to that ip address (for example, 192.168.1.106 or whatever yours is) with the same username pi and password raspberry Now you can control the Pi from your desktop computer! Remember that this IP address can only be accessed from within the lo cal netwo rk (that is, your home or office with a shared router) - you can't get to it from the outside world if you have a 192. or 10. IP address. Ask your friendly IT/Network admin if you have questions about this.

Adafruit Industries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds

Page 4 of 11

Prepare Python
We have to install Python (the programming language controller) and some libraries to follow the tutorial Now from either the keyboard/monitor or SSH console type in sudo apt-get install pytho n-dev sudo apt-get install pytho n-pip

Then you can install the email management python library: sudo pip install feedparser

Adafruit Industries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds

Page 5 of 11

We had to update our Python distribution next, run sudo easy_install -U distribute

Finally you can install the Raspberry Pi GPIO (General Purpose Input/Ouput) library : sudo pip install RPi.GPIO

Adafruit Industries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds

Page 6 of 11

Wire LEDs
Now we can use the Adafruit Pi Cobbler (http://adafru.it/914) to help us wire up the LEDs. You can follow that product tutorial first to get it assembled, then come back here. When connecting the 26-pin GPIO cable, make sure that you note the red wire, that's pin #1 of the cable. That end goes at the side closest to the SD Card and is labeled P1 on the Pi. The other side connects to the cobbler and can only be inserted one way due to the notch in the cable Place the cobbler onto the board board straddling the center line. Connect the GND pin (ground) to the blue power rail on the side of the breadboard. You'll need two resistors (any values from 100 ohm up to 1000 ohm are fine). One end of the resistors connects to the cobbler row marked #18 and then the other side connects to a row that isn't used by the cobbler. The other resistor connects to the cobbler row marked #23 and the other end to another empty row.

Now grab a red LED and a green LED. Look for the long pins on the LEDs, those are the positive (+) legs. Connect the long (+) leg of the red LED to the resistor connected to #23 (GPIO #23) and the long leg of the green LED to the resistor connected to #18. The short legs plug into the blue striped rail on the side of the breadboard.

Adafruit Industries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds

Page 7 of 11

That's it! You've just wired two LEDs with current-limiting resistors to the GPIO pins of the Pi

Adafruit Industries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds

Page 8 of 11

Python Script
Now you're ready to write the code to check your gmail IMAP email (using feedparser) and light up the red or green LEDs Copy and paste the following text starting with cat << ! > raspi-gmail.py and ending with the ! and paste that into your SSH window to paste all of the code into a new file called raspigmail.py cat <<! > raspi-gmail.py #!/usr/bin/env python import RPi.GPIO as GPIO, feedparser, time DEBUG = 1 USERNAME = "username" PASSWORD = "password" NEWMAIL_OFFSET = 1 MAIL_CHECK_FREQ = 60 # just the part before the @ sign, add yours here # my unread messages never goes to zero, yours might # check mail every 60 seconds

GPIO.setmode(GPIO.BCM) GREEN_LED = 18 RED_LED = 23 GPIO.setup(GREEN_LED, GPIO.OUT) GPIO.setup(RED_LED, GPIO.OUT) while True: newmails = int(feedparser.parse("https://" + USERNAME + ":" + PASSWORD +"@mail.google.com/gmail/feed/atom")["feed"]["fullcount"]) if DEBUG: print "You have", newmails, "new emails!" if newmails > NEWMAIL_OFFSET: GPIO.output(GREEN_LED, True) GPIO.output(RED_LED, False) else: GPIO.output(GREEN_LED, False) GPIO.output(RED_LED, True) ! time.sleep(MAIL_CHECK_FREQ)
raspi-gmail.py view raw

This Gist brought to you by GitHub.

Don't forget to set the username and passwo rd to match your gmail account! You can edit the file after the fact using the simple text editor nano , that is run nano raspi-gmail.py to make simple edits

Adafruit Industries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds

Page 9 of 11

Next up, we'll make the file into an application (executable), type chmo d +x raspi-gmail.py Finally you can run the script! Type in sudo ./raspi-gmail.py Send yourself some emails to see the green LED light up!

Adafruit Industries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds

Page 10 of 11

Adafruits Cobbler Breakout Kit


Adafruits Cobbler Breakout Kit (http://adafru.it/914)

Adafruit Industries

Last Updated: 2012-09-18 09:45:25 PM EDT

Page 11 of 11

You might also like