It sets up HAP-NodeJS (HomeKit Accessory Server) for Sonoff switches on an RPI with OSMC.
On a clean Raspberry Pi with a fixed IP and installed OSMC, issue the following command to install the server:
curl -sSL https://goo.gl/pH5zCp | bash
Then install an accessory (Sonoff or GPIO pin) using the following command:
accessoryInstaller
I personally use the HAP-NodeJS server to connect a couple of Sonoff devices to my Apple Home setup. This way I can transform my dumb devices and lighting into smart devices and smart lighting.
Check out below to see how to use and configure a Sonoff basic device to be compatible with HAP!
- Install HAP
- Accessory installer (very usefull)
- Sonoff
I use OSMC.
Preferred way is connecting your Raspberry Pi using ethernet and giving it a fixed IP address.
If you do not know how to set a fixed IP address, take a look at the network settings in osmc.
This is the standard loggin for SSH:
username: osmc
password: osmc
To install HAP-NodeJS, I created a simple script that will do everything for you. Just execute the following line on your Raspberry Pi, lay back and relax. This can take a while (up to 1 hour, maybe even longer...).
curl -sSL https://goo.gl/pH5zCp | bash
cURLing and Piping To Bash can be dangerous, if you do not trust this, I recommend you to download the file with 'wget', check its content and then run the installer yourself.
When this completes, HAP-NodeJS will be installed and will be running. There will be 2 folders that you'll update regulary when setting up your Homekit devices:
- cd /home/pi/HAP-NodeJS/accessories
- cd /home/pi/HAP-NodeJS/python
The accessories folder will contain all Homekit accessories (e.g. power points, lighting, ...). The accessory files must all end on 'accessory.js'.
The python folder will contain all scripts that you want to execute when the state of a certain accessory changes (configurable in the accessories).
To add a device easily, without having to change source-code, copying files, etc, I have added an installer that guides you through the installation of everything. This installer is able to create, add and configure 2 accessories:
- An accessory that toggles a GPIO pin of the raspberry Pi.
- A Sonoff switch accessory
To run the installer, use the following command:
accessoryInstaller
The installer should be pretty clear, but I want to give you one extra tip:
Choose a good name for the accessory (e.g. 'small kitchen lights in the left corner' instead of 'lights'). This will ease your life when removing accessories later.
For this, you'll need to flash the Sonoff device with a custom firmware. The programmer that you'll need is a USB to TTL Serial Adapter. I bought mine on eBay for about 5 euros.
To use the Sonoff device, you'll have to flash a custom firmware that provides the Sonoff device with Web, MQTT and OTA functions. To download the necessary files, you can click here. Be sure to unzip these files and store them somewhere you can find them (I just kept them in my Downloads folder).
To install the Sonoff packages, you have to do absolutely nothing. If you followed this guide, you already have them installed! I included them in the main HAP-NodeJS installer.
Firstly, you'll have to create a copy of the example accessory that I've already installed:
cp /home/pi/HAP-NodeJS/accessories/examples/SonoffMQTT_accessory.js /home/pi/HAP-NodeJS/accessories/SonoffMQTT_accessory.js
To configure the accessory, you almost have to do the same things like you had to do with the light accessory.
Open the file '/home/pi/HAP-NodeJS/accessories/SonoffMQTT_accessory.js'. In this file you must edit (at least) these lines:
- Line 16: This will be the name of the accessory.
- Line 17: This will be the pincode that you'll need to connect to the accessory.
- Line 18: This must be a unique hexadecimal code (0-9 and A-F), unique for every accessory.
- Line 21: This is the name of the Sonoff device that you've configured on the web interface of the Sonoff device (see next paragraph).
Save the file and exit.
Secondly, we'll change some settings on the Sonoff's web interface. Open the website of the Sonoff device and then click on 'Configuration -> Configure MQTT'. Here we will change 2 things:
- Host: Enter the static IP of your Raspberry Pi.
- Topic: Enter a name for the device WITHOUT spaces. (E.g. 'kitchenlights')
Click save to save this configuration and restart the device.
If you now open 'Console' on the website of the device, you should be able to see the MQTT commands if the device has been connected successfully to the Raspberry Pi.
Next go to 'configuration -> Configure other'. Here we will change again 2 things:
- Friendly name: A friendly name for the device. (E.g. 'Kitchen Lights')
- Emulation: We select 'Belkin WeMo'. Click save to save this configuration.
Lastly, we go to our raspberry pi and restart the HAP-NodeJS server with the following command:
restartHAP
This is a quick tip, but you can send commands directly to your Sonoff device from your raspberry pi. For more information on this, check this wiki.