It sets up HAP-NodeJS (HomeKit Accessory Server) for Sonoff switches and togling the Raspberry Pi's GPIO pins.
On a clean Raspberry Pi with a fixed IP, issue the following command to install the server:
curl -sSL https://goo.gl/8oN37T | 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
Before you can start your installation of a HAP-NodeJS server, you first need to preconfigure your Raspberry Pi:
I use raspbian Jessie Lite. The Lite version because we don't need a graphical desktop. If you're on a mac but don't really know how to flash to OS on your SD card, then I can recommend Etcher.
When you boot your Raspberry Pi, it will have a user named 'pi'
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 using CLI, than you can take a look on how I did it.
This is necessary if you want to connect to your Pi when it's tucked away somewhere with limited physical access.
You can do both things using the raspi-config configuration window:
sudo raspi-config
Now you can SSH into your Raspberry pi (change IP address with your own static IP address):
It's recommended to change the default password for the user 'pi' on your raspberry pi. This can be easily done by using the following command, it will ask you for a new password.
sudo passwd pi
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/8oN37T | 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 test your installation, you can configure the onboard LED of your Raspberry Pi as a light accessory. This is also a great way to learn how to use the example accessories.
To install the LED accessory, execute the following command:
curl -sSL https://goo.gl/JGdV8Z | 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.
This will install 2 files:
- /home/pi/HAP-NodeJS/accessories/Light_OnBoardLED_accessory.js
- /home/pi/HAP-NodeJS/python/onboard-LED.py
In the accessory file, you can see that you must change some variables (if you start from the demo file), located on line 14-19. The comments in the code explain the meaning of each variable.
Next you might want to change what script gets executed when the light changes of state. Line 30 shows the script that will be executed. Change to your needs.
The script that will toggle the LED is located in the python folder (as shown on Line 30 in the accessory file).
Now you're able to create a light accessory. If you want to create a second light accessory file, just duplicate the Light_OnBoardLED_accessory.js file, change the name, and change line 14-19 and 30.
To remove the onboard LED accessory, execute the following command:
rm /home/pi/HAP-NodeJS/accessories/Light_OnBoardLED_accessory.js /home/pi/HAP-NodeJS/python/onboard-LED.py && echo mmc0 | sudo tee /sys/class/leds/led0/trigger && restartHAP
To remove any other accessory, You can just remove the accessory.js file (and, optionally, the python script). Be sure to always restart the HAP-NodeJS server by executing the following custom command:
restartHAP
I included 2 other files:
- A simple light accessory file that toggles a GPIO pin (e.g. to toggle a LED that you've connected).
- The python script to toggle the GPIO pin.
To use these, just copy the files to the correct folder, change the correct lines in the accessory file, and extra: change line 13 of the accessory file with the GPIO number of the output that you want to control.
Be sure to always restart the HAP-NodeJS server after changing, removing or adding accessories, by executing the following custom command:
restartHAP
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).
Next you'll need to install Atom. This is my default text editor on my mac, and I really can recommend it. The advantage of Atom is the ability to install certain packages that extend functionality. We are going to use this ability and we have to install the plugin PlatformIO IDE. With this plugin we'll be able to upload our code the the Sonoff device and start a serial monitor.
The installation can take a few minutes. Be sure to restart atom when you're finished with the installation.
After restarting Atom, you'll be able to create a PlatformIO account, but you can skip this.
In Atom go to 'PlatformIO > Open project folder' and select the folder of the FW that you've downloaded. Normally the name of the folder will be 'Sonoff-Tasmota-master'.
The tree view of the project files will appear on the left side of Atom. Unfold the folder 'sonoff' and open the file 'user_config.h'.
In 'user_config.h' you need to edit line 42 and 43. These lines define 'STA_SSID1' and 'STA_PASS1' which is respectively the SSID of your network (wireless network name) and your WiFi Password. Optionally, you can edit line 44 and 45 too, these lines configure a backup wireless network (if available).
You're free to change other settings in this file, the file itself has many comments so everything should be clear. I personally chose to give the device a fixed IP so that all my Sonoff devices are in the same range of my network.
Once you're done, go to 'File -> Save' to save the 'user_config.h' file.
To connect your programmer to the Sonoff device, you'll have to solder a header on the Sonoff device. There are enough tutorials to find online about how to do this. In the end, your connection between the programmer and the Sonoff switch will be as following:
SONOFF J1 | PROGRAMMER |
---|---|
1 : VCC-3V3 | 3V3 |
2 : E-RX | TXD |
3 : E-TX | RXD |
4: GND | GND |
5 : GPIO14 | Not connected |
With pin 1 the pin against the switch on the Sonoff device.
Please be sure that the Sonoff device is not connected to 120V or 230V...
Now connect your programmer to your computer using a USB cable AND press the button on the Sonoff device when plugging in to enable write mode.
In atom, click on the 'platformio.ini' file (located at the bottom in the tree view), then go to 'PlatformIO -> Upload' to (build and) upload the firmware into the Sonoff device.
When the code is successfully loaded into the Sonoff device, go to 'PlatformIO -> Serial monitor' and select your serial device (programmer) in the dropdown list. Then you can set the baudrate to '115200'. Click 'start' to start the serial monitor.
When the messages stop appearing, unplug and replug the VCC pin of the Sonoff device (pin 1 in the previous table). This will restart the device.
In the logs that appear, you'll now find a log that will look like this:
00:00:08 HTTP: Webserver active on sonoff-1234.local with IP address 192.168.1.50
You can now surf to your Sonoff device with your favorite webbrowser. Go to 'sonoff-1234.local' or '192.168.1.50' to open the web interface of the Sonoff device.
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.