Introduction: AirPi - Air Quality Sensor

Have you ever wondered why you're getting a headache? And if this is due to a bad air quality? With this device you're able to check if this is the case. This device measures the CO2-value, TVOC-value, temperature and humidity. You can see the air quality live on the LCD-display and see a clear indication if it's getting dangerous. This way you can open your windows in time.

If you enter the IP-adres - shown when you start up the device in your browser - the website will open up. You can see a lot of information about the indoor environment, along with graphs of the past minutes / hours. There's also a live indication and some information and tips on the dashboard.

This project was made by a student at Howest Kortrijk, NMCT (New Media and Communication Technology).

Step 1: Materials

This is everything I bought to create this project. This is a relatively cheap project, depending on the cost of the 3D-print. If you're able to print it at school this could be very cheap. Otherwise it depends on where you print it and the material you print it in. You'll notice I bought alot in bulk, simply because it's hard to find individual resistors or leds and this makes it even cheaper. If you have time, you can order most items on aliexpress.com, the delivery could take a while, but this way you can limit your expenses.

Without the print, the money I spent on this project is €81,80.

These are the materials you need:

Circuit:

  • Raspberry Pi 3
  • SD card 8GB (minimum)
  • CCS811 air quality sensor
  • DHT22 temperature and humidity sensor
  • Potentiometer (contrast LCD)
  • LCD 16x2
  • Female to female jumper wires
  • Green and red LED
  • Resistors (2x470ohm and 1 4700ohm)

Case:

  • 3D-print
  • Screws
  • 2 component glue (or other hot glue)
  • thread-cutting tool

Only of you're using a PCB:

  • Soldering iron
  • Flux (makes it easier)
  • Tin
  • Experiment pcb 2x4cm

Step 2: Connections

Connect the wires as above. You can see an electrical circuit in the fritzing file. It's not a very complicated circuit, but if you want to make it as small as my possible, you definitely want to get an experiment PCB Board. The wiring would be the same, except the GND and Vin will be connected to the PCB board. The sensors will be connected by female jumper wires or males with soldering. Don't forget to solder the resistor on the DHT22 sensor.

I also recommend to use short cables, 10cm should do. Otherwise the box would be filled with cable even more. You don't need very long ones since the size of the print is made as small as possible.

Step 3: 3D-print

The first tought that popped into my head when i was thinking about a case was a 3D-print. Since my dad had printed out serveral other items and he designed them himself. Together we created this design, and tought about every aspect. It should be well enough cooled, everything can be screwed up into place and if not, it can be shoved into place.

We even drew every component to check if everything fits in. The file is available for everyone and we would love to hear some feedback. We were very pleased with the result.

Step 4: Code

The code for this project can be found on Github. If you've used other pins (for example another GPIO-pin for the LED's, you will have to adjust these variables. There's gonna be two python scripts runnning, web.py for the website and sensor.py to read the sensors and update the database. We will be importing the class LCD from lcd.py.

With a configured raspberry pi you can get started. First of all you'll need to update and upgrade all packages:

sudo apt-get update && sudo apt-get upgrade

After that you'll need to install the following packages:

sudo apt install -y python3-venv python3-pip python3-mysqldb mariadb-server uwsgi nginx uwsgi-plugin-python3 

Now create a virtual environment:

me@my-rpi:~ $ python3 -m pip install --upgrade pip setuptools wheel virtualenv
me@my-rpi:~ $ mkdir project1 && cd project1
me@my-rpi:~/project1 $ python3 -m venv --system-site-packages env
me@my-rpi:~/project1 $ source env/bin/activate
(env)me@my-rpi:~/project1 $ python -m pip install mysql-connector-python argon2-cffi Flask Flask-HTTPAuth Flask-MySQL mysql-connector-python passlib

Since this is done, you can clone the code from my GitHub into your virtual environment. This can be done in multiple ways.

In the directory conf you can find four files you'll have to adjust if necessary. You'll definitely have to change the user and homedirectory in every file. The uWSGI ini should be fine as long as you didn't change my code, make sure to change the user and virtualenv if necessary .

Since the CCS811 sensor was intentionally used for the arduino, this can't communicate over the i2c bus at the speed of the raspberry pi. You'll have to reduce the speed to a baudrate of 10000 (I used 9600) in the config file.

You'll also have to get the library of the adafruit sensor. I could explain this here but there's a perfect adafruit guide that explains all of this very well.

Because we want the python scripts to run automatically when the raspberry is connected, you'll have to use the services. They should be fine if you kept my code. All you need to do for them to run is enable them. Before you do that there's one last thing.

