Skip to content

owen-od/airPi_Project

Repository files navigation

AirPi Air Quality Project

This project is a Raspberry Pi air quality monitor (AirPi) built using a Raspberry Pi, Canakit breakout board and bme680 environmental sensor.

The AirPi device collects data on temperature, humidity, pressure, gas resistance and air quality (as a percentage).

This data is then published to the ThingSpeak platfrom using the MQQT protocol, allowing for the creation of graphs to map the data and reactions to occur (e.g. a tweet to be sent) when certain enviromental limits are passed (e.g the air quality degrades below a certain percentage).

The data is then accessed and displayed in a companian web application. A prototype of this companian web application is available on glitch here: https://airpi-web-app.glitch.me/ (Link to the code here).

AirPi Web Application

To explore the prototype of this web application, use the below login details:

email: homer@simpson.com
password: secret

While this is the main part of the project, in the bonus section below there is: (i) further instructions on how to connect a light to the AirPi that will turn on when the user is home and certain environmental limits are passed; (ii) an API that allows access on the local network to sensor data from the bme680 sensor, connected Netatmo air quality monitors and OpenWeatherMap API which may be used for further projects; (iii) a simple bash script that uses the API to check the CO2 level every 30 minutes and emails the user a warning if it climbs above 1000ppm.

Installation

At command line, you will need to install the following:

sudo apt get install y python smbus
sudo apt get install y i2c tools
sudo apt-get install mosquitto
sudo pip3 install paho-mqtt
sudo pip3 install python-dotenv
sudo pip3 install bme680

Connect the bme680 sensor to the Raspberry Pi as in the below image:

Sensor connected to Pi using breadboard

Now we will enable I2C:

Enter sudo raspi-config
Select 3, interface options
Select p5, 12C
Enable

Now reboot your Raspberry Pi: Sudo reboot

Check if the sensor has been detected by running at the command line: sudo i2cdetect y 1

you should see the below output:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- 77

This indicates that there is a device at address 0x77.

Now clone this repo into the relevant directory: git clone https://github.com/owen-od/airPi_Project

Configuration

The following assumes that you have created a ThingSpeak account and channel and added a new device. To proceed you will need a list of credentials for your device that includes Client ID, username and password. This can be downloaded from ThingSpeak as a plain text file.

Create a file called .env in your directory. Copy your credentials from the plain text file into this directory.

Then add the ThingSpeak channel id for your device. Finally, add a transmission interval to the file. This will determine how frequently sensor data is published to ThingSpeak.

Your .env file should now look like the below

username = LbbgBrFyDd4lKhEkORo8AAA
clientId = LbbgBrFyDd4lKhEkORo8AAA
password = 6c+vxaw3CVLredzEsheaHy7B
channelId = 1319333
transmissionInterval = 60

How to use this

In order to obtain air quality readings from the local environment, the indoor-air-quality.py script should first be run for 5 minutes. This allows the sensor to run for a burn in periord. After 5 minutes have elapsed, it will then use a combination of relative humidity and gas resistance to estimate indoor air quality as a percentage. The latest air quality reading will continuously be updated in a text file, allowing the data to be accessed in other scripts.

The thingspeak.py script may then be run. This will get readings for temperature, humidity, pressure, gas resistance and air quality and publish them to ThingSpeak (every 60 seconds by default).

The data, and graphs of the data, can then be accessed from ThingSpeak in the web application.

Note that the above scripts may also be set as a cron job to allow the scripts to run at boot. To do so at the command line run sudo crontab -e. Then, adjusting the paths to match your directories, enter the below:

@reboot python3 /home/pi/airPi/indoor-air-quality.py
@reboot sleep 300 && python3 /home/pi/airPi/thingspeak.py mqtt://mqtt3.thingspeak.com:8883

Note that if a light has also been connected to your AirPi (see bonus section below), then the lights.py script can also be added to run at boot:

@reboot python3 /home/pi/airPi/lights.py

Bonus

(i) This section describes how to connect an LED to the Raspberry Pi, so that the LED light alerts the user when certain devices are detected on the network (indicating that the user is home) and certain environmental limits are passed.

