11
Raspberry Pi E-mail Notifier Using LEDs Created by Mikey Sklar

Raspberry Pi Email Notifier

Embed Size (px)

DESCRIPTION

Raspberry Pi Email Notifier

Citation preview

Page 1: Raspberry Pi Email Notifier

Raspberry Pi E-mail Notifier Using LEDsCreated by Mikey Sklar

Page 2: Raspberry Pi Email Notifier

234579

11

Guide Contents

Guide ContentsOverviewRemote SSHPrepare PythonWire LEDsPython ScriptAdafruits Cobbler Breakout Kit

© AdafruitIndustries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds Page 2 of 11

Page 3: Raspberry Pi Email Notifier

Overview

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

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

© AdafruitIndustries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds Page 3 of 11

Page 4: Raspberry Pi Email Notifier

Remote SSH

The first step is to get the Raspberry Pi ready to SSH in. This allows us to do all the typing on abigger, 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 PiConnect 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 andthe login: prompt. Login with user name pi and password raspberryThen type in the following commands in order1. sudo bash2. ssh-keygen -t rsa (hit enter twice when prompted)3. mv /boot/boot_enable_ssh.rc /boot/boot.rc

Then reboot by typing in reboot. Now look for the IP address on boot up. Its circled in redabove. Now you can use an SSH client on a windows/mac/linux computer (there are lots of free onesout 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 canonly be accessed from within the local network (that is, your home or office with a sharedrouter) - you can't get to it from the outside world if you have a 192. or 10. IP address. Askyour friendly IT/Network admin if you have questions about this.

© AdafruitIndustries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds Page 4 of 11

Page 5: Raspberry Pi Email Notifier

Prepare Python

We have to install Python (the programming language controller) and some libraries to followthe tutorial

Now from either the keyboard/monitor or SSH console type in sudo apt-get install python-devsudo apt-get install python-pip

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

© AdafruitIndustries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds Page 5 of 11

Page 6: Raspberry Pi Email Notifier

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

© AdafruitIndustries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds Page 6 of 11

Page 7: Raspberry Pi Email Notifier

Wire LEDs

Now we can use the Adafruit Pi Cobbler (http://adafru.it/914) to help us wire up the LEDs. Youcan 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 ofthe cable. That end goes at the side closest to the SD Card and is labeled P1 on the Pi. Theother side connects to the cobbler and can only be inserted one way due to the notch in thecable

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 (anyvalues from 100 ohm up to 1000 ohm are fine). One end of the resistors connects to thecobbler row marked #18 and then the other side connects to a row that isn't used by thecobbler. The other resistor connects to the cobbler row marked #23 and the other end toanother 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 intothe blue striped rail on the side of the breadboard.

© AdafruitIndustries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds Page 7 of 11

Page 8: Raspberry Pi Email Notifier

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

© AdafruitIndustries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds Page 8 of 11

Page 9: Raspberry Pi Email Notifier

view rawraspi-gmail.py

Python Script

Now you're ready to write the code to check your gmail IMAP email (using feedparser) and lightup the red or green LEDs

Copy and paste the following text starting with cat << ! > raspi-gmail.py and ending withthe ! and paste that into your SSH window to paste all of the code into a new file called raspi-gmail.py

cat <<! > raspi-gmail.py#!/usr/bin/env python

import RPi.GPIO as GPIO, feedparser, time

DEBUG = 1

USERNAME = "username" # just the part before the @ sign, add yours herePASSWORD = "password"

NEWMAIL_OFFSET = 1 # my unread messages never goes to zero, yours mightMAIL_CHECK_FREQ = 60 # check mail every 60 seconds

GPIO.setmode(GPIO.BCM)GREEN_LED = 18RED_LED = 23GPIO.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)!

This Gist brought to you by GitHub.

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

© AdafruitIndustries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds Page 9 of 11

Page 10: Raspberry Pi Email Notifier

Next up, we'll make the file into an application (executable), type chmod +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!

© AdafruitIndustries

http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds Page 10 of 11

Page 11: Raspberry Pi Email Notifier

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