Tag Archives: python

Build a Raspberry Pi powered LED web counter

Here’s a little project to build an LED web counter for your blog. Proudly(!) display the number of visitors on a retro LED display, using the wordpress stats API – or potentially using any web accessible stats page.

You’ll need a Raspberry Pi (could even be the computer that’s hosting your blog) and a WordPress blog – either hosted yourself and using the Jetpack Stats plugin or on WordPress.com.

For this project I’m using a Python library called beautiful soup which can grab information from a web page – so as well as using this script to display a web counter, you could use it to display any information scraped from any accessible web page.

If you’re not using WordPress you could also do this using google analytics and the Embedded Analytics service, or by using your own counter installed on your website (see ideas below).

The Python script grabs the value from the web, and then sends it to the Pi’s serial port where it is displayed on an LED matrix.

For my project I’m using the Wharfe-Education.com BelleVue kit which fits neatly inside a Ferrero Rocher box and has a nice Back to the Future look about it. For 15 quid it’s a nice easy to solder together kit which features a 6 figure 7 segment LED display.

Wharfe-Education.com BelleVue and Raspberry Pi
Wharfe-Education.com BelleVue and Raspberry Pi

Raspberry Pi to BelleVue serial wiring diagram
Best to attach these with the power off

First make sure the Pi is powered down before attaching anything to the GPIO pins.

To attach the BelleVue to the Pi, you’ll need to attach the TXD (transmit), 5v and GND (ground) pins on the Pi’s GPIO to the inputs on the BelleVue – the right is a diagram for reference: – TXD on the Pi goes to RxD on the BelleVue, 5v on the Pi goes to Vcc and GND to GND.

For reference the diagram on the right shows the top row of GPIO pins with the Pi logo and text on the board the right way up – on the BelleVue it’s the 3 pins on the left hand side as you look at the board, again with the text the correct way up.

When you power on the Pi a sequence of characters will be sent out across the serial port. It is possible to disable this using the instructions found on the Raspberry Pi Spy website here – although they are for the Pi-Lite they equally apply to the BelleVue or any arduino powered LED display.

The Raspberry Leaf provides a handy guide to get the correct pins on the GPIO – or you can use the Adafruit cobbler and some breadboard. The serial port pins are in the same location for all revisions and A and B models of the Pi.

You could also use an arduino with an LED display – here’s an LED Matrix board made by Ciseco, which I’ve written about before:

LED matrix shield Ciseco
LED matrix shield sat on top of an Arduino Duo

or a Pi-Lite for a scrolling ticker board effect. The Pi-Lite just plugs into the GPIO and sits neatly on top of the Pi. I’ve found that the transparent Mod-My-Pi.com case perfectly fits over the Pi-Lite:

Pi-Lite and Raspberry Pi
Pi-Lite and Raspberry Pi

Alternatively if you’re up to the challenge (and want to save some cash) you could make your own – the Pi-Lite, BelleVue and LED matrix boards are all LED modules driven by Arduino based micro controllers and there’s a handy tutorial here which also includes the code you need.

Setup guide

First on the Raspberry Pi we need to install pyserial to make use of the Pi’s serial port:

sudo apt-get install python-serial

Then install the Beautiful Soup library. This allows us to grab information from a web page.

sudo apt-get install python-beautifulsoup

We could use something simpler, but Beautiful Soup is quite a handy way of scraping information from any web page, and it’s an interesting library to learn about.

For WordPress based stats:

Get your API key from https://apikey.wordpress.com – this will require you to log in using your wordpress account.

A quick test is to use the following URL:


Replacing yourAPIkey and blog_uri with your values. The final part of the URL defines what data is returned. You should see a plain text message of “views” and a number if everything is working properly. The &days=-1 returns the total number of unique visits to the site – you can replace this with &days=1 for number of views in the last day or &days=30 for number of views in the last 30 days, etcetera.

Once you’re happy with your URL, create a new python script – either on the Pi Desktop or on the command line using

sudo nano webcounterticker.py

and pasting the following python script

Once you’ve saved the script, run it using the:

python webcounterticker.py

Command – you should see your attached LED display list the number of visits to your site. This will loop with the same value until the script is stopped or run again.

Finally – 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 (short for 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 webcounterticker.py program every minute:

sudo crontab -e

Opens your cron table in the nano editor – you just need to add

* * * * * sudo python /home/webcounterticker.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.

A few other ideas:

Once you’ve got your LED display running you might want to think of a few other things to do with it:

  • You can use Embedded analytics to generate a copy of your Google stats to use with the Beautiful Soup library
  • Use your own web counter – e.g. this php script (which I’m currently using on the footer of GhostPi.org)
  • Host a blog on the same Raspberry Pi that is running the web counter
  • Build an Ashes scoreboard to record England’s epic victory over Australia
  • Build a rack of LED counters for Web visits, Twitter followers, Emails sent or other things you want to measure (and show off)
  • Buy a DMC-12 on ebay and turn it into a time machine

As always, corrections and improvements are welcome!

Using a Raspberry Pi LEDborg as an IFTTT.com blinky LED notifier

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.

Spidey checking out the LEDborg

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:

LEDborg fitted to a Raspberry Pi
Resistance is futile

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')

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

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:
varSubject = varSubject[0:32] ='...'

#print the subject to test

print varSubject

#output the subject to the ledborg

LedBorg = open('/dev/ledborg', 'w')
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!

Check out the fork here.