Accessing GPIO from a web server

After successfully accessing the GPIO pins from the command line, my next goal was to read and write pin values from a web site. The first step is to install a web server, and the obvious choice would normally be Apache, but with the Raspberry Pi being limited in CPU and memory resources, I decided to check out lighty (lighttpd) as it has a small memory footprint. Installation is reasonably easy:

sudo groupadd www-data
sudo adduser www-data www-data
sudo usermod -a -G www-data www-data
sudo chown -R www-data:www-data /var/www
sudo apt-get install lighttpd

The first four steps might not be necessary on  distributions more recent than Debian “Squeeze”, but for me, the installation failed with an error that user “www-data” doesn’t exist. Web files can now be placed in “/var/www”. At this point it’s time to decide on a server side programming language, my initial thoughts were PHP or Python. As Python is the “official” language of the Raspberry Pi, I decided to go with that.

To get Python to work with lighttpd, you need to enable CGI:

Open the lighttpd configuration file (/etc/lighttpd/lighttpd.conf), and uncomment the “mod_cgi” line (remove the # from the beginning of the line if one exists) or add this line if not present.

server.modules = (

Add the following to the bottom of the file:
$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" )
}Restart the lighttpd daemon:

sudo service lighttpd force-reload

We can now create python files in the /var/www/cgi-bin/ directory. As it’s a bad idea to run a web server as a root, you need to access the GPIO from a non-root account. In order to do this, I used the “gpio” program from the WiringPi package. I created a “ReadGpio” function that looks like this:

# Read the GPIO pin using the gpio application

import subprocess as s

def ReadGpio(pin) :
process = s.Popen([“/usr/local/bin/gpio”, “-g”, “read”, pin], stdout = s.PIPE)
data, _ = process.communicate()

data = str.replace(data, “\r”, “”)
data = str.replace(data, “\n”, “”)

return data

Now that I’ve got GPIO integrated with a web server, I’m going to start looking at a Python web framework, as typing “print ‘<open bracket>Something</close bracket>'” gets a bit tedious :).

Posted in GPIO, Hardware, LEDs, Raspberry Pi | 3 Comments

Programmable LEDs (Basic)

Having setup an LED to be permanently lit via the 3.3V GPIO pin, the next step is to get the LED to light on demand, via a GPIO control pin. There are several pins to choose from, I used pin 17. It’s a similar circuit to the previous example, except we move the anode wire from the 3.3V pin to pin 17.

Once you’ve got the circuit wired up, we need to switch over to the software in order to get the LED to light up. There are many different languages and libraries you can use, but as this is the basic installment, we’re going to use some pre-made libraries – WiringPi and RPi.GPIO. WiringPi is for shell access and RPi.GPIO is for Python access.

To control the LED by the command line, type the following into the shell:

gpio -g mode 17 out
gpio -g write 17 1

This will set the pin to output mode and then turn the LED on, and to turn it off again you can type the following:

gpio -g write 17 0

To control the LED by Python, you need to write a little script:

import time
import RPi.GPIO as GPIO
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.HIGH)
GPIO.output(17, GPIO.LOW)

This will turn the LED on for 5 seconds, then turn it off again before exiting the script. Like I say, all very basic stuff, but we can expand on this from here.

Posted in Circuits, GPIO, LEDs, Raspberry Pi | Leave a comment

Park Tools Cyclone Chain Cleaner Review

I’ve recently picked up the Park Tools CG-2 Chain Gang Chain Cleaner Set from Wiggle after it was heavily discounted. I’d always been a little dubious about how good this tool could be and have previously opted for a manual cleaning process which involved removing the chain and scrubbing with a brush and degreaser.

The tool itself was easy to attach to the chain, simply remove the top, then replace it with the chain between the top and bottom pieces. To perform the cleaning, just hold onto the handle and then move the pedals in a backwards rotation. It can be a bit of a mucky operation as some of the cleaning detergent will slosh about in the cleaner, occasionally spilling out onto the ground – so make sure you put something underneath the bike before you begin.

As you can see from the photo, the degreaser ended up completely black from dirt, despite the chain appearing relatively clean to start with. What you can’t easily see from the photo is the vast amount of metal that gets sucked to the bottom of the unit by the integrated magnet. Metal that would otherwise be rubbing and wearing away at the chainset components.

After this shot was taken I rinsed out the chain cleaner and started the process over again, removing even more dirt, oil, grit and metal. I was thoroughly impressed with the cleaning ability of the Cyclone, it was quick and easy, and resulted in a chain which looked cleaner than when I first bought the bike! I’ve not tried any other chain cleaners, but compared to scrubbing the chain manually, this beats it hands down, every time.


