In my previous post on this project, I described my initial set-up of my Raspberry Pi.
The next step was to interface the RPi (see how nonchalant I’ve become?) to HomeKit, Apple’s mechanism for home automation. There’s already software for this written by the RPi user community: Homebridge.
Homebridge depends on another package, Node.js, which I see as a way of running Javascript outside of a web browser. I followed the instructions on this page (possibly a mistake; I might have been better off with the Homebridge web page) by installing the latest version of Node.js:
wget -O - https://raw.githubusercontent.com/sdesalas/node-pi-zero/master/install-node-v.last.sh | bash
When you do this, you want to want to assure that the system will always be able to find the location of npm:
sudo emacs /etc/profile.d/node.sh
(Remember, emacs is my preferred text editor on UNIX; you may want to use vi or nano. Also note that we’re editing system files, which are normally protected from abuse by ordinary users; you have to use sudo for permission to change them.)
I added the following line:
export PATH=$PATH:/opt/nodejs/bin
After that (and logging off and logging back in again for the above script to work), I could install Homebridge:
npm install -g homebridge
For some reason, there was an installation problem with Homebridge. It was no problem to solve, but I’ll document it just in case someone has the same issue in the future. (I’m going a little out-of-order here, since I didn’t detect this problem until after I started playing with systemd as I describe below).
It turned out that the Homebridge installation was supposed install a separate ‘homebridge’ account, but had not done so. I added the account manually, by editing the system’s password file:
sudo emacs /etc/passwd
I added this line to the end of /etc/passwd:
homebridge:x:911:911:/var/lib/homebridge:/bin/false
That defines a user ID, but users must belong to groups. I edited the system’s group file:
sudo emacs /etc/group
I added the line:
homebridge:x:911:
If you did a web search on the passwd file, you’ll see that I defined the home directory to be ‘/var/lib/homebridge’, since that’s the directory that Homebridge stores its configuration and work files. I had to grant permission for the ‘homebridge’ account to access this directory:
sudo chown -R homebridge:homebridge /var/lib/homebridge
I could start Homebridge just by typing the command
homebridge
Is that good enough? No. I wanted Homebridge to automatically start every time I turned on the Raspberry Pi. To make that happen, I had to fiddle with Raspian’s service manager, systemd. I created a new systemd service file for Homebridge:
sudo emacs /etc/systemd/system/homebridge.service
I put the following in that file:
[Unit] Description=Node.js HomeKit Server After=syslog.target network-online.target [Service] Type=simple User=homebridge EnvironmentFile=/etc/default/homebridge # Adapt this to your specific setup (could be /usr/bin/homebridge) ExecStart=/opt/nodejs/bin/homebridge $HOMEBRIDGE_OPTS Restart=on-failure RestartSec=10 KillMode=process [Install] WantedBy=multi-user.target
I decided I wanted to follow general UNIX standards, and put the Homebridge options in a separate file:
sudo emacs /etc/default/homebridge
I edited it to:
# Defaults / Configuration options for homebridge # The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others) HOMEBRIDGE_OPTS=-U /var/lib/homebridge # If you uncomment the following line, homebridge will log more # You can display this via systemd's journalctl: journalctl -f -u homebridge # DEBUG=*
I had to issue the following command so that systemd would know I fiddled with its configuration:
sudo systemctl daemon-reload
Then I could start running Homebridge using systemd:
sudo systemctl start homebridge
Again, the reason to go through this additional work is that the above command would now automatically be executed every time the RPi was turned on.
There are a couple of ways to monitor the execution of Homebridge. I preferred to look at the system log file using the less command::
sudo less /var/log/syslog
As it stands, Homebridge won’t do much. That’s because it requires two additional components: a configuration file and a plugin to tell Homekit what it can actually do.
I started with a configuration file:
sudo emacs /var/lib/homebridge/config.json
I put in the following lines:
{ "bridge": { "name": "Homebridge", "username": "B8:27:EB:8F:C5:D7", "port": 45525, "pin": "031-45-154" } }
A couple of notes here:
- The username field I set to the MAC address of the Ethernet port of my RPi. See my previous post for how to find this out.
- Getting the braces and brackets and commas correct in a JSON file turned out to be tricky. I found a JSON syntax checker that made fiddling with these details much easier.
I restarted Homebridge so it would use the new configuration file.
sudo systemctl restart homebridge
Once I did this, I could run the Eve iOS app on my iPhone. In that app, I went to Setting->Accessories->Add Accessory to Home. I selected manual entry of the new Homekit device, and typed in the same number that I put into the configuration file above: 031-45-154. I fiddled with the accessory’s configuration in the app, and changed its name to “Air Conditioner”.
I originally planned to discuss the next step, controlling GPIO pins on the Raspberry Pi, in this post. Now that I see how long it is, I’ll end things here and discuss GPIO in my next blog post.
Pingback: 230V Air Conditioner and HomeKit, part 5: GPIO – The Argothald Journal
Pingback: 230V Air Conditioner and HomeKit, part 8: Power Cords – The Argothald Journal
Pingback: 230V Air Conditioner and HomeKit, part 9: The Box – The Argothald Journal