Installing Homebridge and homebridge-camera-rpi on a Raspberry Pi Zero W

I finally started over from scratch with my Raspberry Pi Zero W and Camera Module V2. These are the steps I took to configure it with Homebridge as a Homekit-compatible camera.

Initial Pi Setup

  1. Using balenaEtcher, I installed the September 2019 version of Raspbian Buster Lite (link to latest release) on my SD card.
  2. I enabled ssh access by creating an empty file named ssh at the root of the SD card.
  3. To allow the Pi onto my home network, I created a file named wpa_supplicant.conf at the root of the SD card. The contents should be:

    country=US 
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    network={
        ssid="YOUR_NETWORK_NAME"
        psk="YOUR_PASSWORD"
        key_mgmt=WPA-PSK
    }
    
  4. Then I inserted the SD card in the Pi, booted it up, found it’s IP address on my network (I used LanScan), and connected via ssh pi@<ipaddress>.

  5. It’s important to change the pi account password with the passwd command.
  6. I selected my time zone and set the locale to en_us.UTF-8 via sudo raspi-config.
  7. Finally, I updated the software with:

    sudo apt-get update
    sudo apt-get upgrade
    

Install Homebridge, homebridge-pi, and homebridge-camera-rpi

  1. Homebridge requires an installation of node.js, which, surprisingly, can’t be installed via apt on the Pi Zero. This command will install the most recent LTS version of node.js by running a script from node-pi-zero:

    wget -O - https://raw.githubusercontent.com/sdesalas/node-pi-zero/master/install-node-v.lts.sh | bash
    
  2. From Running Homebridge on a Raspberry Pi, Homebridge will require git to be installed:

    sudo apt-get install git
    
  3. Those instructions also indicate that avahi must be installed:

    sudo apt-get install libavahi-compat-libdnssd-dev
    
  4. Finally, we can actually install homebridge:

    sudo npm install -g homebridge
    
  5. As well as homebridge-pi (which will provide thermal measurements from the Pi in Homekit):

    sudo npm install -g homebridge-pi
    
  6. Before installing homebridge-camera-rpi (which will expose the camera to Homekit), we need to:

    • Activate the camera via raspi-config
    • Edit sudo nano /etc/modules and add the line bcm2835-v4l2
    • Reboot
    • Install ffmpeg: sudo apt install ffmpeg
  7. Then, we can install homebridge-camera-rpi:

    sudo npm install -g homebridge-camera-rpi
    
  8. And then setup the configuration file (~/.homebridge/config.json) to contain:

    {
      "bridge": {
        "name": "PiZeroHomebridge",
        "username": "CC:22:3D:E3:CE:30",
        "port": 51826,
        "pin": "031-45-154"
      },
    
      "description": "This is an example configuration file",
      "accessories": [
        {
          "accessory": "PiTemperature",
          "name": "Pi Zero Temperature"
        }
      ],
    
      "platforms": [
        {
          "platform": "rpi-camera",
          "cameras": [{"name": "Pi Camera"}]
        }
      ]
    }
    
  9. Finally, I needed to add homebridge to my path via export PATH=/opt/nodejs/bin:$PATH.

  10. Now, you can run homebridge and add the devices to HomeKit configuration.

Configuring Homebridge to run on Bootup

There are great instructions for running Homebridge automatically at Bootup. These are the steps I used to configure it as a system service:

  1. Download these files:

    • homebridge to /etc/default via this command:

      sudo wget -O /etc/default/homebridge https://gist.github.com/johannrichard/0ad0de1feb6adb9eb61a/raw/1cf926e63e553c7cbfacf9970042c5ac876fadfa/homebridge
      
    • homebridge.service to /etc/systemd/system via this command:

      sudo wget -O /etc/systemd/system/homebridge.service https://gist.github.com/johannrichard/0ad0de1feb6adb9eb61a/raw/1cf926e63e553c7cbfacf9970042c5ac876fadfa/homebridge.service
      
  2. Fix path to the homebridge binary in homebridge.service with:

    sudo sed -i 's,/usr/local/bin/homebridge,'"$(which homebridge)"',' /etc/systemd/system/homebridge.service
    
  3. Create a new user:

    useradd --system homebridge
    
  4. Per the camera setup instructions, add the new user to the video group:

    sudo adduser homebridge video
    
  5. For convenience, I added the pi user to the homebridge group as well, so it can edit the configuration files:

    sudo adduser pi homebridge
    
  6. Create a new directory for the configuration files:

    sudo mkdir /var/lib/homebridge
    
  7. Give the new user ownership of the configuration files:

    sudo chown -R homebridge:homebridge /var/lib/homebridge/
    
  8. Give the group write permissions for the configuration files:

    sudo chmod -R 775 /var/lib/homebridge/
    
  9. Log out and log back in (still as pi) to pick up the new permissions.

  10. Duplicate the configuration file in the new folder:

    cp .homebridge/config.json /var/lib/homebridge/config.json
    
  11. Make sure the ownership and permissions for config.json are set:

    • sudo chown homebridge:homebridge /var/lib/homebridge/config.json
    • sudo chmod 664 /var/lib/homebridge/config.json
  12. Have the system read the new configuration:

    sudo systemctl daemon-reload
    
  13. Enable it, start it up, and confirm it’s running:

    sudo systemctl enable homebridge
    sudo systemctl start homebridge
    systemctl status homebridge
    

And with that, you can now just plug in the Raspberry Pi and it will boot and start up homebridge with your new camera! This is the cheapest way I’ve seen to get a HomeKit camera up and running.