Camden Open Data API & Physical Computing
In 2017 Camden published a tutorial on how to use the API in Camden's open data platform - check it out here. This tutorial looks at physical computing and how it can be used with the API to build an electronic air quality indicator so you can see at a glance what the current air quality in Camden is like.
This takes the form of three LEDs in a 'traffic light' formation that light up depending on the average air quality value taken from this dataset; it can be achieved with a Raspberry Pi computer, a few basic electronic components and a small Python script.
The tutorial that can be done at home, in schools or in coding clubs - ideas to expand this project are listed at the end of the tutorial.
To keep this tutorial brief, it assumes the reader has a basic knowledge of APIs, Python, Bash and electronics...there is a risk of damage to your Pi if you're not familiar with the GPIO (General Purpose Input/Output) header so please do some research before attempting this tutorial.
How it works
The Python script gets the average Nitrogren Dioxide (NO2) level reading in Camden for the current date from this dataset using the Open Data API.
The script compares the API value with some predefined thresholds (these can be changed in the code) and lights the corresponding LED attached to the GPIO header.
Using the API Documentation and SoQL functions you can change the API call to get different data, for example data from a specific air quality monitor.
Step 1 - gather the hardware
To build the air quality status monitor you'll need:
- A Raspberry Pi computer (not pictured) - any model should work but it needs to have internet access, also please note the GPIO layout differs between models
- 3X LEDs (red, amber and green) - 5mm LEDs with a forward voltage of 2v and a forward current of 20mA were used here
- 3X resistors - 1/2 watt resistors between 100 - 330 ohms
- Assortment of jumper wires - to connect components
- A breadboard - to put all the components on
Step 2 - assemble the hardware
Ensure the Pi is turned off for this part. The following pin numbers refer to the layout on a Raspberry Pi 3B+.
- Insert the three LEDs into the top section of the breadboard, noting which side of the LED has the short (negative) leg
- Connect a jumper wire from the short leg of each LED to an empty column in the top row of the breadboard - this is the ground
- Connect a jumper lead from that column to one of the ground pins on the Pi, e.g. pin #9
- Connect a resistor to the long leg of each LED so that it bridges the top section to the bottom section (the middle section isolates the top and bottom)
- Connect a jumper wire from each resistor to a pin on the Pi, e.g. #11, 13 and 15 (you can change the pins in the code)
The end result
Depending on the Pi model and the GPIO pins you want to use, it should look something like this - I couldn't find the right jumper wires so the black ground wire from the breadboard turns into the brown ground wire on the Pi and the orange jumper is not used!
You may want to use different GPIO pins, just amend the pin numbers in the code below.
Step 3 - writing the program
There's a variety of ways to do this, here we use the Bash command line and the Nano text editor to create a Python script. Python 2.7 is used, but with a few changes to the urllib code Python 3 should also work.
On the command line type pwd and hit enter - this will show the current directory. Change the directory if you want to store the script somewhere else.
On the command line type sudo nano air.py - this will create a file called air.py and open it in Nano for editing
Type in the code below, you might want to make some tweaks - for instance the thresholds used to determine which LEDs to light up or the GPIO pin assignment (check out the comments in the code)
Save and close Nano by hitting Ctrl-X > y > Enter
Step 4 - running the program
Check all the connections between the LEDs and the Pi and then run the program - on the command line type sudo python air.py and hit enter. Within a couple of seconds you should see the LED that corresponds to the current air quality light up!
Summary and next steps
This is a very quick tutorial to demonstrate APIs and physical computing. There are several ways this script could be updated to improve the functionality:
- Run the program every hour using a loop in Python or even better a Cron job
- Make the program more robust by checking an internet connection is established
- Flash the green LED when the program has run successfully before setting the status or flash the red LED if it's failed - enabling the Pi to be run 'headless' (without a screen attached)