Category menu

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:

http://stats.wordpress.com/csv.php?api_key=yourAPIkey&blog_uri=www.yourblogaddress.com&days=-1&summarize

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!

, ,

9 Responses to Build a Raspberry Pi powered LED web counter

  1. richard November 30, 2013 at 7:22 pm #

    Hi Pete,

    Really helpful article…thanks.

    I want to display a number from a local text file that is updated every 2 secs…would the script need much amending to have it do this?

    Any help much appreciated.

    Thanks
    Richard

  2. Pete December 1, 2013 at 1:36 pm #

    Hi – yes but you’d need to modify the script as 2 seconds is a much quicker update time – you could modify the loop at the bottom of the script to check the file and update the display.

    You won’t need the beautiful soup library – just open the text file – instructions are here – http://www.tutorialspoint.com/python/python_files_io.htm

    plus the BelleVue is best suited for this as the scrolling displays take longer to update.

  3. richard December 5, 2013 at 1:17 am #

    Thanks for the reply Pete. I’m a newbie so please bear with me 🙂

    The wharfe education led arrived today so I hooked it up following your instructions but I get an error “E: unable to locate package serial” when I run sudo apt-get install python-serial.

    So I tried “sudo wget https://raw.github.com/lurch/rpi-serial-console/master/rpi-serial-console -O /usr/bin/rpi-serial-console && sudo chmod +x /usr/bin/rpi-serial-console” and understand I have to disable the console which I have done.

    But after rebooting I stiil can get the LED to illuminate in any way.Is there a way I can check the serial port?

    Sorry for my ignorance and thanks for pointing me to the info re opening a text file.

    Thanks
    Richard

  4. Pete December 5, 2013 at 11:28 am #

    hi,
    try sudo apt-get update first – and check you’re typing sudo apt-get install python-serial and not python serial –

    • vivek December 5, 2013 at 3:03 pm #

      hi Pete how do you do….

      hey i need your help about raspberry pi
      i want to run my python script on start up how i do that
      please help me

      my project should be run automatically when ever i give power up.

      .

  5. Pete December 5, 2013 at 3:14 pm #

    there’s a discussion on running scripts at startup here: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=63&t=46125

  6. richard December 5, 2013 at 8:12 pm #

    thanks….did just that and looks like its installed without errors. Rebooted still no display on led. Searched and installed mnicom to run a test, ran minicom -b 9600 -o -D /dev/ttyAMA0 which opened minicom, but still nothing. Should the rpi-serial-console be enabled or disabled?

    I’ll get some female connectors at the weekend, as I’m now thinking its the way I have it wired.

    thanks
    richard

  7. Ders October 12, 2016 at 3:50 pm #

    I’m a real noob when it comes to this. Could you make a guide on how to to this with the subscriber count one’s youtube channel?

Leave a Reply