Raspberry Pi Air Quality Monitor Project #2

To complete this project you’ll need:

  • Raspberry Pi Zero W or better.
  • MicroSD Card 8GB or larger.
  • A free account at io.adafruit.com
  • Raspberry Pi Imager software.
  • SDS011 sensor for measuring particulate matter.
  • PuTTY or an equivalent tool for SSH communication.
  • Wi-Fi available for the Raspberry Pi Zero W to connect.

Configure Your Pi Zero W

This project is just like Air Quality Project #1 so we’ll want to refer to the exact same steps for configuring the Raspberry Pi device, whether you’re using a Zero, 3B, or 4. The main difference, in my case at least, is that we’ll be following that 13th step and connecting to the WiFi because I don’t have a micro USB to Ethernet adapter for the Pi Zero W. If you don’t already have the Pi device configured feel free to refer back to “Raspberry Pi Air Quality Project #1” to follow the configuration section before continuing here.

Connect to your Raspberry Pi

With your Raspberry Pi device connected to the WiFi, you can log into your router again, just like in Air Quality Project #1, to find the IP address of the Pi Zero. With that IP address, you can open PuTTY and create a new session and enter the IP address and default credentials for the Pi device.

One thing you might notice when logging into the Pi Zero is that you’re on armv61 instead of armv71.

Additionally, if you’re noticing any lag while communicating with your WiFi-connected Pi Zero over SSH, you can directly connect a keyboard/mouse and monitor to your Pi Zero and enter the following command and reboot your Pi device.

echo "IPQoS 0x00" >> /etc/ssh/ssh_config && echo "IPQoS 0x00" >> /etc/ssh/sshd_config

Install the “pyserial” and “adafruit-io” Modules

In order to read the data from the SDS011 sensor we need to install the pyserial module and in order to push it to Adafruit.io we need the adafruit-io module.

You can install both of these modules with the following command,

pip3 install pyserial adafruit-io

If you run into an error for not having pip3 you can install it with the “sudo apt install” command listed below. Once that is installed try again running the “pip3 install” command listed above.

sudo apt install python3-pip

Write (or copy) a Python script to communicate with the sensor

For this part you can use whatever text editor you like but I’m going to use Nano because I’m already familiar with how to use it.

1. Create a directory for your script to live in and then navigate to it.

mkdir air_quality_monitor
cd air_quality_monitor

2. You can create your python script file by using the “touch” command.

touch aqm.py

3. We can start editing the file by opening it in nano.

sudo nano aqm.py

4. If you haven’t already, you’ll want to take a quick break and navigate over to io.adafruit.com and create your free account/login to get your Username and Key. From here you can copy the following code and customize it with your own Adafruit Username, Adafruit Key, and variable/feed names. Once you have it customized you can press Ctrl+X, then Y, then Enter to save your script.

import serial, time
from Adafruit_IO import Client
aio = Client('YourAdafruitUserName', 'YourAdafruitKey')

ser = serial.Serial('/dev/ttyUSB0')

while True:
        data = []
        for index in range(0,10):
                datum = ser.read()
                data.append(datum)

        pmtwofive = int.from_bytes(b''.join(data[2:4]), byteorder='little') / 10
        aio.send('aqmtwofive', pmtwofive)
        pmten = int.from_bytes(b''.join(data[4:6]), byteorder='little') / 10
        aio.send('aqmten', pmten)
        time.sleep(10)

5. With your script customized and saved, you’ll want to plug in your SDS011 sensor. Plugging this sensor into the Pi Zero may cause it to reboot, I know mine does. Once it has rebooted we can log back in and continue.

6. Navigate over to io.adafruit.com and login to your account. Follow these steps to create a dashboard and block for your data.

