DEV Community

Cover image for How to build an air quality monitor using Raspberry Pi Zero W
Takuya Matsuyama
Takuya Matsuyama

Posted on • Originally published at community.inkdrop.app

How to build an air quality monitor using Raspberry Pi Zero W

Hi, it's Takuya.

Knowing the air quality is useful to keep yourself productive because the bad air quality would affect your brain performance more than you'd think.
So, I built a room air quality monitor that displays the temperature, humidity, CO2 density, and barometric pressure of my home office.
It notifies with sound when the CO2 level gets more than 1,000 ppm - So, I can know when to refresh the air.
I'm gonna show you a walkthrough of how to build it.

Let's get started.

Architecture

This is the architecture overview.
It is made with a Raspberry Pi Zero W, which runs Nginx and has an ANAVI Infrared pHAT with some sensors attached.
And you can view it from a browser on any device.

Unbox a Raspberry Pi Zero W

SanDisk 32GB MicroSD Card

Anker 2-in-1 USB C Memory Card Reader

Download Raspberry Pi Imager

Write a Raspberry Pi OS to the SD Card

Configure your Wi-Fi network

To get your Rasberry Pi to connect your Wi-Fi network, create wpa_supplicant.conf file in the root directory of the SD card memory.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<Insert 2 letter ISO 3166-1 country code here>

network={
 ssid="<Name of your wireless LAN>"
 psk="<Password for your wireless LAN>"
}
Enter fullscreen mode Exit fullscreen mode

Enable SSH server

You have to enable SSH server to connect through the network.
Create an empty file named "ssh" in the root directory by touch command.

Boot the Raspberry Pi

Check if connected to the Wi-Fi network


ping raspberrypi.local
Enter fullscreen mode Exit fullscreen mode

Log in to the Raspberry Pi via SSH


ssh pi@raspberrypi.local
Enter fullscreen mode Exit fullscreen mode
  • user: pi
  • pass: raspberry

Unbox an ANAVI Infrared pHAT

ANAVI Infrared pHAT is an add-on for the Raspberry Pi family, which adds an ability to control your old consumer electronic devices like air conditioning with infrared signals.
On top of that, it supports sensor modules.
With the advanced kit, you can get these sensor modules for the air quality:

  • HTU21D - temperature & humidity
  • BMP180 - temperature & barometric
  • BH1750 - light

Install the ANAVI Infrared pHAT to the Raspberry Pi Zero

Connect the sensors to the I2C slots

MH-Z19 - CO2 density sensor

MH-Z19 is a low-cost CO2 density sensor.

Connect MH-Z19 to the UART slot

Check if MH-Z19 is working

Update Raspberry Pi packages


sudo apt update
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Install git & build tools

sudo apt install git build-essential
Enter fullscreen mode Exit fullscreen mode

Install python-smbus and i2c-tools

sudo apt install python-smbus i2c-tools
Enter fullscreen mode Exit fullscreen mode

Enable I2C interface


sudo raspi-config
Enter fullscreen mode Exit fullscreen mode

Choose 'Interface Options' -> 'I2C' -> 'Yes'.
Then, reboot.

Check the I2C interface working


sudo i2cdetect -y 1
Enter fullscreen mode Exit fullscreen mode

Get the example code for testing the sensors

git clone https://github.com/AnaviTechnology/anavi-examples.git
cd anavi-examples
Enter fullscreen mode Exit fullscreen mode

Install wiringpi

sudo apt install wiringpi
Enter fullscreen mode Exit fullscreen mode

Build the example code


cd ./sensors/HTU21D/c
make
./HTU21D
Enter fullscreen mode Exit fullscreen mode

Enable Serial interface


sudo raspi-config
Enter fullscreen mode Exit fullscreen mode

Choose 'Interface Options' -> 'Serial Port' -> 'No' -> 'Yes'.

Install python-pip

sudo apt install python-pip
Enter fullscreen mode Exit fullscreen mode

Install a Python module for mh-z19

sudo pip install mh-z19
Enter fullscreen mode Exit fullscreen mode

Test it:

sudo python -m mh_z19
# -> {"co2": 5000}
Enter fullscreen mode Exit fullscreen mode

Put MH-Z19 outside for a while for calibration

Run ZERO point calibration

While the device is put outside, run ZERO point calibration to calibrate the CO2 sensor:

sudo python -m mh_z19 --zero_point_calibration
Enter fullscreen mode Exit fullscreen mode

After a while:

sudo python -m mh_z19
# -> {"co2": 400}
Enter fullscreen mode Exit fullscreen mode

Install nginx web server


sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

Clone a web UI

I published my web UI for displaying the sensor data on GitHub here:

You can reuse it by cloning the repository.

cd ~
git clone https://github.com/craftzdog/anavi-phat-sensors-ui
cd anavi-phat-sensors-ui
Enter fullscreen mode Exit fullscreen mode

Build C programs for the sensors


cd sensors/BH1750
make
cd ../HTU21D
make
cd ../BMP180
make
Enter fullscreen mode Exit fullscreen mode

Prepare test data

cd ~/anavi-phat-sensors-ui
mkdir data
./sensors/HTU21D/HTU21D > ./data/HTU21D.json
./sensors/BMP180/BMP180 > ./data/BMP180.json
./sensors/BH1750/BH1750 > ./data/BH1750.json
sudo python -m mh_z19 > ./data/MH_Z19.json
Enter fullscreen mode Exit fullscreen mode

Configure nginx


sudo vi /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Replace the line:

root /var/www/html;
Enter fullscreen mode Exit fullscreen mode

with:

root /home/pi/anavi-phat-sensors-ui;
Enter fullscreen mode Exit fullscreen mode

Then, restart nginx.

sudo /etc/init.d/nginx restart
Enter fullscreen mode Exit fullscreen mode

Open the web interface from browser

Boom!

Configure root's crontab to update data every 5 minutes

sudo crontab -e
Enter fullscreen mode Exit fullscreen mode

Edit it like so:

*/5 * * * * /usr/bin/python -m mh_z19                > /home/pi/anavi-phat-sensors-ui/data/MH_Z19.json
*/5 * * * * /home/pi/anavi-phat-sensors-ui/sensors/HTU21D/HTU21D > /home/pi/anavi-phat-sensors-ui/data/HTU21D.json
*/5 * * * * /home/pi/anavi-phat-sensors-ui/sensors/BMP180/BMP180 > /home/pi/anavi-phat-sensors-ui/data/BMP180.json
*/5 * * * * /home/pi/anavi-phat-sensors-ui/sensors/BH1750/BH1750 > /home/pi/anavi-phat-sensors-ui/data/BH1750.json
Enter fullscreen mode Exit fullscreen mode

Put the Raspberry Pi in a cable box


That's it.
I hope you found it helpful.
Enjoy hacking.

Follow me online

Top comments (1)

Collapse
 
lafaee profile image
Oblivious

Nice post