Since we're using an nginx webserver, we'll have to deactivate the default and replace it by our own config. To do this these steps should be followed:

  • copy conf/nginx to *sites-available*
  • Remove the link to the default-config
  • Add a link to the new config
  • Restart nginx to save the changes
me@my-rpi:~/project1 $ sudo cp conf/project1-*.service /etc/systemd/system/
me@my-rpi:~/project1 $ sudo systemctl daemon-reload
me@my-rpi:~/project1 $ sudo systemctl start project1-*
me@my-rpi:~/project1 $ sudo systemctl status project1-*

Nginx and mysql should be running at all times. They start up along with the raspberry pi. The web script and sensor script not just yet.

To do this you still need to enable these two services with these commands:

sudo systemctl enable project1-flask.service
sudo systemctl enable project1-sensor.service

Step 5: Database

My database consists of three tables. User has no relation with other tables. This is only used to log in and grant access to the website. When the device is turned on, the CO2-value and TVOC-value will be written to the database every 50 seconds. The temperature and humidity every 5 minutes. This way we get a clear overview of the past.

The SQL-file can be found in here, but to get the database on the raspberry pi, you should follow these steps:

After the installation of the packages in the previous step, mariadb/mysql should be running immediately. You can check this with this line:

me@my-rpi:~ $ sudo systemctl status mysql

To create the database and users you can simply run the sql-scripts in the code from GitHub. If you did this correctly you should see your tables using this command:

me@my-rpi:~ $ echo 'show tables;' | mysql project1 -t -u project1-admin -p

Now we're all set, you can test this without the case to make sure everything works. Unless you're connected to wifi, you'll need to connect it with an ethernet cable and run it manually.

Step 6: Connect to Wi-Fi

Open the wpa-supplicant configuration file in nano (doesn't really matter, just make sure you can work with the text editor).

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Go to the bottom of the file and add the following (replace the ssid-name and password-name by yours):

network={
    ssid="ssid-name"
    psk="password-name"
}

To create an encrypted password you can use wpa_passphrase and simply copy it into the wpa_supplicant.conf psk to make things more secure.

wpa_passphrase "ssid-name" "password-name"

If you want it to connect to this Wi-Fi network automatically, and there are others in the config file, make sure you change the priority to a higher level by adding this line to the network in the config file:

priority=2

Don't forget to reconfigure the interface with:

wpa_cli -i wlan0 reconfigure

Now you're all set and connected to a wifi network.

Step 7: Putting Everything Together

Since everything is wired up and soldered, we can get to the case. This was designed, so that you could open up the case without any loose wires. This means everything is attached to the bottom part. The first thing to do is a little adjustment to the raspberry. It has holes in it on every corner, but these aren't as big as they should be. The diameter should be enough to fit a 3mm screw inside.We had to polish the holes so that they got a little wider.

The second thing to do is cutting a screw thread into each hole. This may sound hard but can be easily done with the right tools. I'd recommend to do this at a local hardware store, just ask for a thread-cutting tool. Since my dad is a goldsmith he had the tools to do this at work. I might upload a new stl-file so that this is printed in later on, but this would need a very accurate printer.

The third step is to screw the pi onto the bottom part. You'll need a 4 7mm long screws with a diameter of 3 mm. After this you can shove the PCD board into the provided place at the top of the bottom part. The CCS811 sensor can be shoved in to the provided place at the left side and the DHT11 can be attached to the right plate. Both are isolated and ventilated sufficiently, but afterwards we noticed it was still getting hot inside. More about that later on.

Then you need to attach the LED's to their tube. We did this with 2 component glue but you can do it however you like. Make sure that they stick on there.

Now you can attach the LCD-display, you'll need screws with the same diameter as the previous ones, but a little longer. Mine were 1cm. If the four screws are screwed in, there's only one thing left to do. Attach the top part. All you need is four screws, with the same diameter and these are 2cm. Now everything should be in place and you can start it up.

Step 8: Start It Up

The start-up process of this project is very easy:

  1. Attach the power cable into the left side of the case. It's not very visible but you can see through the vents. If you got it once this won't be a problem again.
  2. Give it some time to start up.
  3. The IP-address will show up on the display for ten seconds. The only thing you need to do is make sure you're connected to the same network and enter the IP-address into the address bar of your browser.
  4. You're now on the website. You don't have an account yet so create an account.
  5. If you are registered, log in.
  6. Done! You can see all data on the webpage and the LCD-display is showing the current air quality.

Since warmth rises, we have located the sensors at the bottom of the case. This way the temperature wouldn't have a big impact on the registered values. So for optimal measurements, make the device stand up or you can simply hang it onto the wall.