In this article we will configure a power socket in HAP-NodeJS. I will also explain how to build the accessory physically, using a Wemos D1 mini board and a relay shield.
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 Mosquitto and 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/Outlet_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 switch too, pairing is done with the code 031-45-154.
Now let’s move on to the hardware. To build and assemble this switch more quickly, I recommend using the shield with the relay already mounted.
So you will need:

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)
Now, before uploading the Arduino code to the Wemos D1 mini board, we need to update the Arduino code with our Wi-Fi network parameters and the IP address of our Raspberry Pi — and that’s it!

Enjoy!
