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…


This content originally appeared on DEV Community and was authored by Takuya Matsuyama

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>"
}

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


ssh pi@raspberrypi.local
  • 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

Put the Raspberry Pi in a cable box

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

Follow me online


This content originally appeared on DEV Community and was authored by Takuya Matsuyama


Print Share Comment Cite Upload Translate Updates
APA

Takuya Matsuyama | Sciencx (2021-05-13T02:32:15+00:00) How to build an air quality monitor using Raspberry Pi Zero W. Retrieved from https://www.scien.cx/2021/05/13/how-to-build-an-air-quality-monitor-using-raspberry-pi-zero-w/

MLA
" » How to build an air quality monitor using Raspberry Pi Zero W." Takuya Matsuyama | Sciencx - Thursday May 13, 2021, https://www.scien.cx/2021/05/13/how-to-build-an-air-quality-monitor-using-raspberry-pi-zero-w/
HARVARD
Takuya Matsuyama | Sciencx Thursday May 13, 2021 » How to build an air quality monitor using Raspberry Pi Zero W., viewed ,<https://www.scien.cx/2021/05/13/how-to-build-an-air-quality-monitor-using-raspberry-pi-zero-w/>
VANCOUVER
Takuya Matsuyama | Sciencx - » How to build an air quality monitor using Raspberry Pi Zero W. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/13/how-to-build-an-air-quality-monitor-using-raspberry-pi-zero-w/
CHICAGO
" » How to build an air quality monitor using Raspberry Pi Zero W." Takuya Matsuyama | Sciencx - Accessed . https://www.scien.cx/2021/05/13/how-to-build-an-air-quality-monitor-using-raspberry-pi-zero-w/
IEEE
" » How to build an air quality monitor using Raspberry Pi Zero W." Takuya Matsuyama | Sciencx [Online]. Available: https://www.scien.cx/2021/05/13/how-to-build-an-air-quality-monitor-using-raspberry-pi-zero-w/. [Accessed: ]
rf:citation
» How to build an air quality monitor using Raspberry Pi Zero W | Takuya Matsuyama | Sciencx | https://www.scien.cx/2021/05/13/how-to-build-an-air-quality-monitor-using-raspberry-pi-zero-w/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.