Using HomeBridge on a Raspberry Pi to control devices from HomeKit

If you’ve got a selection of devices that you’d like to control using iOS HomeKit, setting up homebridge might be a good solution. In short, it’s a platform which acts as a “bridge” between your devices and “HomeKit”. There are MANY plugins available for it: https://www.npmjs.com/search?q=homebridge.

Here’s how I use it:

  1. I have a Broadlink RM Mini that controls my AC, TV, Radio and a digital photo frame using homebridge-broadlink-rm: https://www.npmjs.com/package/homebridge-broadlink-rm 
  2. I have it enable/disable an indoor camera using MotionEye using homebridge-script2: https://www.npmjs.com/package/homebridge-script2 

With all those plugins, the options are almost endless if the device is connected to the network. To do this I use a Raspberry Pi 2. To get started, you’ll need to have raspbian installed and running. To do this, there are various guides available, including this official one: https://projects.raspberrypi.org/en/projects/raspberry-pi-getting-started.

Now we can install Homebridge. There’s a guide to doing this on a Raspberry Pi available here: https://github.com/nfarina/homebridge/wiki/Running-HomeBridge-on-a-Raspberry-Pi. I found this guide confusing regarding the Node.js installation so I'll try to document the whole process here.
    1. Start with some prep, lets get things up to date:
    sudo apt-get update
    sudo apt-get upgrade

    2. Install some prerequisites:
    sudo apt-get install git make
    In most cases this should do nothing, but older version may need these installed.

    3. Check you gcc version is up to scratch:
    g++ -v

    You'll see a whole lot of text but near the bottom there will be a line starting gcc version. In most cases this should be 4.9.2 or later. If it isn't, install the latest version: https://solarianprogrammer.com/2015/01/13/raspberry-pi-raspbian-install-gcc-compile-cpp-14-programs/ and set it as your default compiler:
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7

    4. Now, we need to find out what version of Node we can run. Run this command
    uname -a
    This will give you a line like this:
    Linux raspberrypi 4.4.50-v7+ 970 SMP Mon Feb 20 19:18:29 GMT 2017 armv7l GNU/Linux

    Take note of that part just before GNU/Linux. e.g. armv71, or on my Pi 2 it was armv6l

    5. Go here and get "Copy link address" for the download link related to the ARM version you found above:

    6. Now run these lines, using the link you've just copied for the first line, and the file name for the second:
    wget https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-armv6l.tar.xz
    tar xJvf node-v6.9.5-linux-armv6l.tar.xz
    sudo mkdir -p /opt/node
    sudo mv node-v6.9.5-linux-armv6l/* /opt/node/
    sudo update-alternatives --install "/usr/bin/node" "node" "/opt/node/bin/node" 1
    sudo update-alternatives --install "/usr/bin/npm" "npm" "/opt/node/bin/npm" 1

    7. Now we can install some last prerequisites:
    sudo apt-get install libavahi-compat-libdnssd-dev

    8. Node makes a mess of the folders so lets clean them up:
    sudo npm config set prefix /usr
   Also it's good to edit /etc/profile and update some env variables by adding to the bottom:
    export NODE_PATH=/usr/lib/node_modules:/opt/node/lib/node_modules
    export PATH="/usr/lib:$PATH"

    9. And then, finally install homebridge:
    sudo npm install -g --unsafe-perm homebridge

    10. Setup up homebridge to run on startup using systemd. First, create a user account to run it
    sudo useradd --system homebridge

    11. Setup a folder for the homebridge config:
    sudo mkdir /var/lib/homebridge
    sudo chown -R homebridge:homebridge /var/lib/homebridge

    12. Setup the systemd files:

    Create the file /etc/default/homebridge:
    sudo vi /etc/default/homebridge
    and add this as the content:
    Create the file /etc/systemd/system/homebridge.service:
    sudo vi /etc/systemd/system/homebridge.service
    and add this as the content:

    13. Then Enable and run the service (first time) with the following commands:
    systemctl daemon-reload
    systemctl enable homebridge
    systemctl start homebridge

    14. You can check the status of the service by calling

    systemctl status homebridge
     or view the logs using
  journalclt -u homebridge -f -a

In another post soon, I'll document my configuration. To get started, you can checkout some information here: https://github.com/nfarina/homebridge

Comments

Popular posts from this blog

Sync iTunes with MythMusic

Using Homebridge and Broadlink RM Mini to automate

LetsEncrypt and AWS ELB Load Balancers