Posted in Cycle Maintenance, Reviews | Leave a comment

Let there be light! (Update)

Following on from the previous post I have a correction to make. The 270 ohm resistors that shipped with the LEDs aren’t overkill for the 3.3V Raspberry Pi GPIO circuit. There must have been a mistake in the way I built the original circuit, since when re-trying it the other night, it worked perfectly fine (albeit slightly dimmer than intended).

Posted in Circuits, Hardware, Raspberry Pi, Resistors | Leave a comment

Let there be light!

In the beginning…

As a budding electronic engineer, where should you start? This might be a question with many correct answers, but I decided to start with a simple single LED circuit. I bought the following equipment to get me started (all from Oomlout):

Super Bright LEDs (Red, Green, Blue & White, with 270 ohm resistors)
Breadboard Jumper Wires
Digital Multimeter

I knew there would be a learning curve with this, so I figured a multimeter is an essential bit of kit. After all, you wouldn’t get far attempting to write software without using a debugger.

When I received the delivery I immediately realised I’d forgotten to buy some essential kit – female to male jumper wires, in order to breakout from the Raspberry Pi GPIO to the breadboard. Luckily I remembered I had some old IDE cables lying around which I used as a stand-in replacement. As the cable supports 40 pins, and the Raspberry Pi only has 26 pins, some of it hung over the end of the board (notice I haven’t actually pressed the cable down properly in the following shot).

Raspberry Pi GPIO with IDE cable

Once the IDE cable was connected I was able to use the male-to-male jumper wires to breakout onto the breadboard. This was a bit tricky and took a bit of trial and error to get the correct pins connected. I used the multimeter to make sure I was connected to 3.3V and ground pins. Following this I connected up one of the LEDs and the associated 270 ohm resistor. Here’s the first circuit:

Simple LED circuit

Any seasoned electronic engineer reading this may already have spotted a problem – but I was stumped, why wasn’t the LED lighting up?! It turns out that you can’t blindly use any resistor you like, even if they were shipped with the LEDs.

Use the science!

After a little bit of Googling around, I got re-acquainted with an A-Level physics formula which had fallen out of my memory.

Ohm’s law

V = IR

[Where V = Voltage, I = Amperage, and R = Resistance]

It’s important to note that V refers to the voltage differential, meaning the source voltage minus the device voltage (i.e. source voltage – LED voltage). This means I need to find out the specifications of the LEDs I’ve purchased and plug them into the above calculation. Luckily Oomlout provide the specifications for the LEDs on the purchase page.

LED specifications

Red – 20mA@2.0V (max 2.5V)

Green – 20mA@3.2V (max 3.6V)

Blue – 20mA@3.0V (max 3.6V)

White – 20mA@3.0V (max 3.4V)

If we plug the values for the red LED into Ohm’s law, we will see the that the 270 ohm resistor is overkill for the Raspberry Pi’s GPIO pins (we use the red LED because it has the lowest power requirements, therefore the largest required resistance).

R = V ÷ I

R = (3.3 – 2.0) ÷ 0.02

R = 65 ohms

This confirms that you will need a set of resistors with much lower resistance than the ones provided with the LEDs. Time to break open the wallet again!

Posted in Circuits, Hardware, Raspberry Pi | 1 Comment

A journey into hardware

Hello, and welcome! Now, this might be the third or fourth attempt at starting a blog, but I’m convinced this time it’s going to be different – I actually have something to write about! A few weeks ago my Raspberry Pi arrived and I had (grand) delusions of giant robots roaming throughout the house, all controlled by The Pi. There was only one problem with this – I know nothing about electronic circuits. I used to know a little bit, but then I forgot, shortly after I became old enough to drink. After unpacking I had to do some basic setup. This was all fairly trivial stuff, mostly just copied verbatim from the following tutorials:

1) Preparing the SD card (with Debian “Squeeze”, username = pi, password = raspberry)

2) Resizing the partition to fill the whole SD card (best to do this on a fresh install, just in-case you mess it up)

3) Enabling SSH

4) Mounting devices and transferring files (remotely with WinSCP)

5) Changing the hostname

6) Changing your password

7) Changing the Message of the Day

That was enough to get to a decent starting point, allowing the Raspberry Pi to be plugged into power and networking then being completely controllable from a remote client.

Posted in Hardware, Raspberry Pi | Leave a comment