Categories
Activism Raspberry Pi Work stuff

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

https://twitter.com/ConcernUK/status/593761711020183552

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.

https://twitter.com/kimondo/status/595957624614563841

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.

Categories
Gadgets Random Raspberry Pi

Digital Holga part 3 – taking photos with the inputs

This is my follow up to my previous posts about building a digital camera out of a Raspberry Pi Model A and a medium format Holga 120 film camera.

My aim being to build a hackable portable digital camera which hopefully would be able to capture images not possible with a normal digital camera, and to recapture the unpredictable spirit of film cameras (without the expense of the chemicals).

In my first post I fitted a Raspberry Pi model A and camera board into a hollowed out Holga 120. The Holga’s plastic construction makes it easy to work with and it’s roomy enough to accommodate a Pi without having to remove components from the board.

Pi camera case with Pi fitted
Here it’s sitting in it’s case. Snug.

In my second post I added the final inputs to the Holga case – a rotary selector switch with 3 positions, a push button switch on the side (with a trigger input on the bottom) and a power switch. I added a filter adapter to the front lens and I also took a few pictures by remotely triggering commands on the Pi.

Holga 120d Raspberry Pi Camera Case
Here’s the 3/4 view with the big clunky switch. It now switches the mode of the camera.

I’ve now connected up the switches and added 2 LEDs to the Raspberry Pi, making the Holga 120d a practical portable digital camera.

The rotary switch combined with the trigger switches effectively gives the Holga 120d 3 input buttons. The 2 LEDs (blue and orange) indicate the ‘mode’ the camera is in when I push the shutter button.

Each of the 3 ‘modes’ is programmable – at the moment I have 2 set up – one takes a photo which is saved to the Pi’s SD card, and the other performs a safe shutdown. It’s possible to add any command to the python script – and I’m working on an additional artistic mode which I’ll detail in a later blog.

I soldered all the inputs & the 2 LEDs to a Slice of Pi Breakout Board – these are available for around £5 and fit neatly over the Raspberry Pi without taking up too much space.

Raspberry Pi Holga 120d internals
Bit of a cram, but it all fits.
Holga 120d LED viewfinder
These light up to tell you the mode the camera is in.

From the board leading to the case, the green wires are to the push switch and trigger input, the 3 red wires & black to the right lead to the rotary switch and the red and black pairs lead to the LEDs.

The yellow wires just out of shot are to the flash hot-shoe. I did consider connecting this using an opto-isolator LED and there this space to do this at a later date.

There is also (just) about enough space to add a real time clock module – I used the Adafruit DS1307 Real Time Clock which connects to the 5v, GND, SDA and SQL pins on the slice of pi board. I followed Adafruit’s instructions (leave the resistors off the board!) and it works well.

The LEDs themselves slot into the viewfinder and light up according to the position of the switch. So in mode 1 the blue LED lights up, 2 both LEDs light and in 3 just the orange LED. This gives a bit of useful feedback to show that everything is working properly and the Pi is taking pictures when you click the shutter button.

For an easier to understand diagram – here’s the circuit laid out in Fritzing.

GPIO 23,24 & 25 are the inputs, with GPIO 17 and 18 driving the 2 LEDs. The switch at S2 is the rotary selector switch.

I used 330 ohm resistors for the LEDs and 10K resistors for the GPIO inputs.

Holga 120d wiring schema
Try on a breadboard first

 

The code I used is really simple.

Make sure you’ve run sudo apt-get update and have installed the Camera py modules first.

#!/usr/bin/env python

import time
import picamera

from time import sleep
import os
import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)

while True:
        if ( GPIO.input(23) == False ):
                print('Orange: Taking photo')
                GPIO.output(17, True);
                date_string = time.strftime("%Y-%m-%d-%H:%M:%S")
                with picamera.PiCamera() as camera:
                        camera.capture('image' + date_string + '.jpg');
                sleep(2);
                GPIO.output(17, False);
        if ( GPIO.input(24) == False ):
                print('Blue')
                GPIO.output(18, True);
                os.system("sudo shutdown -h now")
        if (GPIO.input(25) == False):
                print('Both')
                GPIO.output(17, True);
                GPIO.output(18, True);
                sleep(2);
                GPIO.output(17, False);
                GPIO.output(18, False);
        sleep(0.1);

In the code above, one of the settings takes a photo which is saved with a time / date stamp, the other safely shuts the Pi down, and the other is yet to be used.

The easiest way to get your photos off the Pi is to use Filezilla and to connect to your Pi using sFTP – this uses your SSH login and enables you to easily to download and delete photos.

For connecting to your Pi remotely see my previous post about finding your Pi on your network.

I’ve had lots of interesting feedback about the Holga 120d – with the Pi model A (and potentially a smaller model A+ in the works) there are lots of opportunities for adding a digital camera to an existing film camera.

Now i’ve got my Holga 120d up and running I’m going to do some experimenting – my aim being to capture images that you can’t recreate with Instagram. If you’ve got any creative ideas to share please leave them in the comments below!

Categories
Gadgets Raspberry Pi

Raspberry Pi model A arrives…

A new Raspberry Pi model A arrived in the post yesterday. I’ve got a very specific project in mind for this slimmed down model – but for now a couple of pictures.

It lacks Ethernet and has 256mb of RAM, but consumes less power (about 1/3rd) –  than the existing model B version. It’s also a bit cheaper so you can now buy a computer which can do high definition video for less then the price of a blu-ray movie.

Raspberry Pi model A (fisheye view)
Via a big bendy eye camera…

From the side it’s noticeably thinner (1.7cm deep allowing for the SD card compared to 2cm) than the existing model B – the widest components on the board being the video connector, followed by the audio out and GPIO pins – the single width USB and HDMI connector being about the same width. So there’s also some scope here to make it even thinner with a bit of careful desoldering:

Raspberry Pi model A side view
and from the side

A year ago I’d never consider bringing a soldering iron near a PC, but perhaps that’s the effect of the super-low-cost computer.