1. Click the "+New Dashboard" button. Give your new dashboard a name and description and click "Create".
2. Click on the "Gear" icon in the top right and click "+Create New Block", choose "Line Chart".
3. On the bottom of the "Connect Feeds" page where it says "Enter new feed name", enter the names you chose for the script, in this case it's "aqmtwofive" and "aqmten". So we'll type "aqmtwofive" and click "Create" then type "aqmten" and click "Create" again.
4. Check the box next to the two new feeds that you created and click the "Next step>" button in the bottom right.
5. On the "Block settings" page you can enter a "Block Title" and/or scroll down and click the "Create block" button.

This new block is going to be blank until we get our script going in the next steps.

7. With our data block configured on Adafruit, we can log back into our Pi Zero and start our Python script. If you’ve just logged back in and are sitting at the home directory, you can start your script as a background process by typing the following command.

python3 air_quality_monitor/aqm.py &

If you run into any errors trying to start the script, make sure the Feed names match the names you chose in your script. Also, make sure the sensor is plugged into your Pi device.

8. Verify that your script is running with the following command.

ps -ef | grep aqm.py

9. With your script running you should see data starting to come in to your data block on your Adafruit dashboard. Again, if you’re not seeing any data you’ll want to make sure your feeds are set up correctly.

10. If you have everything working as intended, you can configure your script to run at boot by opening crontab

crontab -e

and adding the following line

@reboot sleep 60 && python3 /home/pi/air_quality_monitor/aqm.py &

You can exit here, if you chose nano as your editor, by pressing Ctrl + X, pressing Y, and pressing Enter.

Final Touches

Now that your Pi Zero has a script that will interface with the SDS011 sensor, the script runs on boot, and will constantly push the data from the sensor to your adafruit.io dashboard, you can place this device anywhere you need. For my final touches I have been looking into a case for the whole setup and a rechargeable battery but for now it’ll have to do with being plugged in. If you’ve made any improvements or just want to share your build feel free to contact me.

Raspberry Pi Air Quality Monitor Project #1

To complete this project you’ll need:

  • Raspberry Pi 3B+ or Raspberry Pi 4.
  • MicroSD Card 8GB or larger.
  • Raspberry Pi Imager software.
  • SDS011 sensor for measuring particulate matter.
  • PuTTY or an equivalent tool for SSH communication.
  • Ethernet or Wi-Fi available for the Raspberry Pi to connect with.

Configure Your Raspberry Pi

If you’re already familiar with Raspberry Pi’s you might already have it ready but if not you’ll need to open your Raspberry Pi kit and assemble the device per the included instructions. Once you have the device assembled and memory card available we can begin.

With the Raspberry Pi kit assembled, you’ll want to insert your MicroSD card into your computer so that we can put Raspberry Pi OS Lite on it. For this part we’ll be using the Raspberry Pi Imager software to make this process incredibly easy.

  1. Insert your MicroSD card into your PC’s MicroSD card slot or if you’re using an adapter, go ahead and plug it in and make sure your PC can see the storage device.
  2. Open the Raspberry Pi Imager software.
  3. Click on the “CHOOSE OS” button under “Operating System”.
  4. On the Operating System menu, click “Raspberry Pi OS (other)”.
  5. From the list of choices, click “Raspberry Pi OS Lite (32-bit)”.
  6. With the OS selected, click on the “CHOOSE STORAGE” button under “Storage”.
  7. On the Storage menu, be sure to select the correct storage device as the next still will erase all data on the MicroSD card.
  8. With the storage device selected, click the “WRITE” button after it becomes available.
  9. If you have selected the correct storage device, go ahead and click “YES” on the warning regarding erasing the storage device data.
  10. Once the Raspberry Pi Imager is complete you’ll get a notification saying it’s safe to disconnect the storage device, go ahead and remove the MicroSD card from the computer.
  11. Plug the MicroSD card back into your PC so that we can prepare the Raspberry Pi for remote accessibility. This will allow us to configure the device for remote access without us having to first plug it directly into a monitor, keyboard, and mouse for additional configuration.
  12. Once the MicroSD card has been picked up by your PC, go to the drive labeled “boot” and create a new text file. Then right click the new blank text file and rename it to “ssh”, completely removing the file extension. If you plan to connect your device to ethernet, we can go ahead and eject the MicroSD card and plug it into your Raspberry Pi.
  13. (OPTIONAL) If you plan to use WiFi instead of ethernet, we can create another text file and name it “wpa_supplicant.conf”. Once created, edit the file and copy code from the snippet below and customize it for your particular WiFi network, then save the file before ejecting the MicroSD card from your PC. Once ejected you can remove it from the PC and plug it into the Raspberry Pi.
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
	ssid="MyWiFiNetwork"
	psk="MyWiFiPassword"
	key_mgmt=WPA-PSK
}

