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.
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:
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:
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.
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
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:
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!