HomeBridge Server on Raspberry Pi

admin Projects, Technology

What is HomeBridge and why should you use it?

HomeBridge is a utility that some wonderful geniuses wrote to allow you to tie together all of the various “almost smart” home devices into Apples new HomeKit framework for Siri to control. If you want to learn more about Siri and HomeKit then I would suggest you start reading Apple’s website for some examples and details. Now that you are familiar with Siri and HomeKit we can move onto using HomeBridge. HomeBridge is a utility that needs to be run on a device that can stay powered on all the time in the background. Ideally you will want something that doesn’t consume lots of power or resources.

That’s where the Raspberry Pi comes in. You can buy a Raspberry Pi 2 on the cheap almost anywhere, my preferred vendor was Amazon this time. At the time of this writing it is priced at $37.70 (Click Here to View on Amazon). Of course you will need a 5V 2Amp Micro USB power supply and a Micro SD Card to install the operating system on. I would strongly recommend getting a fast MicroSD card such as the SanDisk Ultra 32GB MicroSD. The speed is important to the overall performance of your soon to be server.

Proceed with Caution! I am not liable for your mistakes.

Ok I’m convinced. Now what?

Now you will need to get everything together and assembled. Some of the things I’ll be covering will be optional. Such as you can optionally connect your server via Ethernet or WiFi. In that case, I would suggest Ethernet for quicker response time. You may also want to protect your Raspberry Pi by installing it into a case, and there are hundreds of options out there for you to choose between.

Here’s what you will need: 

Once you have acquired all the necessary and desired pieced you will need to download the operating system to install on your raspberry pi. Go to the Ubuntu Raspberry Pi website and download the OS. Once you have it downloaded you will need to download the tool we will use to install the OS to the MicroSD card. I will apologize in advance for providing a SourceForge URL. Should you find the software elsewhere to download, then more power to you. Go to this download page for Win32DiskImager. Once you have that downloaded and running, you will need to write the .iso to the MicroSD card. When selecting your Device, make sure you have chosen the correct drive letter so you don’t accidentally ruin your hard drive. This may take a few minutes, so be patient.

Now assemble all the pieces, plug your Raspberry Pi in. You will want to make sure you have a monitor, mouse, keyboard, and ethernet connected so we can set up the basics.

