How to build an air quality monitor using Raspberry Pi Zero W
Mục lục
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.
Original video
I put plenty of images so you can enjoy it as if you read a magazine 🙂 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>"
}
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
Log in to the Raspberry Pi via SSH
- 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
Install git & build tools
sudo apt install git build-essential
Install python-smbus and i2c-tools
sudo apt install python-smbus i2c-tools
Enable I2C interface
sudo raspi-config
Choose ‘Interface Options’ -> ‘I2C’ -> ‘Yes’.
Then, reboot.
Check the I2C interface working
sudo i2cdetect -y 1
Get the example code for testing the sensors
git clone https://github.com/AnaviTechnology/anavi-examples.git
cd anavi-examples
Install wiringpi
sudo apt install wiringpi
Build the example code
cd ./sensors/HTU21D/c
make
./HTU21D
Enable Serial interface
sudo raspi-config
Choose ‘Interface Options’ -> ‘Serial Port’ -> ‘No’ -> ‘Yes’.
Install python-pip
sudo apt install python-pip
Install a Python module for mh-z19
sudo pip install mh-z19
Test it:
sudo python -m mh_z19
# -> {"co2": 5000}
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
After a while:
sudo python -m mh_z19
# -> {"co2": 400}
Install nginx web server
sudo apt install nginx
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
Build C programs for the sensors
cd sensors/BH1750
make
cd ../HTU21D
make
cd ../BMP180
make
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
Configure nginx
sudo vi /etc/nginx/sites-available/default
Replace the line:
root /var/www/html;
with:
root /home/pi/anavi-phat-sensors-ui;
Then, restart nginx.
sudo /etc/init.d/nginx restart
Open the web interface from browser
Boom!
Configure root’s crontab to update data every 5 minutes
sudo crontab -e
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