Air quality monitoring in times of Covid19
Tim Herden
Empowering Digital Transformation Through Low-Code | Director Solution Architecture at Mendix | Helping Enterprises Accelerate Innovation | DACH, Nordics, Iberia, Italy & France
Eight months ago a friend showed me his smart home setup. It was impressive, he could irrigate his garden, move the window blinds, switch the light and monitor temperature in- and outdoors. Sure useful but I was shocked to hear that he had spent well over 1000 € ?? on equipment and is paying a monthly fee for the central data hub. I felt that more people should have access to such technology. So, it has to cost way less. I didn't have a clue how much exactly but I took on the challenge to realise a smart home for as little as possible.
Fast forward 8 months and the world is in a crisis due the new Corona virus ??. People in most countries are required to stay at home ??. Now, this is a severe situation and I urge everyone to keep your distance to others to kill the virus. It also means that you and your families or partners should feel comfortable in your house or apartment. So monitoring the quality of the air ?? and temperature ?? at home becomes more important and that is way I decided to share the technology and code I used for free for everyone to implement under GPL-3.0. (Also, this might be a good countermeasure against potential boredom after 14 days of quarantine ?? and a little puzzle for your kids if you have any.)
Note: I know that room monitoring is not the same as a fully automated smart home since it lacks the actuators; but it is a necessary prerequisite and the part that I can share here ??.
The overall system architecture will look like this:
What you need (bill of material):
- 1x ESP8266 microcontroller (6,50 € // https://amzn.to/39ap9Qe )
- 1x BME680 sensor breakout (20,75 € // https://amzn.to/3923hGE )
- 1x Micro USB-Cable (5,00 € // https://amzn.to/2QAIPGk )
- 1x RaspberryPi 3 incl. SD card (72,50 € // https://amzn.to/33LZbS9 )
- 5x Jumper wires (female-to-female) (5,79 € // https://amzn.to/3al76rG)
- You also need a micro USB charger to power the Raspberry but most people have one at home, e.g. from your smartphone.
Total costs: approx. 110 € (compare this to > 1000 € !!)
Note: We will only use free software and libraries, so do not expect any additional costs beyond the money spent above for the hardware. ????
Let the coding begin ??
I. The server / data hub
First start up your Raspberry Pi and boot into Raspbian. (If you need help installing Raspbian or in general with regards to your Raspberry, please check their documentation section on raspberrypi.org) We will use the Raspberry as a server, therefore it is not required to start into the GUI (Graphical User Interface). The CLI (Command Line Interface) is sufficient.
Activate SSH: In order to manage your raspberry from remote, we need to be able to connect to it via SSH (Secure Shell). SSH is installed in the default version of Raspbian but we need to activate it. We can do this from the Raspberry Pi Configuration Tool and we get there by executing the following command:
pi@raspberry:~ $ sudo raspi-config
Then, select Interfacing Options, navigate to and select SSH.
Save and close rasps-config. Now execute
pi@raspberry:~ $ ifconfig
to retrieve the IP address of your Raspberry. (Note it down - we will need it soon!) It should look something like this: 192.168.2.123
From now onwards, we can configure everything from the comfort of our laptop by using your preferred SSH-client and connecting to your Raspberry
Where pi is the username and the password is the one that you set during the Raspbian setup (default password is raspberry but I hope you had changed that ?? ).
Now, we are going to do the following steps (an overview):
- Install and run Mosquitto (an open source message broker that implements the MQTT protocol)
- Install and run InfluxDB and Telegraf (a time series database and a data connector)
- Install and run Grafana (dashboarding)
To install Mosquitto enter these commands:
pi@raspberry:~ $ sudo apt update pi@raspberry:~ $ sudo apt install -y mosquitto mosquitto-clients
To make Mosquitto auto start on boot up enter:
pi@raspberry:~ $ sudo systemctl enable mosquitto.service
To test the successful installation enter:
pi@raspberry:~ $ mosquitto -v
The result should like this:
To install InfluxDB and Telegraf, we first need to add the application repository to the packet manager of Raspbian by entering the following commands:
pi@raspberry:~ $ curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add - pi@raspberry:~ $ echo "deb https://repos.influxdata.com/debian stretch stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
Now we load the new repository into the packet manager's database:
pi@raspberry:~ $ sudo apt update
... and then install the two applications:
pi@raspberry:~ $ sudo apt install influxdb telegraf
Now we need to start InfluxDB:
pi@raspberry:~ $ sudo systemctl enable influxdb pi@raspberry:~ $ sudo systemctl start influxdb pi@raspberry:~ $ influx
... and create the admin user (with username admin and password password - I suggest to use different credentials and note them down of course) and initial database named smarthome :
CREATE USER admin WITH PASSWORD 'password' WITH ALL PRIVILEGES CREATE DATABASE smarthome
Now check and edit the connection setting for InfluxDB in the settings file influxdb.conf:
pi@raspberry:~ $ sudo nano /etc/influxdb/influxdb.conf
In the config file in section [http] you must check the following lines and uncomment them by removing the hash '#':
enabled = true bind-address = ?:8086“ auth-enabled = true
Note: 8086 specifies the port, we will use to connect to the database.
Since we changed the configuration, we need to restart the service:
pi@raspberry:~ $ sudo systemctl restart influxdb
Now we need to edit the configuration file telegraf.conf of Telegraf by entering:
pi@raspberry:~ $ sudo nano /etc/telegraf/telegraf.conf
In this file we basically configure the data inputs (in our case we get the data from MQTT) and the data outputs (in our case we store the data into InfluxDB):
######################################################################### # OUTPUT PLUGINS # ######################################################################### [[outputs.influxdb]] urls = ["https://localhost:8086"] skip_database_creation = true username = "admin" password = "password"
For inputs:
######################################################################### # INPUT PLUGINS # ######################################################################### [[inputs.mqtt_consumer]] servers = ["tcp://localhost:1883"] topics = ["esp8266"] data_format = "value" data_type = "float"
Save and exit the nano editor and (re-)start the Telegraf service:
pi@raspberry:~ $ sudo systemctl enable telegraf pi@raspberry:~ $ sudo systemctl restart telegraf
Going on to install the final server application: Grafana.
sudo apt-get install -y adduser libfontconfig1 wget https://dl.grafana.com/oss/release/grafana_6.7.1_armhf.deb sudo dpkg -i grafana_6.7.1_armhf.deb
To check for yourself which version is the most recent and stable release, see here: https://grafana.com/grafana/download
Now we also need to start Grafana as a service:
sudo systemctl enable grafana-server sudo systemctl start grafana-server
When you now start your favourite web browser (I use Safari) and enter the URL:
https://192.168.2.123:3000
You should see the login page of Grafana (running on your RaspberryPi):
Note: at first logon, you can assign a new password.
In order to be able to see data from the InfluxDB database, we need to configure it as a data source. Use the data as shown in the image:
Note: User and Password are the credentials that you chose when configuring the InfluxDB.
If everything works correctly, you should see this notification:
Now that we have the data source connected, we need to take care of the sensor since our database is still empty.
II. The sensor
We start off with the decentral equipment, i.e. the ESP micro controller and the sensor.
Connect the BME680 to your ESP8266 following this diagram:
Now download and install the Arduino IDE from https://www.arduino.cc/en/Main/Software. Choose the latest stable version (i.e. not beta).
Once the Arduino IDE is installed, start it. We must now install the board manager for ESP8266 by selecting Settings and adding the following URL to the board management configuration:
https://arduino.esp8266.com/stable/package_esp8266com_index.json
Now you should see the ESP boards under Tools:
Also adjust the board's configuration to match the image above (do not forget to choose the correct port where your ESP is connected to under Port).
We now need to import the required libraries. That will allow us to make use of MQTT and the BME680 sensor. For this, we open the Library Manager (Tools -> Manage Libraries...) and search as well as install the following libraries:
- Adafruit Unified Sensor (required for all Adafruit Unified Sensor based libraries)
- Adafruit BME680 Library (require to use the BME680 sensor)
- PubSubClient (a lightweight client library for MQTT messaging)
Now we can finally write the code that controls the micro controller. You will have the chance to copy & paste the entire sketch for the ESP8266 from my Github repository (click here to follow link) but if you want to understand what the program is doing, I suggest that you familiarise yourself with the basic of Arduino / ESP programming but checking out the tutorial section over at Arduino.cc (click here to get there directly).
In an overview, our program will do the following:
1.Connect the ESP8266 to your local wifi network.
void setup_wifi() { delay(10); // Connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); // Printing connection parameters to serial monitor Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); }
2.Connect to your MQTT server.
void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect if (client.connect(clientId.c_str())) { Serial.println("connected"); // Once connected, publish an announcement... client.publish("esp8266/status", "Hello from ESP8266"); // ... and resubscribe // client.subscribe("inTopic"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } }
3.Initialise the BME680 sensor to work with the ESP8266.
void setup_bme680() { Serial.begin(115200); while (!Serial); Serial.println(F("BME680 test")); // change to !bme.begin() for Adafruit breakout since they use 0x70 as default address if (!bme.begin(0x76)) { Serial.println("Could not find a valid BME680 sensor, check wiring!"); while (1); } // Set up oversampling and filter initialization bme.setTemperatureOversampling(BME680_OS_8X); bme.setHumidityOversampling(BME680_OS_2X); bme.setPressureOversampling(BME680_OS_4X); bme.setIIRFilterSize(BME680_FILTER_SIZE_3); bme.setGasHeater(320, 150); // 320*C for 150 ms }
4.Loop through reading temperature, pressure, humidity and gas resistance and send the measurements to the MQTT server.
if (! bme.performReading()) { Serial.println("Failed to perform reading :("); return; } client.publish("esp8266/bme680/temperature_c", String(bme.temperature).c_str(), true); client.publish("esp8266/bme680/pressure_hpa", String(bme.pressure / 100.0).c_str(), true); client.publish("esp8266/bme680/humidity_p", String(bme.humidity).c_str(), true);
III. The dashboard
Now that the data from the BME680 is configured to send data every 10s into our database, we can visualise it in a chart. Log back on to your Grafana frontend with your web browser. Click on Create Dashboard and configure a new query like this:
Note: In the topic column, you will find the MQTT-topics that we used in our Arduino sketch:
Once we have configured the panels also for humidity and gas resistance, the final dashboard could look like this:
Phew - we made it! (I never said it was easy I just said it is low-cost ?? )
If you like this article, please consider giving a ????. If you have comments or questions, please leave a comment. I try to answer each of them.
Thank you and stay safe during the Covid19 crisis! ???♂?
Some links that you might find interesting as well:
- My personal homepage: https://www.timherden.com
- My GitHub repository: https://github.com/timherden/room-monitoring
- My first LinkedIn post on the smarthome topic: https://www.dhirubhai.net/posts/timherden_smarthome-nodemcu-esp32-activity-6554460296113602560-RaKJ
- My second post: https://www.dhirubhai.net/posts/timherden_smarthome-nodemcu-esp8266-activity-6555756059733053440-6oQC
- My third post: https://www.dhirubhai.net/posts/timherden_esp8266-esp8266-mqtt-activity-6558241051386105856-wAHb
- My last post: https://www.dhirubhai.net/posts/timherden_lowcost-homeautomation-nodemcu-activity-6639628569368641536-fGSg
- Thorough explanation about SSH on a RaspberryPi: https://www.raspberrypi.org/documentation/remote-access/ssh/
Keywords: #smarthome #lowcost #iot #influxdb #grafana #esp8266 #nodemcu #raspberrypi #raspbian #telegraf #mosquitto #mqtt #timherden
Principal AI Architect ? Microsoft ? ex-CERN
5 年Pawel K. you might want to see this Tim Herden thanks for sharing
Great work! Very interested, but happy to pay some ?????? , to avoid coding. Well interested, but no time. I’m totally with you, it should be more cost efficient devices and services provided to have easy and energy saving concepts available, monitoring and the. AI controlling. I’ve been testing several brands to get my home controlled 250m2, and did not really find a way yet. So still just investing ???????? to get experiences ?? So far best connections with EWeLink and Sonoff + Alexas, but none of them is connected to my electrivity bill. NordPool connection is missing, plus some planning/forecasting.