I have it booted up, whats next?

  • Now we will need to setup the OS, by running the following commands:
    • There are no Raspbian-specific utilities included, specifically no automatic root re-sizer. However, it’s not hard to do manually. Once booted run this command:
      • $ sudo fdisk /dev/mmcblk0
    • Delete the second partition using these key strokes (d, 2), then re-create it using the defaults (n, p, 2, enter, enter), then write and exit (w).
      • Reboot the system, then:
        • $ sudo resize2fs /dev/mmcblk0p2
    • Now to update the system so it you can install the rest of the needed system files:
      • $ sudo apt-get update --fix-missing
    • If you would like to install an SSH server for remote access run this command:
      • $ sudo apt-get install openssh-server
    • There is no swap partition/file included. If you want swap, it’s recommended you do:
      • $ sudo apt-get install dphys-swapfile
      • You should have a (re-sized) SD card at least 4GB, because by default it will want to create a ~2GB swapfile.
  • You will want to secure the OS by changing the Username and Password and removing the default(s)
    • You can use this to change the root password:
      • $ sudo passwd root
    • This will remove the old default user and their directory:
      • $ sudo deluser --remove-home ubuntu
    • Now we will need to add a new user to run the homebridge service later on:
      • $ sudo adduser homebridge sudo
  • We can now setup the IPTABLES to firewall off the server:
    • $ sudo iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
      $ sudo iptables -A INPUT -i lo -j ACCEPT
      $ sudo iptables -A INPUT -p tcp -s xxx.xxx.x.x/29 --dport 22 -j ACCEPT
      $ sudo iptables -A INPUT -s -j ACCEPT
      $ sudo iptables -A INPUT -j DROP
      • The 3rd line is to whitelist a Public IP or Incoming connection from the outside internet. Replace the X’s with your desired external IP or skip that line
  • I would recommend setting up a new service to automatically load the IPTABLES so they last through a reboot:
      • $ sudo apt-get update
        $ sudo apt-get install iptables-persistent
        • Then to whenever changes are made to the firewall, you will need to save them with this command:
          • $ sudo invoke-rc.d iptables-persistent save
  • Now we can setup RSA-SSH Keys to secure the SSH Access instead of plain text passwords:
    • $ mkdir ~/.ssh
      $ chmod 700 ~/.ssh
      $ ssh-keygen -t rsa -b 4096
      • I saved the file to the default location: (/root/.ssh/id_rsa)
      • I set an encryption password, I would recommend storing this somewhere safe if you do.
    • $ cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys
  • Next we will modify the SSH config so that passwords aren’t allowed, only RSA Keys
    • $ sudo nano /etc/ssh/sshd_config
      • Change this line to only allow key access to root:
        • PermitRootLogin without-password
      • Un-comment this line:
        • AuthorizedKeysFile %h/.ssh/authorized_keys
      • Un-comment and change from yes to no:
        • PasswordAuthentication no
    • $ sudo service ssh restart
  • Now we will want to login to the homebridge user and finish setting up the Homebridge utility.
    • $ su homebridge
  • We can now setup the HomeBridge utility

    • First you will need to get the correct NodeJS version and NPM:
      • $ wget https://nodejs.org/dist/v6.2.1/node-v6.2.1-linux-armv7l.tar.gz
        $ tar -xvf node-v6.2.1-linux-armv7l.tar.gz
        $ cd node-v6.2.1-linux-armv7l
        $ sudo cp -R * /usr/local/
    • Then you will need to install homebridge via NPM
      • $ sudo npm install -g homebridge
        $ homebridge
        • This will fail and tell you that your config.json is missing, but it will create your dir.
      • Make sure to create a config-sample.json from the example on the github link and paste the contents in this file:
        • $ sudo nano .homebridge/config-sample.json
      • Copy the sample to the working one, and then edit the working file:
        • $ cp /homebridge/.homebridge/config-sample.json /homebridge/.homebridge/config.json
  • Now we need to setup the homebridge program to run in the background automatically on bootup
    • There are 2 ways to accomplish this: you can run the command in a screen, or set it up as an init.d service
      • Method 1: using Screen (Easy but poor logging)
        • First is to install screen
          • $ sudo apt-get install screen
        • Then edit this file:
          • $ nano /etc/rc.local
        • Then add this line of code above exit 0
          • /usr/bin/screen -d -m -U -S homebridge /usr/bin/homebridge
      • Method 2: setting it up as an init.d service: (Not quite as easy, but much better logging)
        • First is to create the file for the service
          • $ sudo nano /etc/init.d/homebridge
        • Now you will need to copy and paste this example file:
        • Now we will need to modify the following lines of that pasted data, then save it:
          • dir="/home/homebridge"
            cmd="sudo /usr/bin/homebridge"
        • Now we will need to set the permissions of that file and “install” it:
          • $ sudo chmod 755 /etc/init.d/homebridge
          • $ sudo update-rc.d homebridge defaults
        • Finally we need to authorize our user to run that script upon boot.
          • $ sudo visudo
          • Now add the follow lines at the bottom of this section:
            • # Allow members of group sudo to execute any command
              homebridge ALL=(ALL) NOPASSWD: /etc/init.d/homebridg
              homebridge ALL=(ALL) NOPASSWD: /usr/bin/homebridge
              homebridge ALL=(ALL) NOPASSWD: /usr/local/bin/homebridge
              homebridge ALL=(ALL) NOPASSWD: /usr/bin/npm
        • Now you can manually start the service with this:
          • $ sudo /etc/init.d/homebridge start

Ok, I’ve done all that… What now?

You’re done! Congratulations! You should now have a fully functioning HomeBridge server on your Raspberry Pi. Go have some fun with it, and create more!

Well wait, hold on a sec. Mine isn’t working!.

You can always join the Slack Team and ask questions.

Resources and References: