UP | HOME

Air quality monitor

Air quality monitor integrated to TrueNAS

Background

The need of monitoring internal air quality, temperature and humidity was there for some time, so I've decided to implement it using state-of-the-art tools and environments out there. CO2 measurement was an optional requirement, therefore I chose BME680 gas sensor, integrated with TrueNAS, as I have TrueNAS already running for some time at home.

The Home Assistant [BROKEN LINK: [J]] frontend - exits as TrueNAS plugin - seems a great option for subscribing to the sensor data and visualizing it.

2021-07-07_10-58-14_truenas_mqtt.png
Figure 1: Air quality monitor architecture

The sensor is the BME680, gas sensor measuring relative humidity, barometric pressure, ambient temperature and gas (VOC). [BROKEN LINK: [A]]

The microcontroller is an ESP32 [BROKEN LINK: [D]] development board for simplicity.

Schematics

Wire the BME680 to the ESP32 with the SDA pin connected to GPIO 21 and the SCL pin connected to GPIO 22. Connect GND and 3V3 supply lines too.

2021-07-07_11-41-29_esp32_hw.png
Figure 2: ESP32 hardware prototype

Software

System interfacing based on MQTT [BROKEN LINK: [G]].

  • The ESP32 requests sensor readings from the BME680 sensor.
  • The temperature readings are published in the esp/bme680/temperature topic;
  • Humidity readings are published in the esp/bme680/humidity topic;
  • Pressure readings are published in the esp/bme680/pressure topic;
  • Gas readings are published in the esp/bme680/gas topic;
  • TrueNAS (Home Assistant) is subscribed to those topics;
  • TrueNAS (Home Assistant) receives the sensor readings and then displays them on gauges and text fields;
  • You can receive the readings in any other platform that supports MQTT and handle the readings as you want.

Arduino IDE

The ESP32 uses Arduino IDE, so make sure you have the ESP32 add-on installed. [BROKEN LINK: [I]]

Library dependencies

The following libraries have to be installed in order to build the code and to satisfy the dependencies.

Async MQTT Client Library

To use MQTT with the ESP32 we’ll use the Async MQTT Client Library.

Installing the Async MQTT Client Library

  1. Click here to download the Async MQTT client library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get async-mqtt-client-master folder
  3. Rename your folder from async-mqtt-client-master to async_mqtt_client
  4. Move the async_mqtt_client folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Alternatively, you can go to Sketch > Include Library > Add . ZIP library and select the library you’ve just downloaded.

Async TCP Library

To use MQTT with the ESP, you also need the Async TCP library.

  1. Click here to download the Async TCP client library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get AsyncTCP-master folder
  3. Rename your folder from AsyncTCP-master to AsyncTCP
  4. Move the AsyncTCP folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Alternatively, you can go to Sketch > Include Library > Add . ZIP library and select the library you’ve just downloaded.

Note:

Don't install the ESPAsyncTCP library, as requested by Async MQTT Client, as it is not compatible.

BME680 Sensor Libraries

To get readings from the BME680 sensor module, we’ll use the Adafruit BME680 library. You also need to install the Adafruit Sensor library. Follow the next steps to install the libraries in your Arduino IDE:

  1. Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open.
  2. Search for "adafruit bme680" on the Search box and install the library.
2021-07-05_10-58-49_library_bme680.png
Figure 3: BME680 Sensor Libraries

To use the BME680 library, you also need to install the Adafruit Unified Sensor. Follow the next steps to install the library in your Arduino IDE:

  1. Search for "Adafruit Unified Sensor" in the search box. Scroll all the way down to find the library and install it.
2021-07-05_10-58-55_library_unified_sensor.png
Figure 4: Adafruit Unified Sensor Libraries

After installing the libraries, restart your Arduino IDE.

Library hacking

Due to library compatibility issues with ESP32 vs. ESP8266, I applied the following changes to the library code:

Comment the ESP.getChipId() call:

2021-07-05_10-56-11_async_mqtt_ch1.png
Figure 5: AsyncMqttClient.cpp

Include AsyncTCP.h instead of ESPAsyncTCP.h

2021-07-05_10-57-51_async_mqtt_ch2.png
Figure 6: AsyncMqttClient.hpp

Build and flash the ESP32

The code for the ESP32 can be found here: https://github.com/zwwerg/air_quality_monitor

For the board setup, my board config esd the following:

2021-07-05_10-59-42_board_setup.png

First debug log, through the serial console, after flashing the ESP32:

2021-07-05_10-59-46_serial_console.png
Figure 8: Serial console debug view

After a successful wifi connection:

2021-07-05_15-50-35_esp32_mqtt_publish.png
Figure 9: Serial console debug view - publishing

MQTT Broker

To use MQTT, you need a broker. We’ll be using Mosquitto broker installed on a TrueNAS. 8 The plugin installation is painless

2021-07-07_11-02-12_truenas_mqtt_broker.png
Figure 10: TrueNAS MQTT broker plugin install

MQTT Subscriber - Home Assistant

The ESP32 is publishing sensor readings every 10 seconds on four MQTT topics. Now, we can use any dashboard that supports MQTT or any other device that supports MQTT to subscribe to those topics and receive the readings.

Installation of the Home Assistant is done through the TrueNAS plugin installation surface, it was painless after setting up the jail correctly.

As an example, we’ll create a simple flow using Home Assistant to subscribe to those topics and display the readings on gauges. The Home Assistant dashboard can be accessed through the jail's IP address, and not through TrueNAS.

The MQTT integration 11 has to be installed using the Home Assistant dashboard. (Home->Configuration->Integrations->+Add Integration)

Configuration of the MQTT subscriber is not possible in the UI, however it is possible to subscribe to a topic, in order to see everything works fine with the sensor and the MQTT broker.

2021-07-07_11-11-40_ha_mqtt_integration.png
Figure 11: MQTT integration listen to a topic

In order to subscribe and hook the sensor to a gauge, the configuration.yaml of the Home Assistant has to be edited. I have done this through SSH, accessing the file directly from the file system.

## LZS MQTT sensor test
sensor 1:
  - platform: mqtt
    name: "Living room temperature #1"
    state_topic: "esp/bme680/temperature"
    unit_of_measurement: "°C"

Location of the configuration of the Home Assistant:

/mnt/pool0/iocage/jails/home-assistant/root/home/homeassistant/homeassistant/configuration.yaml

After performing a restart, the temperature reading can be accessed through the dashboard.

2021-07-07_11-11-47_ha_temperature_display.png

Next steps

TODO 3D printed enclosure

I plan to design and print an enclosure for the sensor, and deploy many around the house.

TODO Power saving

Sleep mode with periodic wakeup. Plan is to have ~10s measurement period.

Author: Zsombor Lázár

Date: 2021-06-22 Tue 00:00

Emacs 27.2 (Org mode 9.4.5)

Validate

Index