Controlling an Air Quality Sensor from Apple HomeKit with HAP-NodeJS on Raspberry Pi and Wemos D1 mini via MQTT

In this article we will build and configure an air quality sensor in HAP-NodeJS. I will also explain how to build it physically, using a Wemos D1 mini board, an MQ2 sensor and a DHT22 sensor.
Here too, I used the MQTT protocol (Mosquitto) to make the Raspberry Pi and the Wemos D1 mini communicate with each other.

As a prerequisite, you should of course have a Raspberry Pi with HAP-NodeJS correctly installed.
Below are the links to the two guides:
Apple HomeKit on Raspberry Pi
MQTT Broker (Mosquitto) on Raspberry Pi

You must also have installed the npm packages for the MQTT module; if you haven’t, the commands are as follows:

cd /home/pi/HAP-NodeJS
sudo npm install mqtt --save
sudo npm install mqtt -g

To start, connect to your Raspberry Pi via SSH and run the following commands to download and install the accessory.

cd /home/pi/HAP-NodeJS/src/accessories
wget https://fargionconsulting.com/wp-content/uploads/2020/02/AirQualityTempHumSensor_accessory.ts

Now, if you have set up HAP-NodeJS to start automatically (as described in this guide), restart the Raspberry Pi so that HAP-NodeJS detects the new accessory.
Also, if you have chosen to use BridgedCore.ts, the accessory will automatically appear alongside the accessory you created and paired previously; if you are using Core.ts, you will need to add the new accessory from your Apple device. For this sensor too, pairing is done with the code 031-45-154.

Now let’s move on to the hardware. As mentioned at the start, this accessory requires two sensors: a DHT22 and an MQ2.
For the DHT22 sensor, I recommend using the Wemos D1 mini shield with the DHT22 sensor already soldered on, while the MQ2 sensor needs to be wired ‘manually’.
Looking into the MQ2 library (used to get the most accurate readings from this sensor), I noticed that by supplying it with temperature and humidity, it could calculate the CO2/PPM level in the air more accurately. That is why I decided to add a DHT22 sensor to the same board.
Even with these sensors and this library, the sensor still needs to be calibrated. To make this as simple as possible, I modified the MQ2 sensor library (integrating it directly into the Arduino code) and added a switch (via HomeKit) to start calibration mode.
Calibration only needs to be done once (the first time, and again if you change the USB cable or power supply), by placing the whole board with both sensors outdoors so that it can detect the value for clean air (400 CO2/ppm). Calibration takes about an hour and ends on its own, but you can stop it at any time if needed by turning off the switch in HomeKit.
In addition, once the sensor has been calibrated, it stores the calibration value in its EEPROM, so that even if the board is switched off and on again, it keeps the calibration value in memory.

The wiring diagram is shown below.

Now let’s move on to the Arduino code for the Wemos D1 mini, which you can download from this link.

Here too, note that some libraries need to be installed in the Arduino IDE; they are listed below:

  • ArduinoOTA (available from the Library Manager in the Arduino IDE)
  • PubSubClient by Nick O’Leary (available from the Library Manager in the Arduino IDE)
  • RemoteDebug by Joao Lopes (available from the Library Manager in the Arduino IDE)
  • DHT library available from this link

Now, before uploading the Arduino code to the Wemos D1 mini board (as well as selecting the right serial port, the correct board and the correct baud rate), we need to update the Arduino code with the name and password of our Wi-Fi network and the IP address of our Raspberry Pi — and that’s it!

Enjoy!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top