RPi / AirPi air quality station – Setup

I’ve recently been working with the Atmospheric Chemistry group at the University of Leicester, setting up some AirPis. The AirPi is a small board which pairs with a Raspberry Pi, and reads a number of air quality measures. There will doubtless be future posts about the software, sensor accuracy and reliability, etc., but this one is about just setting the thing up in the first place.

AirPi on top of a Raspberry Pi. © Haydn Williams 2014

AirPi on top of a Raspberry Pi. © Haydn Williams 2014

Step one is to get the Raspberry Pi up and running. I downloaded Raspian Wheezy from the official Raspberry Pi downloads website, and installed it using the SD card slot on my Macbook Pro, although there are instructions for other OSes too.

Once it’s installed and the Pi is running, we need to change the password for the default account to something else. You can do this through the “advanced” menu of the initial config screen when the machine first boots. Alternatively boot the machine yourself manually, log in as the default account (username: pi, password: raspberry) and run the following:

passwd

You can also use that initial config screen to set the hostname for the machine (in the “advanced” section) and to disable SSH if you’re not going to be using it.

Now, set the time zone:

sudo dpkg-reconfigure tzdata

Set up Networking
Next we’ll want some kind of internet connectivity. If you’ve got a Model B board then you’ve got an ethernet port and everything is very easy. If you’re on a Model A board then it gets a bit more difficult – the easiest option is a USB-to-ethernet adapter and SSH. A plus is that when the AirPi is running in the field and recording data locally you don’t have to worry about security issues because you can just unplug the adapter. Alternatively you can go with wifi – I used the Edimax EW-7811Un because it’s (allegedly) supported by Raspian Wheezy, so there was no messing around with extra drivers. There are lots of instructions out there about how to get wifi working once your dongle is running; I’m sure some are better than others, but these worked for me. They only worked for a short time however; the USB-to-ethernet option proved the easiest and most reliable in the long-run.

You can edit ‘/etc/network/interfaces’ to set up your network options. There are plenty of instructions out there, but for wired access it might look like this (although I’ve fudged the IPs in the example below):

auto lo

iface lo inet loopback

# Haydn's House
#==============
iface eth0 inet static
address 192.168.0.123
netmask 255.255.255.0
network 192.168.0.1
broadcast 192.168.0.255
gateway 192.168.0.254
nameserver 192.168.0.1

Note that you may sometimes need to also put DNS details in /etc/resolv.conf

Upgrade Software
Once you’ve got internet, it’s time to update everything which has been updated since your version of the OS was released:

sudo apt-get update
sudo apt-get upgrade

It’ll take some time, depending on your internet connection speed.

Disable hardware
If appropriate, you can disable the onboard sound card:

sudo vi /etc/modules
# Then make sure there is a hash sign (#) in front of the snd-bcm385 line - it should look like this:

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

#snd-bcm2835

and one more thing

sudo vi /etc/modprobe.d/raspi-blacklist.conf

# Then add the line below at the end:
blacklist snd_soc_pcm512x

Another thing you might want to consider is to force output over HDMI (i.e. never revert to component out). Just add the following line to /boot/config.txt:

hdmi_force_hotplug=1

This is useful because in the absence of an HDMI connection at startup (e.g. if the Pi is started headless in the field), it defaults to component-out being active. Setting the above option means that HDMI is always activated, so you can bring the Pi back to the office running on batteries and plug it straight into a monitor without having to reboot. This means you can also use the monitor to stop processes gracefully etc., which you might not have been able to do in the field without a monitor. Nice.

At this point, you might want to give some though as to whether you’ll be accessing the data by SSH or SFTP on the Pi, or inserting the SD card into another computer. If it’s the latter, it’s probably best to read the section about creating a FAT partition in my post on advanced usage. It’s not pretty, but don’t say I didn’t warn you!  :)

 

Now we can prepare for the AirPi.

The AirPi.es website is not being maintained at the moment (July 2014), and there are several sets of instructions floating around about how to set the board recording data. I’ve outlined below the method I used, which seemed to work fine.

Install git and some python tools
Git is to help us download various bits of code, a number of different Python tools because that’s what the AirPi code is written in, i2c tools because that’s how one of the sensors communicates.

If you’re running an old version of Raspbian then you’ll need to install quite a lot of stuff:

sudo apt-get install git-core python-dev python-smbus python-setuptools python-requests python3 python3-dev python3-requests libxml2-dev libxslt1-dev python-lxml gpsd gpsd-clients python-gps i2c-tools

I have most recently done this with the 20th June 2014 release, in which git-core, RPi.GPIO and python3 already installed. This means you have marginally less to do:

sudo apt-get install python-dev python-smbus python-setuptools python-requests python3-dev python3-requests libxml2-dev libxslt1-dev python-lxml i2c-tools

 

What You Don’t Need
Contrary to other installation instructions floating around, you do not need to install Python EEML (it’s no longer used in the AirPi code), nor do you need to use PIP to install requests or rpi.gpio (they’re both installed using apt-get along with everything else above).

 

