Easily measure air quality, avoid polluted areas, and improve your health and that of your community.
Story
Introduction
Air pollution is a topic that you hear about frequently, however there are a very limited number of consumer products for the measurement of pollutants. Air quality is monitored by corporations, cities and universities but I believe consumers should be able to monitor their own environment.
Pollutants are very localised, so the data available for your city might differ from your road, your bedroom or your commute. This product aims to give you the ability to avoid polluted areas to improve your health and the health of those in your community.
Data is collected by the particulates sensor and is combined with readings of temperature, humidity and air pressure. (Location and mapping of visited areas coming soon). This data is then transferred to the cloud, where it is visualised on a dashboard.
Most solutions are either too costly or too heavy to carry around. My device will be low-cost and lightweight bringing air quality monitoring to a wider audience. This will allow you to recognise the importance of the air we breathe and give you the power to change your environment.
Step 1: Setting up the Pi Zero W
Firstly you will need a Raspberry Pi with an installation of Raspian (I used Raspian Jessie). You could also follow all of these steps using a different Raspberry Pi device if you wish.
Enabling the serial port, I2C and GPIO
In order to interface with the sensors, the Raspberry Pi must be setup to allow connections via the GPIO using both I2C and Serial. This can be done using the configuration tool with the following command.
sudo raspi-config
This will launch the interface shown below.
Then proceed to Interfacing Options. Enable I2C by selecting it in this menu and then pressing Yes.
Then to enable Serial, choose Serial on the menu. Select No for Serial Console and Yes for Hardware Serial Port.
You can now click Finish, you may be prompted to reboot. Go ahead!
Install Hologram libraries
BenStr from Hologram has created a fantastic guide detailing how to install the required libraries to get your Hologram Nova up and running.
I recommend you check it out here:
https://www.hackster.io/hologram/add-cellular-to-a-raspberry-pi-with-hologram-nova-ea5926
Once done, congratulations! Your Pi is now ready, time to start playing with some hardware.
Step 2: Connecting the Hologram Nova
Firstly let’s give this shiny exciting Hologram Nova a try.
I recommend following this other guide (also by BenStr) to get yourself setup with the Hologram Nova. It takes you through how to register your sim-card up to how to send some basic commands up to the Hologram server.
https://www.hackster.io/hologram/hologram-python-sdk-sending-data-45f305
If you followed the guide, you should now be able to send messages up to the Hologram Cloud and see them arriving in your dashboard. Nifty right?
Step 3: Connecting the PMS5003
Now let’s get the particulate sensor set up. I chose the PMS5003. There are other Plantower PMS models such as the 3003 and 7003. I’d recommend using the 5003 because it comes with a cable, but all should work with this guide.
Now I decided to cut off the connector that came with the device because I couldn’t find anything that would fit into the other end. I will keep scouring the internet and let you know if I find one.
Instead, I soldered the wire onto a 9 pin male serial connector, so that I could attach dupont cables to the female side and use those to interface with the Pi.
Below you can see the pin out of the PMS5003. It may seem like a lot, but we only need to use 4 of the connections. RX and TX for serial transmission as well as 5V and GND for power.
Here is a diagram of how I connected it to the Pi. The Hirose adapter is representing the PMS5003. I have colour-coded the wires to match those on the sensor.
You will now have something that looks a bit like this. My Pi is in a case, but otherwise everything else should be the same.
I found this great program written by Thomas-Tsai on GitHub that works with all PMS x003 sensors. I recommend running it on your Raspberry Pi at this point and see if you can see data being printed.
Software
The program is listed in the code section. You may need to change:
91: pmdata=air.read("/dev/ttyAMA0") to pmdata=air.read("/dev/ttyS0")
Once working, you should see an output like this:
[2, 8, 5, 2, 8, 5]
These values indicate the levels of PM1, PM2.5 and PM10 particulates in the air. The first three are in ‘lab conditions’ and the final three are in a standard atmosphere. The second set are the useful ones for most applications.
Side note: if you having difficulty, you can edit line 6 to get a more detailed output.
6: debug=0 can be changed to debug=1
Step 4: Connecting the BME280
Now to setup the BME280. This little device can provide you with pretty accurate measurements of temperature, humidity and pressure. Very nice.
It uses I2C protocol so is pretty simple to connect too. Just four wires once again, 5/3.3V, GND, SCL and SDA. Here’s a diagram showing how to connect it.
Now lets test it. I recommend using the Adafruit_Python driver for this. It can be found here, or in the Code section of this project.
In order to use it, you will first need their GPIO package. Issue the following commands to get everything downloaded to your device:
sudo apt-get update
sudo apt-get install build-essential python-pip python-dev python-smbus git
git clone https://github.com/adafruit/Adafruit_Python_GPIO.git
cd Adafruit_Python_GPIO
sudo python setup.py install
cd
git clone
The Adafruit_Python_BME280 driver comes with a few examples. Issue these commands to change the working directory to where the program is and then run it.
cd Adafruit_Python_BME280
python Adafruit_BME280_Example.py
If it runs successfully, you should see values of temperature, pressure and humidity printed to your command window. If not, check your connections or follow the steps in this post that explains how to reduce the I2C baud rate of the Raspberry Pi.
Step 5: Configuring Hologram.io
After following the guide posted above, you should have most of your Hologram Dashboard set up. All that is left to do is to create a route for Hologram to forward the data to Losant. Losant is the platform I have chosen for displaying the data. You can use Hologram routes to pass your information to any service that supports web-hooks if you wish.
Start by creating a new route. Give it a name and select the topic with your device name. Then in action select Losant Device Send State (Beta).
You will need to make a Losant account in order to fill out the remaining information. Head to Losant.com
and sign up for a free developer account.
- Firstly navigate to Applications at the top of the screen
- Then create a new application – give it a name and a description
- Next click on Security along the menu bar
- Choose Application API Tokens in the left menu
- Give it a name, ensure Token Permissions is set to all, then create it
Copy the token and paste it into the hologram box. I highly recommend you also save it or press download the file.
Next up is Application ID. You can find yours by pressing on the name of your application in the top menu bar. The ID is shown on the right hand side of this page. Copy this in.
In order to get a Device ID we need to create a device. This is the object that represents your Raspberry Pi and receives the data it transmits.
- Choose Devices in the menu bar
- Then Add Device followed by Create Blank Device
- Give it a name and a description
- Ensure Device Type is set to Standalone
- Device Attributes is where you define the variables that will be sent from your Pi and their type. Fill it in like this:
- Then click Create Device
You should now see the Device ID at the top right of your screen, fill it into the relevant box on Hologram.
Last box (phew!).
JSON Payload is where you tell Hologram how you want the data sent to Losant to be formatted. <<decdata>>
is the decoded data Hologram receives from your Pi. You can access JSON fields in your decdata like this: <<decdata.field>> Here is how to format it:
{
"PM1": "<<decdata.PM1>>",
"PM25": "<<decdata.PM25>>",
"PM10": "<<decdata.PM10>>",
"Temperature": "<<decdata.Temperature>>",
"Humidity": "<<decdata.Humidity>>",
"Pressure": "<<decdata.Pressure>>",
"datetime": "<<received>>"
}
Click add route and you’re done with the Hologram side of things 🙂
Step 6: Configuring Losant
At this point, Losant will be able to receive your data but only display it in a log file. Losant has some fantastic data visualization tools so lets make use of those.
Select Dashboards at the top of the screen and press Create Dashboard. Write in a name and a description.
Now you will see a screen asking you which block you would like to add. Blocks are the segments that make up your dashboard. By arranging various elements you can design it however you wish. In this tutorial I will show you how to set up a couple of blocks, but I’d recommend experimenting with them.
For now, let’s make a Gauge Block to display PM1 data. The Gauge block takes the following parameters:
- Block Header Text – Choose a title for the block, such as “PM1 Concentration”
- Application – Select the application with your device
- Gauge Type – Choose Dial for now and input 0 and 500 as the min and max.
- Precision Type – Set it to 3 significant figures
- Data Type – Historical; this means the last received data-point will be displayed.
- Device ID – Select your device from the drop-down menu
- Label – I typically use this field for units, so in this case µg/m3
- Attribute – PM1
Press Add Block and you should see your first block on your dashboard!
Right now, you will see N/A rather than a number, but we will get to that once the program is run on the Pi for the first time.
Before that, let’s make another block. Press the settings cog on the right hand side, then choose Add Block. Choose Time Series Graph. This block lets you graph your data over a chosen timescale. You can plot multiple variables on one plot for comparison. Here are the parameters to fill in:
- The first few are self-explanatory and are filled in as with the other block
- Duration – Set it as 60 minutes with one point every 5 minutes for now
- Device ID – Select your device
- Attribute – Select PM1
- Series Label – PM1
- Display as – Press the line graph button
Next, press Add Segment and fill in the boxes again for PM2.5 and PM10. Once completed click Add Block.
You should now have two blocks ready for data. So let’s populate them.
Step 7: Running the program
Navigate to the Code section of this project. Here you will find a program called particulater.py. Create a new python file on your Pi and copy this program into it. Move (or create and copy-paste) the Adafruit_BME280.py program into the same directory. This was one of the files used to test the BME280 earlier.
Now enter into the command line
sudo python particulater.py
You should see an output like this, if Hologram receives the data successfully:
Check Losant and wait for your blocks to update. You are finished!
I recommend adding additional blocks to display the rest of the values. For instance you could set up gauges for pressure, humidity and temperature. I’ll leave this for you to try. Here is an image of how I’ve laid out my dashboard.
Step 8: Bonus – Ways to display the data
Losant has the ability to format your blocks based on their value. For instance you can change the gauge colour based on the air quality like this:
This particular set of data was captured when there was smoke outside of my room. I assigned the colours based on the values from the EPA air quality index.
To set this up yourself, edit a block and scroll down to add conditional. Write a series of conditional statements that define which colour should be used, like this:
Step 9: Coming Soon – GPS Mapping and AQI Calculation
GPS location data and mapping is coming very soon, so watch this space. Everything up to this point is fully functional and ready to use.
Having never worked with a Raspberry Pi, Hologram, Losant or the PMS5003, this project was quite ambitious for me and isn’t perfect (yet™). Feel free to offer suggestions, provide feedback or ask questions. I’m always looking to improve my work and I hope you find what I’ve done interesting and useful for your own projects.