This example uses a laptop and a phone to detect the presence of the user at home (the presence of either will mean that the script can run). If the user is home, the LED light will turn on when the temperature drops below a certain limit, notifying the user to take action (e.g. to turn the heating on when the temperature is too low!). When the devices are no longer on the network, the light will temporarily turn off. The light will also turn off when the conditions improve.

Connect an LED to the AirPi as in the below image:

LED connected to Raspberry Pi

Then, you will need to know the MAC addresses of the devices that you wish to use to indicate the presence of the user at home. If you do not know these, run the following command to detect them (replacing the IP address with that of your network): sudo nmap 192.168.0.0/24 | grep MAC

You must then enter the names of the devices and their MAC addresses in the presence_detector.py script, at line 10. You should also replace the network address at line 16 with your network address.

Install the GPIOzero library as follows:

sudo apt install python3-gpiozero

You can now run the script with the following command:

python3 lights.py

Note that this will only run when the devices entered in the presence_detector.py script are detected on the network.

(ii) This section describes how to create a simple web API with Flask that may be used to access data from the AirPi and connected Netatmo air quality monitors on the local network

This example uses a Netatmo smart indoor air quality monitor placed in the bedroom, with an additional module located outside on the balcony. The AirPi is located in the home office.

Netatmo air quality monitors

First, in order to use the Netatmo devices, a valid Netatmo account is required. From this account, the following details can be obtained: client ID, client secret, username, password, device MAC address (note that to obtain the client secret and client Id, you must first create an app in your account - see create your app). Your details can then be added to the .env file created above. This should appear as below in the .env file:

net_client_id = '1234567890abcdef12345678',
net_client_secret = 'ABCdefg123456hijklmn7890pqrs',
net_username = 'user@mail',
net_password = 'password',
net_device = '70:ee:50:XX:XX:XX'

If you wish to use the http://YOUR_PI_IP:5000/home/city route to obtain local weather data, you will first need to obtain an API key from openweathermap (and change the latitude and longitude in the route to your local area). Add the api key to your .env file as below:

ow_api_key = 1111bb4340089aaa393990f4fe742222

To use the API then you will need to additionally install the following:

pip3 install netatmo
pip3 install flask
pip3 install -U flask-cors

Then run the script in the terminal: python3 api.py

You may now make calls to the API using the specified routes. For example: http://YOUR_PI_IP:5000/home/office

A page containing an overview of home environmental data can be accesed with the following route: http://YOUR_PI_IP:5000/

If you do not know the IP address of your Raspberry Pi for the above, run the following command in a terminal on the Raspberry Pi to find it: ip a

(iii) This section describes how to create a simple bash script that will use the API to check the CO2 level every 30 minutes and email a warning to the user if it climbs above 1000ppm.

Install the following at the command line:

sudo apt-get install jq
sudo apt-get install -y swaks
sudo apt-get -y install perl

The example script uses mailgun to send an email via SMTP. A mailgun account will be required for this. If you do not have one, proceed as below:

  • Go to https://www.mailgun.com/ and click "Start Sending"
  • Follow the instructions for setting up an account and verify your account.
  • Go to the Sandbox Domain in the Sending Domains section on the Dashboard.
  • Select the Sandbox and select the SMTP option. You should then see your SMTP settings. You will now use these in the script

Once you have your mailgun credentials, enter your username and password at lines 13 and 14 of the script. Then enter the email that you wish the alerts to be sent to at line 15.

The script may then be set as a cronjob to run at regular intervals. For the script to run every 30 minutes type sudo crontab -e and, adjusting the path to match your directories, add the below to it:

*/30 * * * * /home/pi/airPi/co2_alert.sh

The script will now run every 30 minutes and make a HTTP request to the API to check the CO2 level. If the CO2 level is above 1000ppm an email will be sent to alert the user so they can open the windows.

Note that this script requires the API to be running, so you my also wish to set the API to run at boot.

Project Graphics

Project Graphics

Project Video

A video walk-through of the project is available on YouTube at the following URL: https://youtu.be/1uGTmVYq9_U

Contact

Contact me at 20095405@mail.wit.ie

About

AirPi Air Quality Project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published