Homekit Camera with Raspberry Pi Zero and HKCam

Installing HKCam on a Raspberry Pi with one of the good old ribbon camera modules works well... if you come over some hiccups.

screenshot-2024-10-01-at-17.43.31

Here is how I get it to work:

  1. Setup a fresh RaspbianLite and install HKCam by following the instructions on: https://github.com/brutella/hkcam

  2. Enable legacy camera support:

$ sudo raspi-config nonint get_legacy
$ sudo raspi-config nonint do_legacy 0
$ sudo reboot

Afterwards, you should get output similar to the following:
$ vcgencmd get_camera
supported=1 detected=1, libcamera interfaces=0

https://github.com/brutella/hkcam/issues/141#issuecomment-1687316736

  1. Configure daemon for startup at boot:

Create a new systemd service file:
$ sudo nano /etc/systemd/system/hkcam.service

Add the following content to the file:

[Unit]
Description=HKCam HomeKit Camera Service
After=network.target

[Service]
ExecStart=/home/user/hkcam -data_dir=/var/lib/hkcam/data -multi_stream=true -port=8080
Restart=always
User=user

[Install]
WantedBy=multi-user.target

modify ExecStart=/home/user/hkcam and User=user according to your HKCam location and user.

  1. Configure systemd
    Set the correct permissions for the service file:
    $ sudo chmod 644 /etc/systemd/system/hkcam.service

Reload the systemd daemon:
$ sudo systemctl daemon-reload

Enable the HKCam service to start at boot:
$ sudo systemctl enable hkcam.service

  1. Start the service
    You can start the service immediately without rebooting:
    $ sudo systemctl start hkcam.service

  2. Verify the service
    Check the status of the service:
    $ sudo systemctl status hkcam.service

If you encounter any issues, you can check the system logs for more information:
$ journalctl -u hkcam.service

This should work now but the image quality is terrible. Can we fix this?

Check video resolution of connected camera:
$ v4l2-ctl --list-formats-ext

Then you can create and edit the file /etc/sv/hkcam/runto fiddle around with some settings:

pi@raspberrypi:~ $ cat /etc/sv/hkcam/run
#!/bin/sh -e
exec 2>&1
v4l2-ctl --set-ctrl=rotate=90
v4l2-ctl --set-fmt-video=width=1280,height=720,pixelformat=YU12
v4l2-ctl -c video_bitrate=1000000
exec hkcam --data_dir=/var/lib/hkcam/data --verbose=true

Make it unable with $ sudo chmod +x /etc/sv/hkcam/run.

img_4677