Connect to your Raspberry Pi

With your Raspberry Pi device connected to the internet, you can log into your router and get the IP Address for the device connected with the hostname “raspberrypi”. With that IP address, you can open PuTTY and create a new session and enter the IP address and default credentials for the Raspberry Pi.

  1. Log into your router and find the device with the hostname “raspberrypi” and copy the IP address.
  2. Open PuTTY, type the IP Address into the “Host Name (or IP address)” field, and click “Open”.
  3. When connecting you’ll get a security alert window, click “Yes” to continue.
  4. Once connected, type “sudo raspi-config” and configure any system options. From here it’s easy to change your hostname, password, WiFi connectivity, time zone, and many other things.

sudo raspi-config

Install and Run the Air Quality Monitor

During this part of the process we’re going to be doing a lot of typing in the terminal. You can type these commands yourself or feel free to copy them into the terminal.

1. Update your device with the “sudo apt-get update” command. When updating you may receiving a notice at the end stating that a repository has changed from ‘stable’ to ‘oldstable’. This is just a notice and should not affect the project.

sudo apt-get update

2. Install “git” which will allow us to copy the code from GitHub.

sudo apt-get install -y git

3. With “git” installed, use the “git clone” command to clone the repository for the Air Quality Monitor.

git clone https://github.com/rydercalmdown/pi_air_quality_monitor.git

4. Navigate to the Air Quality Monitor directory.

cd pi_air_quality_monitor

5. Run the installation command. This will install any dependencies in order for this project to work.

make install

6. Run the build command. Depending on your Raspberry Pi, this part may take a second. With a 3B it only took a couple of minutes… to hit me with a big error.

make build
The error was very long but ended like this:

  File "/usr/local/lib/python3.8/tarfile.py", line 2294, in utime
    raise ExtractError("could not change modification time")
tarfile.ExtractError: could not change modification time
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 2
ERROR: Service 'web' failed to build : Build failed
make[1]: *** [Makefile:22: build] Error 1
make[1]: Leaving directory '/home/pi/pi_air_quality_monitor'

7. After encountering the above error, run the following commands to resolve the issue with libseccomp2, a Docker dependency.

wget http://ftp.us.debian.org/debian/pool/main/libs/libseccomp/libseccomp2_2.5.1-1_armhf.deb

sudo dpkg -i libseccomp2_2.5.1-1_armhf.deb

8. Run the “make build” command again.

9. Connect the SDS011 sensor to the USB adapter that was provided with the sensor.

10. Connect the SDS011 USB cable/adapter to the Raspberry Pi.

11. Run the following command to start the sensor and web server. This command may fail to run a few times before it succeeds so if you encounter an error during this part, try running the “make run” command a few times and it should eventually succeed. I had to try it three times and then it ran successfully without me changing anything.

make run

12. Use a web browser to navigate to the IP address of your Raspberry Pi on port 8000 to view the hosted website and collecting/collected data. If you run into the issue where your website is not showing a graph you can open the index.html file by typing the following command.

sudo nano src/templates/index.html

Find the line,

 $.getJSON('http://10.0.0.172:8000/api/'

Change it to,

$.getJSON(/api/,

To save and exit Nano, press Ctrl+X, then press Y, then press Enter.

13. With that saved, refresh your web page and you should now see the graph and data as pictured below.