In this article we will configure a temperature sensor in HAP-NodeJS. I will also explain how to build it physically, using a Wemos D1 mini board and, of course, a temperature 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-NodeJSsudo 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/TempHumSensor_accessory.ts
Now, if you have set up HAP-NodeJS to start automatically, 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. To build and assemble this sensor more quickly, I recommend using the shield with the sensor already mounted.
There are many types of temperature sensor, many of which can also measure other values such as humidity or atmospheric pressure. In this case we will use the DHT22 sensor, which measures humidity as well as temperature, so you will need:

the wiring diagram is shown below (in case you decide not to use the shield but a separate sensor)

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!
