DiY Apple Homekit Camera With Raspberry Pi

Apple Homekit products can be expensive. Instead, I purchased a tiny Raspberry Pi Zero W with a NoIR Camera for $45, and wanted to turn it into something useful. I decided to try setting it up as a Homekit camera whose feed could be checked from the Apple Home app. It’s really quite simple: here are the steps I followed to get everything up and running.

Preparing the SD card and raspberry pi firmware

  1. On my Mac laptop, I opened a terminal and downloaded the latest rasbian lite image

     wget https://downloads.raspberrypi.org/raspbian_lite_latest
    
  2. Use etcher to write the image to my SD card.
  3. Once etcher finished writing, eject and re-insert the SD card.
  4. Set up wifi on the SD card so we can set up the pi without a display

     cd /Volumes/boot
     touch ssh # turns on ssh
     vim wpa_supplicant.conf
    
  5. Enter the following in the wpa_supplicant.conf file (be sure to replace YOUR_SSID and YOUR_PASSWORD according to your router settings):

     country=US
     ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
     update_config=1
    
     network={
      ssid="YOUR_SSID"
      scan_ssid=1
      psk="YOUR_PASSWORD"
      key_mgmt=WPA-PSK
     }
    
  6. You also need to enable the camera in the firmware.

     vim config.txt
    
  7. Enter the following in the config.txt file:

     start_x=1             # essential
     gpu_mem=256           # at least 128, or maybe more if you wish
     #disable_camera_led=1  # optional, if you don't want the led to glow
    
  8. Eject the SD card from the laptop

Preparing the Raspberry Pi operating system

  1. Insert the SD card into the pi, boot the pi and wait 30-90 seconds

  2. Find the IP address that your router assigned to the pi. This can be done with some wifi scanning apps, or by checking your router settings (usually available at 192.168.1.1 on a machine connected to your router). In my example, the IP address for my pi is 192.168.1.110.

  3. Log into the pi using the default password raspberry and set up a new user named rob:

     ssh pi@192.168.1.110
     sudo adduser rob
     sudo visudo
    
  4. Enter the following at the end of the file:

     rob ALL=(ALL) ALL
    
  5. Then we can delete the default pi user for security reasons, and log back in with our regular user:

     sudo deluser --remove-home pi
     logout
     ssh rob@192.168.1.110
    
  6. Make sure the user rob can read the video device using the sudo command (i.e., add user rob to group video):

     sudo usermod -a -G video rob
    
  7. Note that you can do the following to immediately enable the correct permissions, but it won’t stick after a reboot:

     sudo chmod 777 /dev/video0
    
  8. Next install the kernel driver for the pi camera

     sudo modprobe bcm2835-v4l2
     sudo vim.tiny /etc/modules
    
  9. Enter this into the modules file:

     bcm2835-v4l2
    
  10. Update the operating system and reboot so updates and kernel modules are applied:

     sudo apt-get install tmux
     tmux
     sudo apt-get update && sudo apt-get upgrade -y
     sudo reboot
    

Installing a stand-alone homebridge camera

We will be using https://github.com/moritzmhmk/homebridge-camera-rpi

  1. Install the tools we will need for building the camera

     sudo apt-get install git make g++
     sudo apt-get install nodejs
     sudo apt-get install npm
     sudo apt-get install ffmpeg
     sudo npm install -g homebridge-camera-rpi
    
  2. Download the homebridge camera software

     cd /opt
     sudo mkdir homebridge-camera-rpi
     sudo chown rob:rob homebridge-camera-rpi
     git clone https://github.com/moritzmhmk/homebridge-camera-rpi
     cd homebridge-camera-rpi
     npm install
    
  3. Create a service so we can easily start/stop the software:

     sudo vim.tiny /etc/systemd/system/hap-camera-rpi.service
    
  4. Enter the following into the hap-camera-rpi.service file:

     [Unit]
     Description=HAP Camera RPi
    
     [Service]
     ExecStart=/usr/bin/node /opt/homebridge-camera-rpi/standalone.js -c /etc/homebridge-camera-rpi.conf.json
     WorkingDirectory=/opt/homebridge-camera-rpi
     Restart=always
     RestartSec=10
     User=pi
    
     [Install]
     WantedBy=multi-user.target
    
  5. Configure the settings for the camera software:

     sudo vim.tiny /etc/homebridge-camera-rpi.conf.json
    
  6. Enter the following into the homebridge-camera-rpi.conf.json file:

     {
       "name": "Pi Camera",
       "id": "Pi Camera",
       "pincode": "031-45-154",
       "username": "EC:23:3D:D3:CE:CE"
     }
    
  7. Set the homebridge camera, and make sure it starts at boot time

     sudo systemctl start hap-camera-rpi
     sudo systemctl enable hap-camera-rpi
    
  8. Now open the Apple Home app and add the device manually using the code 031-45-154

Adjust some camera settings

  1. I found it useful to adjust the camera settings slightly for a better picture; this can be done using the video4linux tools:

     v4l2-ctl --list-ctrls
     v4l2-ctl --list-ctrls-menu
     v4l2-ctl --set-ctrl brightness=60
     v4l2-ctl --set-ctrl saturation=20
     v4l2-ctl --set-ctrl contrast=20
     v4l2-ctl --set-ctrl white_balance_auto_preset=9
    
  2. I found that the camera would sometimes freeze, so lets make sure it stays up by restarting the camera every night:

     sudo su
     vim.tiny /root/reset-hap-camera.sh
    
  3. Enter the following into the reset-hap-camera.sh script:

     systemctl restart hap-camera-rpi
     v4l2-ctl --set-ctrl brightness=60
     v4l2-ctl --set-ctrl saturation=20
     v4l2-ctl --set-ctrl contrast=20
     v4l2-ctl --set-ctrl white_balance_auto_preset=9
    
  4. Edit the crontab for the root user to run scripts on a schedule:

     sudo su
     crontab -e
    
  5. Append the following to run the script every night at midnight:

     0 0 * * * /usr/bin /root/reset-hap-camera.sh
    

That’s it!

Your device will only connect to the camera stream from the pi when you open the Apple Home app; it won’t be able to record video when it detects movement or anything fancy. Also, It doesn’t seem to work remotely; that is, you cannot view the camera stream from a device that is not connected to the same WiFi network as the camera, event if you have a home hub (HomePod, Apple TV, or iPad).

~Rob