Build an LED Indicator with a Raspberry Pi (for Email, Weather, or Anything)

Configuring Your LedBorg as a Rain Indicator


For this portion of the tutorial we’ll be combing several things together in order to turn our LedBorg LED module into a rain indicator based on the weather forecast for our location. We’ll use a Python script to call a weather API which will in turn read the chance of rain for the day, and then toggle the LED from off to bright blue to indicate the forecasted rain.

First, we need to get an API access key for Weather Underground. For personal use and small development projects the API is free. Visit the Weather API sign up page here and sign up for an API key.

Once you have your API key, visit the Weather Underground and search for the city you want to monitor. In our case we’re going to monitor San Fransisco, CA. The URL for San Fransisco’s forecast page is:

The important part for our purposes is the last part of the URL: /CA/San_Francisco.html. We’re going to use that to edit the forecast URL for the API tool. The base URL is: API KEY/forecast/q/STATE/CITY.json

You can generate the forecast for any US city by entering your API key, the two letter state code, and the city name from the URL you pulled from your Weather Underground search results.

Once you have the API URL with your API key and state/city inserted, you can then edit the following Python script by creating a new text document on your Pi using Leafpad and pasting the following code into it:

from urllib2 import urlopen
import json

req = urlopen(' API KEY/forecast/q/STATE/CITY.json')
parsed_json = json.load(req)
pop = int(parsed_json['forecast']['txt_forecast']['forecastday'][0]['pop'])

# The following is a debugging value.
# Edit out the hash and change
# the integer to a 0-100 to test
# the LED response.

# pop = 0

print 'Current chance of precipitation is {}.'.format(pop)

# The default setting is to turn on the LED
# for any chance of rain above 20%. You can adjust
# the value in "if pop > 20:" as you wish.

if pop > 20:
LedBorg = open('/dev/ledborg', 'w')
del LedBorg
print ('Rain!')
LedBorg = open('/dev/ledborg', 'w')
del LedBorg
print ('No rain!')

Save the file as in the /home/pi/ directory. Open up the terminal and type in the following command:


If you have entered your API key and your state/city codes properly, it should kick back a response that looks like:


If precipitation is predicted for your area your LedBorg output should look like such:


Now waiting for a rainy day to properly test the script would be tedious. If there is no chance of rain in your area today and you want to see the LED light up, edit the script and replace the “pop = pop” passthrough value in line 13 with a value greater than 20 such as the 60 that our forecast returned. Just remember to change the line back to “pop = pop” when you’re done.

The final step is to set up a cron job to automatically run the script we just saved on a regular interval in order to keep the LED indicator current. Since this task is necessary for both this script and the email indicator that follows, we’re going to cover setting up a cron job after we’ve shown you how to set up the other script.

Configuring Your LedBorg as a Gmail Indicator


Who doesn’t like the dopamine fix that comes with seeing new email in their inbox? In this portion of the tutorial we are going to show you how to use the LedBorg as an new Gmail indicator. Just like last time, we are going to combine an external input (in this case an Atom feed instead of an API) and a simple script to drive our LED.

We need to expand the functionality of our Python installation a little bit by installing FeedParser, a Python RSS/Atom feed reading tool. Open up the terminal and enter the following command:

sudo easy_install feedparser

Once the installation is complete we can set to work creating our Gmail checking script. Again, using Leafpad, paste the following text into the editor. Change the username and password to match the username and password of the Gmail account you wish to check.

import feedparser

# Enter your Gmail username
# and password. Don't include
# the portion of
# your username.

username = "username"
password = "password"

mail = int(feedparser.parse("https://" + username + ":" + password +"")["feed"]["fullcount"])

# The following is a debugging value.
# Edit out the hash and change
# the integer to a 0 or 1 to test
# the LED response.

# mail = 0

if mail > 0 :
LedBorg = open('/dev/ledborg', 'w')
del LedBorg
print ('Mail!')
LedBorg = open('/dev/ledborg', 'w')
del LedBorg
print ('No mail!')

Save the script as Open up the terminal and type the following command:


If you have email sitting in your Gmail inbox the LED will turn green and you will get a response like such:


If you have mail in your Gmail inbox your LedBorg will look like so:


Just like the rain checking script, we have included a debugging value. If you don’t have any new email you could either send yourself an email to raise your inbox count to 1 or you could edit out the comment hash and change the debugging line to “mail = 1” to test the script. Remember to revert the line when you’re done testing.

Hop into the next section of the tutorial to set your Gmail script to run on a schedule.

Setting Up a Cron Job to Run Your Script


Now that we have two scripts to play with, we need to set up a cron job in order to run them throughout the day to keep the LED indicator current.

The first thing we want to do is to turn off the LED if it is currently on from our previous experiments. At the terminal type:

echo “000” > /dev/ledborg

While you’re still at the command line, you can open up the cron editor. If you have never set up a cron job before, we would strongly suggest checking out our guide to using it here. That said, we’ll walk you through setting up a basic schedule right here.

At the terminal type:

sudo crontab –e

This will open up the Raspbian cron table in the Nano text editor. Use the arrow keys to scroll down to the very bottom. This is where we’re going to setup the recurring cron job for our Python scripts.

If you want to set up the rain script, enter the following line into the cron table:

*/5 * * * * python /home/pi/

Press CTRL+X to exit; select yes to save and overwrite the existing cron table. The value we entered in the cron table “*/5 * * * *” sets the script to run every 5 minutes, forever.

Every 5 minutes is a fine span of time for a script that checks for forecasted rain—you could even argue it’s a bit too aggressive—but if you’re trying to keep on top of your email it’s a bit too long a span for notification. If you’re setting up the schedule for the Gmail notification script enter the following line in the cron table:

*/1 * * * * python /home/pi/

This entry runs the script every minute for a much speedier update notification.

That’s all there is to it! You can experiment with your own Python scripts by lifting the if/else statements out of ours and trying them out with brand new variables. If you can find an input source for the data you can turn it into a variable in your Python script—stock market averages, pollen counts, Twitter mentions, if there is an API for it you can turn it into an ambient LED indicator.

Finally, I want to give thanks to all the great resources I used to work on this project. It has been a bit over a decade since I was actively writing programs and it took a few knocks to get the dust and rust out. The contributors at /r/LearnPython helped me muck about in the API output for Weather Underground, studying how Michael over at Mitch Tech dealt with the Gmail Atom feed made parsing it for the LedBorg easy, and studying the Python learning modules at Code Academy was a great way to pick up the basic syntax and structure of a language I’d never used before.

Jason Fitzpatrick is a warranty-voiding DIYer who spends his days cracking opening cases and wrestling with code so you don't have to. If it can be modded, optimized, repurposed, or torn apart for fun he's interested (and probably already at the workbench taking it apart). You can follow him on if you'd like.