One thing I’ve always wanted in my house is to have the ability to control devices power from my phone at a reasonable cost. I’ve finally achieved this with Apple HomeKit with Belkin WeMo, being able to control two sets of lights and my Air Conditioner is extremely handy. There is nothing more satisfying then getting in my car as I leave work each day and saying “Hey Siri, turn on my AC” and then getting home to an icy cold room or being able to control my lights straight from my Apple Watch.

 

Requirements

  • Apple device that supports Apple Homekit – I use my iPhone 6 and Apple Watch Series 2
  • Somewhere to install a Ubuntu server that acts as our bridge from Belkin to Apple HomeKit – This can also be installed on a Rasberry Pi
  • Apple TV 3rd generation or newer – This is only required if you want the ability to control your devices without being on the local Wi-Fi network
  • Belkin WeMo devices to control – I have this one for my lights and this one for my AC, the Insight enables me to track the power usage of the AC

Homebridge

A bit of background on the situation, for devices to support HomeKit they need either a hub or new hardware to then talk to HomeKit with certain authentication methods etc. Thankfully some very awesome people have created their called Homebridge which acts as the hub between your devices an HomeKit. With a fully open source code base behind it  and the ability to create your own plugins the limitations are endless. Read more about the Homebridge project on the Github page here.

In this guide I will go over a basic Homebridge setup using just the Belkin WeMo devices using the Homebridge plugin found here. You can view a full list of the Homebridge plugins here.

Installation

There are a few dependencies we need to install before we can install Homebridge, a compiler, Node.js and then npm which is the package manager.

First start off by installing the C++ compiler by running the following.

sudo apt-get install g++-4.9

We can then confirm it has installed by typing

g++-4.9 -v

This should return something similar to the following

If the install failed you will get an error saying that the program can be found in the following packages…

 

We now want to install python

sudo apt-get install python

 

We will now install Node.js, at the time of writing this these instructions are correct as the latest node is version 6. Refer to here for up to date install instructions.

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs

 

Install the final required dependencies

sudo apt-get install libavahi-compat-libdnssd-dev

 

Now we can install Homebridge!

sudo npm install -g --unsafe-perm homebridge hap-nodejs node-gyp
 cd /usr/lib/node_modules/homebridge/
 sudo npm install --unsafe-perm bignum
 cd /usr/lib/node_modules/hap-nodejs/node_modules/mdns
 sudo node-gyp BUILDTYPE=Release rebuild

 

You’re now ready to run Homebridge to test that all has worked so far.

homebridge

If all has been successful you should see similar to below in the console.

Press Control + C to exit Homebridge, we now need to install the WeMo plugin for Homebirdge

sudo npm install -g homebridge-platform-wemo

Now start Homebridge again and you should see Homebridge detect your Belkin devices if you’ve configured them already.

Configuration

Before we can configure the Homebridge side of things, please configure your WeMo devices in the WeMo app, I would recommend calling the device in the WeMo app the same name you want them to be called in HomeKit. Below are my three devices.

We need to create a folder to store our Homebridge configuration in

sudo mkdir /var/lib/homebridge

We can then create our Homebridge configuration file

sudo nano config.json

Below is an example configuration file, this is the file I use at home, you can set the pin and name to whatever works for you.

I have highlighted in red the name’s that need to match up to the WeMo name from the app as I mentioned earlier. Highlighted in blue is the name that will show up in the Home app and also what you will call the device when asking Siri to control it.

{
    "bridge": {
        "name": "Homebridge",
        "username": "CC:22:3D:E3:CE:30",
        "port": 51826,
        "pin": "031-45-154"
    },
    
    "description": "This is our Homebridge configuration file",

    "platforms": [
        {
            "platform": "BelkinWeMo",
            "name": "TV Lights",
            "description": "TV Lights",
            "BelkinWeMo_name": "TV Lights"
        },
        {
            "platform": "BelkinWeMo",
            "name": "Bed Lamp",
            "description": "Bed Lamp",
            "BelkinWeMo_name": "Bed Lamp"
        },
        {
            "platform": "BelkinWeMo",
            "name": "AC",
            "description": "AC",
            "BelkinWeMo_name": "AC"
        }
    ]
}

Save the config.json file.

 

We can now test our configuration by running the below command to launch Homebridge with specified configuration.

sudo homebridge -U /var/lib/homebridge/

You should see Homebridge detect the devices you configured in the console like below

You will now be able to add the Homebridge to the Home app on your phone, it may prompt you saying the device is not certified, just click Add Anyway

It will then go through the setup of the devices which you’ll then be able to define the device type, I have my AC as a switch and my lights/lamp as lights as it looks a lot cleaner. Make sure to add your devices as a favourite so that it shows up in the Home Control Centre like below. You will now also be able to control the devices with Siri! Give it a go!

 

If all is working we can now move onto having Homebridge run as a service with auto-start.

Service

Thanks to johannrichard on Github for the original Homebridge service configuration found here.

First we need to tell Homebridge to read our configuration file that is located under /var/lib/homebridge, we do this by creating the homebridge file.

sudo nano /etc/default/homebridge

Then paste the contents below into the file and save it

# 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=*

Second we need to create the service file

sudo nano /etc/systemd/system/homebridge.service

Then paste the contents below into the file and save it

[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)
# See comments below for more information
ExecStart=/usr/bin/homebridge $HOMEBRIDGE_OPTS
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target

We then need to create the homebridge user that the service will run as and grant them permission to the configuration folder

sudo useradd -M --system homebridge

Set the homebridge user to the owner of the /var/lib/homebridge/ folder

sudo chown -R homebridge /var/lib/homebridge/

We can now enable the service and start it

systemctl daemon-reload
systemctl enable homebridge
systemctl start homebridge

You can check if the service is running by typing

systemctl status homebridge

That’s it! We are done! I would recommend restarting your machine to ensure the service successfully starts up automatically.