The first step in playing with a Raspberry Pi’s GPIO interface is to turn an LED on and off on command, the Hello World of digital electronics. As fun as that is, it would be more fun to do from my mobile phone using my home wireless network, this article runs through the software that I’ve used to do just that. I haven’t said that these techniques allow real-time web control of GPIO pins because they do not, but I get a response time of about 100ms using wireless ethernet with my Pi which does the job for me.
I’m going to be using Python, as it’s a very popular language on the Raspberry Pi, a bit of HTML and Javascript to provide the user interface and some linuxy goodness to glue it all together. There are many other ways of doing this, but this one is mine. I’m glossing over most of the hardware issues, so please be careful not to blow up your Pi.
The steps can be roughly broken down into:
- Hardware and environment set up
- Python and GPIO
- Python, FastCGI and a web server
- HTML and Javascript
Hardware and environment set up
This article intends to focus on the software side of things, so I’m going to assume that:
- you’ve got a fully functioning Raspberry Pi running a reasonably recent build of Raspbian Linux
- that is connected to your home network through wired or wireless
- that your home network has internet access through a router that uses NAT
- that you’re comfortable using your Pi’s command line interface
With those out of the way, you will also need an LED and a resistor (I recommend a 330 ohm resister, but any value between 220 ohms and 3k3 ohms will do for this exercise) and some way of wiring those up to the Pi’s GPIO pins. One of the best ways is to use a breadboard and Adafruit’s Pi Cobbler, but you can also just twist the wires together with insulating tape.
Connect the LED and resistor in series between the Pi’s ground pin and one of its GPIO pins, I’ve used 18.
Python and GPIO
A version of the RPi.GPIO python library is bundled with Raspbian Linux, so we’ll just use that for now to keep things simple. The following Python code will configure GPIO pin 18 as an output and then turn it on. This code has to run as root, so you need to save it to a file and then run it with:
sudo python myFile.py
1234567 |
#!/usr/bin/python
import RPi.GPIO as G # reference the GPIO library
G.setmode(G.BCM) # use the ‘BCM’ numbering scheme for the pins
G.setup(18, G.OUT) # Set pin 18 as an Output
G.output(18, True) # Turn it on
raw_input(‘Press return to exit’)
G.cleanup() # Tidy up after ourselves so we don’t generate warnings next time we run this
|
To take this to its next level, we can allow for some user input by reading something from the terminal:
12345678910111213 |
#!/usr/bin/python
import RPi.GPIO as G # reference the GPIO library
G.setmode(G.BCM) # use the ‘BCM’ numbering scheme for the pins
G.setup(18, G.OUT) # Set pin 18 as an Output
while (True): # keep going around this loop until we’re told to quit
key = raw_input(“Enter ‘w’ for On, ‘s’ for Off and any other key to quit. You’ll need to press enter after each character: “)
if key == “w”:
G.output(18, True) # Turn it on
elif key == “s”:
G.output(18, False) # Turn it off
else:
break # leave our loop
G.cleanup() # Tidy up after ourselves so we don’t generate warnings next time we run this
|
Python, FastCGI and a web server
This is great, we’ve now got a small program running on the Pi that waits for a human to command it to turn the LED on and off, and then it does as its told, but we can take the next step of hooking it up to a web browser. To do this, we need to turn the Raspberry Pi into a web server and then create a small website on it.
One of the great features of Raspbian Linux is that it’s got a really good repository of software that easy to install, we’ll be calling on that repository to install the lighttpd Web Server and the Flup Python library to help us talk to that Web Server using FastCGI.
For more detail: Controlling a Raspberry Pi’s GPIO over the network