An image of the un-cased RaspberryPi
Hardware Projects Raspberry Pi

RaspberryPI plus LEDBorg plus IFTTT.com = Fun with Lights

||||| 0 Like |||||

So, now my Raspberry Pi us up and running there are several questions as to what I can learn with it. One brilliant add on is called the “LEDBorg” by a British Company PiBorg.

This is simply a three colour LED you can control from the Pi’s GPIO pins. It plugs in over the top of the pins and is simple to use. I won’t cover setup and configuration – for that please see http://www.piborg.com/ledborg

Using the LEDBorg with Gmail and IFTTT.com

Now you have your device all setup and configured head over to the original source for this information : http://kimondo.co.uk/ifttt-blinky-led-with-a-raspberry-pi/(Thanks Pete Taylor).

Some Changes to Pete’s Code

Pete’s code is an excellent starting point, but what more can you do?

import imaplib
import email

#connect to gmail

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('email@gmail.com','yourPassWordPlease')
mail.select('inbox')
mail.list()

typ, data = mail.search(None, 'ALL')
for num in data[0].split():
    typ, data = mail.fetch(num, '(RFC822)')
typ, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()

# get most recent email id
# Any Emails? 
if id_list:
    latest_email_id = int( id_list[-1] )
    for i in range( latest_email_id, latest_email_id-1, -1):
        typ, data = mail.fetch( i, '(RFC822)')
    for response_part in data:
        if isinstance(response_part, tuple):
            msg = email.message_from_string(response_part[1])
    varSubject = msg['subject']
    varFrom = msg['from']
    varFrom = varFrom.replace('<','')
    varFrom = varFrom.replace('>','')
    if len( varSubject ) >35: # Subject to large - turn the light off
        varSubject = '000'
else: #No Emails so turn the light off
    varSubject = '000'

#print the subject to test

print varSubject

#output the subject to the ledborg

LedBorg = open('/dev/ledborg', 'w')
LedBorg.write(varSubject)
del LedBorg
#Remove used emails from mailbox
typ, data = mail.search(None, 'ALL')
for num in data[0].split():
   mail.store(num, '+FLAGS', 'Deleted')
mail.expunge()
mail.close()
mail.logout()

This can be downloaded/forked on Gist – https://gist.github.com/vwillcox/5090214#file-gmail-py

I have added the following extra features to the pre-existing code

1) Added If Else loop to check for empty mailbox and turn LED off if no emails
2) Added Section to empty mailbox after using them

So, this will run every 1 minute check for the latest email and light up the appropriate colour. If there are no emails, it will turn the light off for you.

Future Additions

When I get time I will be adding some extra features that will only turn the light off if you have seen it (an email switch can be sent) and if you have multiple alerts since you have last flagged the emails, it will flash all colours as needed (Flash/Fade)

Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *