Here’s a little project to build a very simple web-controlled multicoloured LED indicator with your Raspberry Pi and the PiBorg LEDBorg board.
Using IF This Then That (IFTTT.com) – a simple online service which allows you to daisy chain different web things together – you can light up your Pi’s LEDborg based on particular events.
Take for instance it’s predicted to be frosty outside – your Pi could light up blue, or perhaps someone’s just mentioned you on twitter – your Pi could light up green. Or your boss has just emailed you and your Pi lights up red. IFTTT allows you to set up alerts based on a wide range of events. You could even potentially use your Pi to trigger events by sending information to IFTTT – but in this example I’m just using it as a receiver.
At about a fiver, the LEDBorg is one of the cheapest add-ons available for the Pi that plugs directly into the GPIO (General Purpose Input and Output) pins. It consists of a single 3 colour LED surface mounted on a board with a controller chip. It’s fairly simple – if you’re feeling adventurous you could probably have a go at building your own version, although it would be tricky to make something so compact. The LEDBorg fits neatly over the GPIO pins and would probably fit inside most Pi cases – I’ve opted to use my upright lego case to show off the light better.
First you need to fit your LEDborg, and install the software according to which revision of the Pi you have.
The LEDborg comes with a nice app that runs in a window – you can use this to test that everything is working correctly. My first impression of the LEDborg was how bright it was – it’s considerably brighter than the on-board indicator lights:
Next comes to automating the LEDborg to work with our online service IFTTT.
To do this I’m going to be using a specially created email account. There are other ways to do this which are a bit quicker, but email is easy, doesn’t require much setup and as long as the Pi has internet access it will work. In this example I’ve opted for gmail.
Next we need a bit of code on the Pi to check the gmail account, and fetch the subject of the latest email sent to it. This code then uses the email subject to program the LEDborg. This is my first attempt at writing Python (so be kind if you spot any mistakes!) – you just need to change the your_email_address and your_password for the bits you set up:
import imaplib import email #connect to gmail mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login('[email protected]','your_password') mail.select('inbox') mail.list() typ, data = mail.search(None, 'ALL') for num in data.split(): typ, data = mail.fetch(num, '(RFC822)') typ, data = mail.search(None, 'All') ids = data id_list = ids.split() # get most recent email id 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) varSubject = msg['subject'] varFrom = msg['from'] varFrom = varFrom.replace('<','') varFrom = varFrom.replace('>','') if len( varSubject ) >35: varSubject = varSubject[0:32] ='...' #print the subject to test print varSubject #output the subject to the ledborg LedBorg = open('/dev/ledborg', 'w') LedBorg.write(varsubject) del LedBorg
(you can also download this from Gist)
Save this as ledchecker.py
You can test this by sending an email with the subject line 111 to the gmail account and by then running the script with:
sudo python ledchecker.py
Hopefully your Pi should be bathed in white light!
The position of the number in the subject refers to the red green blue LEDs in the LEDborg, and the value the intensity 2= full 1=half 0=off
So 100 is dim red, 200 is bright red. You can mix colours by using different combinations. Sending an email with the subject 000 switches the LEDborg off.
Excellent – but we don’t want to be constantly typing in the commands to run the script all the time – to get by this we’re going to use cron (think of it as Linux’s cronometer – a regular clock that does things at regular intervals). For a bit more info about cron check out this blog from David Singleton.
We can set cron to run our ledchecker.py program every minute:
sudo crontab -e
Opens your cron table in the nano editor – you just need to add
* * * * * sudo python /home/ledchecker.py
and then control-O to save and control-X to exit. You might need to adjust the line above depending on where you saved your python script.
Now your Raspberry Pi should automatically check the email account every minute and light up the LEDborg if it receives the appropriate email.
The final step is to head over to IFTTT.com and try creating a ‘recipe’ to feed your Raspberry Pi with information – I’ve created a simple recipe to get you started. If you think of any good ones, please share in the comments below!
Next thoughts are how to improve on this – perhaps it could cycle through last 5 emails? blink if it’s something really important? have a wired up ‘reset’ button. Play a chime when the notification changes?
Update: Fork me!
Brilliant – vwilcox has forked the original script and made it better – it’s now indented properly, and has a loop to check if there’s no email, for rejecting the email if the subject is too long, and a nice empty the inbox function. This is the first time anything I’ve written on gist has been forked, so that has made my day (in a very nerdy way) – thank you!