Skip to content

Instantly share code, notes, and snippets.

@ladyada
Created July 6, 2012 19:16
Show Gist options
  • Save ladyada/3062225 to your computer and use it in GitHub Desktop.
Save ladyada/3062225 to your computer and use it in GitHub Desktop.
Raspberry Pi LED E-mail Notifier
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 here
PASSWORD = "password"
NEWMAIL_OFFSET = 1 # my unread messages never goes to zero, yours might
MAIL_CHECK_FREQ = 60 # 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)
!
@andyfleming
Copy link

I have a quick fix for this code for you below. I found out if you don't cleanup when closing the program, it will stop working and cause a "Channel in use" type of warning.

#...
try:
    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)

except KeyboardInterrupt:
      GPIO.cleanup()

@TheRealEdDawson
Copy link

Rad program. I made a simpler version of it that checks whether a web site is up or down:

https://github.com/TheRealEdDawson/SiteLED

@Smuuch
Copy link

Smuuch commented Feb 18, 2014

Hi guys,

May I kindly ask what would be the python script like if I would only want to use a single LED(green) for first incoming email and allow it to stay on until the second new email comes in? In a nutshell, the new email turns the led on, second email turn it off and the next turn it on and so forth, emails that are read should not be considered in this. Any help will be highly appreciated.

@egocks
Copy link

egocks commented Aug 11, 2014

I wrote a version that uses imaplib, since there seems to be a problem with authentication for the Gmail atom feed. https://gist.github.com/egocks/e32f320af67edfbc0619#file-raspi-gmail-imaplib-py

@tushar121121
Copy link

i am new to Pi 2 ...for this project no need to install anything like "imapclient" ???

please respond

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment