Category menu

Raspberry Pi powered General Election Swingometer

In the UK it’s election time again and the BBC are preparing to wheel out the latest version of their Swingometer. The BBC Swingometer dates back to 1959, and was a visual device for displaying the balance in seats won by the two main political parties. The Swingometer coined a whole load of phrases along the lines of a ‘swing away’ or ‘swing towards’ a particular party or candidate, and was well suited towards the simple days of two party politics.

Here’s version 1.0 back in 1959:

There's a chap with a pipe behind controlling the arrow

There’s a chap with a pipe behind moving the arrow (photo BBC)

The BBC have more or less stuck to the same device for every election since, although sadly in the same way that CGI ruined the Star Wars films, Swingometers have recently become more and more complicated. This is perhaps also a consequence of the UK moving to multi-party politics where working out who exactly in power after polling day is getting a bit complicated.

Here’s version 14.0 from 2010:

There's just a big green room

There’s just a big green room (photo BBC)

In a tribute to the original, the charity I work for – Concern Worldwide have been using a Development Swingometer, which pays homage to the original 1959 version and tracks the number of candidates who have signed up to support Concern’s 5 pledges on fighting global poverty and protecting international aid (you can tweet them on that page with some code I mashed up using TheyWorkForYou.com and YourNextMP.com).

Building a Raspberry Pi powered Swingometer

As our low-budget version is a bit simple, for a bit of fun I thought I’d build a slightly more technical version that updates itself automatically. My updated Swingometer uses a servo to move the arrow, and a Raspberry Pi to fetch the ‘swing value’ from a web page when you push the big friendly button. The code I’ve used can be edited to fetch more or less any numerical value from a web page and turn it into a ‘swing’ so It should be possible to repurpose this into a political swingometer, or something that displays anything with a value of 1-180 (the angle of swing possible with a servo).

Push the button for results

Just push the button for the latest results

The back of the frame (£5 from Wilko) shows the Raspberry Pi model A and a little slice of Pi board I soldered up. There are two buttons – the big friendly update button for the front and a safe shutdown button on the back, so I can run this without a screen and keyboard.

Here's the back with an arcade machine button and the wiring to the servo

Here’s the back with an arcade machine button (white wires) and the wiring to the servo (brown – red – yellow wires)

The method I’ve used to control the servo is similar to RaspiTV’s Raspberry Pi flag waving post – although I opted for the version detailed in the Raspberry Pi cookbook over Adafruit’s single servo control. For my power supply I used a D-link powered USB hub to supply both the Pi and the Servo via separate USB cables. This results in a slightly wobbly update of the Swingometer arrow, which suits me fine. For more precise control you can use a dedicated servo control hat which can control up to 16 separate servos.

For the code I modified the Raspberry Pi cookbook tutorial code to fetch a value from a web page using Beautiful Soup. You can install this on the pi using:

sudo apt-get install python-beautifulsoup

Note that this installs version 3 – but you’re still able to do lots of clever things to grab content from web pages. If you want to build a political swingometer I’d suggest grabbing values from the election forecast page. There’s a lot more information about using Beautiful Soup on the web page here.

Here’s the code I’ve used to update the swingometer:

import RPi.GPIO as GPIO
import time
from BeautifulSoup import BeautifulSoup
import urllib2

#change this bit
url="http://www.kimondo.co.uk/swingometer-control/"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())

value = soup.find('span',{'style':'color: #99cc00;'})
value = value.next
value = float(value)

print value

GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 100)
pwm.start(5)
t_end = time.time() + 1

while time.time() < t_end:
        print value
  angle = value/100.0 * 180
  print angle
        duty = float(angle) / 10.0 + 2.5
        pwm.ChangeDutyCycle(duty)

exit()

An alternative I considered was using the Sentiment 140 twitter sentiment tracker API – this tracks positive and negative sentiment towards keyword searches on twitter – although it seems to get fooled by ironic tweets.

If you’ve been inspired to build a Swingometer please share it in the comments! One day I’d quite like to build something like the Weasley clock.

You can ask your election candidates to support Concern’s 5 pledges to fight global poverty and support development here and my fantastic blog about the Development Swingometer on Concern’s website.

, , , , ,

8 Responses to Raspberry Pi powered General Election Swingometer

  1. dougiel May 2, 2015 at 7:10 am #

    Where are you going to get your data on the big day? From the BBC News front page? From a RSS feed?

    • admin May 2, 2015 at 11:04 am #

      at the moment the script is just pulling the value from this page http://kimondo.co.uk/swingometer-control/ the beautiful fruit code just looks for the

      span style=”color: #99cc00;”

      tag and returns the next value – for the Concern swingometer this will be replaced with our election blog which we’ll update.

      For a more general election swingometer you could use either an RSS feed, or grab values from http://www.electionforecast.co.uk/ or the bbc itself depending on how they update the results pages – unfortunately the beautiful fruit method of grabbing values from a page gets a bit confused by flash.

  2. vwillcox May 2, 2015 at 2:48 pm #

    I’m back! Long time no fork! I am going to borrow some of your examples and turn it into a Unicorn Hat Swingometer! Colour coded seat counting thing – with all the lights adding up to all the seats. Then at a glance I’ll be able to see who is winning, loosing etc etc etc.

    Thank you for the idea. When completed I will post back here and on my blog.

    • vwillcox May 2, 2015 at 5:43 pm #

      Ok, this has become more of a SeatOMeter – showing how many seats each party has. Until Thursday – it will be the forecast from http://www.electionforecast.co.uk/index.html

      After then – I hope it will be the actual counted seats.

      Code will be on my blog shortly.

      • vwillcox May 2, 2015 at 8:41 pm #

        http://www.ttinfo.it/17 – Messy code – could be cleaned up with procedures. I will add a Video/Gif tomorrow.

        • admin May 5, 2015 at 8:04 am #

          This is brilliant!

Trackbacks/Pingbacks

  1. Prototype Swingometer powered by a Raspberry Pi | Raspberry Pi Pod - May 2, 2015

    […] Taylor, over at Kimondo.com, has written up his project to create an old-fashioned Swingometer. The Swingometer at the moment does not track the General Election results (on account of us not […]

  2. Talk Tech | Raspberry Pi + Unicorn Hat = UK Election Seat Tracker - May 2, 2015

    […] Thursday 7th May 2015, the UK vote for who they want running the country next. Pete Taylor over at http://kimondo.co.uk/ devised a very cool “Swingometer” to track candidates that have signed up for […]

Leave a Reply