Install Twitter support for Python
This is used to Tweet when there is an error, via the new ‘notifications’ module.

easy_install twitter

 

Set up i2c
i2c is the bus over which the BMP085 pressure sensor communicates with the main Raspberry Pi. There are some good instructions here, summarised below.

sudo vi /etc/modprobe.d/raspi-blacklist.conf

# Comment out the third line in raspi-blacklist.conf (blacklist i2c-bcm2708)

sudo vi /etc/modules

# Add "i2c-dev" (no quotes) onto a new line

sudo adduser pi i2c
sudo reboot

sudo i2cdetect -y 1
# If the AirPi is accurately detected over i2c, you should see the following:
# 0 1 2 3 4 5 6 7 8 9 a b c d e f
# 00: -- -- -- -- -- -- -- -- -- -- -- -- --
# 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# 70: -- -- -- -- -- -- -- 77

 

Test the GPS
If you’re using an Adafruit Ultimate GPS on your AirPi board, you can enable support by running the following:

sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock

and then check it’s correctly reporting your location:

cgps -s

Note that you’ll need to run the gpsd command after each reboot to start the daemon again (or make it run on startup).

 

Get the AirPi code
This is the code which will take data from the sensors and either print to screen, save, or upload to an online service. There are a few different packages available – the rest of this post will assume you use one of the first three (all variations of the original code):

  1. The original Python-based AirPi.es code: https://github.com/tomhartley/AirPi
  2. A revised version which includes CSV output: https://github.com/guruthree/AirPi
  3. My further-revised version which has a couple of tweaks for running headless: https://github.com/haydnw/AirPi
  4. oswabox – Written in C and therefore needs compiling. Didn’t work for me, but the developers are promising great things: https://github.com/mgrennan/oswabox
  5. For node.js users, there is support for the BMP085, but seemingly not much else.
  6. There is also a nitrogenjs package which supports the BMP085 (and it’s replacement, the BMP100).
  7. There has been work to support the emoncms package in the original software; emoncms is described as providing “open-source energy visualisation“.

To download and use my version of the code, do the following:

cd ~
git clone https://github.com/haydnw/AirPi

 

Add the AirPi folder to $PATH
This means that you can refer to the main airpictl.sh script without having to type the full path or ‘./’ each time you want to run it. I’ve assumed that the software is installed at ‘~/AirPi’.

sudo vi ~/.profile

# Then add the following to the end of the file:
export PATH=$PATH:/home/pi/AirPi

 

Add a Login Message
This step is entirely optional, but adds a nice message when a user logs in and displays the current status of the AirPi, like so:

=======================================================================
        _               _  _
  __ _ (_) _ __  _ __  (_)/ |
 / _` || || '__|| '_ \ | || |
| (_| || || |   | |_) || || |
 \__,_||_||_|   | .__/ |_||_|
                |_|
IP: 123.321.12.33
[AirPi] Status: SAMPLING
=======================================================================
airpi1:~ $

First we’ll install figlet so that we can produce the banner.

sudo apt-get install figlet

Then create the new dynamic “message of the day” file ‘/usr/local/bin/motddynamic’ as below:

#!/bin/sh

# =========================================
# File:    motddynamic
# Purpose: Show hostname and IP on login.
# Author:  Haydn Williams <pi@hwmail.co.uk>
# Date:    August 2014
# =========================================

clear

hostname=`hostname`
ip=`hostname -I`

echo "======================================================================="
figlet -o $hostname
echo "IP: $ip"
echo `/home/pi/AirPi/airpictl.sh status`
echo "======================================================================="

Once you’ve created the file, we need to load it on login by adding the following lines to ‘/etc/profile’:

# Dynamic motd
/usr/local/bin/motddynamic

 

Make a Backup
If you use a Mac, now is the time to make a backup of the SD card. This is done in the form of an ‘image’ which takes a snapshot of the entire card. This makes it very easy to get the card back to this point again in the future, without having to install everything again manually. There is a very good set of instructions on tutsplus.com so I don’t intend to reinvent the wheel here.


Start recording data!

I’ll add a new post soon which covers how to use my modified version of the software, as it has quite a few extra features not included in the original release from the makers of the AirPi. These include:

  • Added ‘airpictl.sh’ script to control sampling in different modes.
  • Added ability to output average data (e.g. read every 1 min for 10 mins, then output the average for the 10 mins).
  • Added ability to record metadata such as Raspberry Pi serial no. and operator name at start of run.
  • Added ability to start automatically at boot for headless operation (does not require any user interaction).
  • Added ‘Notifications’ module which allow messages to be sent when errors occur. Includes email, SMS and tweet.
  • Standard print-to-screen format tidied up and made more digestable.
  • Can automatically name CSV files and HTTP titles using date and hostname.
  • Added ThingSpeak integration.
  • Added the following new options to ‘settings.cfg’:
    • Greater control of LED behaviour.
    • Can disable error messages printed to screen.
    • Can print to screen in CSV format.