• Stars
    star
    881
  • Rank 51,820 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Open-Source HomeKit Surveillance Camera

hkcam

hkcam is an open-source implementation of an HomeKit IP camera. It uses ffmpeg to access the camera stream and publishes the stream to HomeKit using hap. The camera stream can be viewed in a HomeKit app. For example my Home+ app works perfectly with hkcam.

Camera Stream

Features

Get Started

hkcam uses Go modules and therefore requires Go 1.11 or higher.

Mac

The fastest way to get started is to

  1. download the project on a Mac with a built-in iSight camera
git clone https://github.com/brutella/hkcam && cd hkcam
  1. build and run cmd/hkcam/main.go by running task hkcam in Terminal
  2. open any HomeKit app and add the camera to HomeKit (pin for initial setup is 001 02 003)
  3. view the web ui by entering the url http://localhost:8080

These steps require git, go, task and ffmpeg to be installed. On macOS you can install them via Homebrew.

brew install git
brew install go
brew install go-task/tap/go-task
brew install ffmpeg

Raspberry Pi

You can use a camera module or USB camera with a Raspberry Pi to create your own surveillance camera.

ELP 1080p

For example the ELP 1080P USB camera dome is great for outdoor use. It is IP66 waterproof and has built-in IR LEDs for night vision. This camera gets you good quality and great performance when running hkcam on the latest Raspberry Pi OS.

A cheaper alternative is a camera module attached via ribbon cable. You'll have to enable Legacy Camera Support when using a camera module on Raspberry Pi OS. That's why this option is not ideal in my opinion.


How to Install on a Raspberry Pi?

Follow these steps to install hkcam and all the required libraries on a Raspberry Pi OS Lite (32-bit).

  1. Download and run the Raspberry Pi Imager from https://www.raspberrypi.com/software/

Raspberry Pi Imager

  • Choose OS → Raspberry Pi OS (other) → Raspberry Pi OS Lite (32-bit)

Raspberry Pi Imager

  • Insert a sd card into your computer and choose it as the storage

Raspberry Pi Imager

  • Click on the settings icon and enable SSH, Set username and password and configure wifi

Raspberry Pi Imager

  • Write the operating system on the sd card by clicking on Write

Raspberry Pi Imager

  1. Insert the sd card in your Raspberry Pi

  2. Connect your camera (in my case the ELP 1080P) and power supply

  3. Connect to your Raspberry Pi via SSH (the first boot may take a while, so be patient) ssh [email protected] (enter your previously configured password)

  4. Install ffmpeg apt-get install ffmpeg

  5. Install v4l2loopback apt-get install v4l2loopback-dkms

  • Enable v4l2loopback module at boot by creating a file /etc/modules-load.d/v4l2loopback.conf with the content
v4l2loopback
  • Specify which loopback file should be created by the module (in our case /dev/video99) by creating the file /etc/modprobe.d/v4l2loopback.conf with the content
options v4l2loopback video_nr=99
  • Restart the Raspberry Pi and verify that the file /dev/video99 exists
  1. Install hkcam
wget https://github.com/brutella/hkcam/releases/download/v0.1.0/hkcam-v0.1.0_linux_arm.tar.gz
  • Extract the archive with tar -xzf hkcam-v0.1.0_linux_arm.tar.gz
  • Run hkcam by executing the following command
./hkcam -data_dir=/var/lib/hkcam/data -multi_stream=true -port=8080 -verbose
  1. Add the camera to HomeKit
  • Launch the Apple Home-app and tap + → Add Accessory

  • Tap More Options...

More options

  • Select Camera and confirm that the accessory is uncertified

Select Accessory

  • Enter the pin 001-02-003 and Continue

Select Accessory

If everything works as expected, you have to configure hkcam as a daemon – so that hkcam is automatically run after boot. This can be done in different way – systemd is recommended,

How to install with Ansible?

I've made an Ansible playbook which configures your Raspberry Pi and installs hkcam. The following steps require ansible to be installed. On macOS you can install it via Homebrew.

brew install ansible

First install Raspberry Pi OS, as described above. Then create ssh key and copy them to the Raspberry Pi.

ssh-keygen
ssh-copy-id [email protected]

After that you can execute the playbook with the following command.

cd ansible && ansible-playbook rpi.yml -i hosts

Once the command finishes, your camera can be added to HomeKit.

Multistream

Normally in HomeKit a camera stream can only be viewed by one device at a time. If a second device wants to view the stream, the Apple Home app shows

Camera Not Available Wait until someone else in this home stops viewing this camera and try again.

hkcam allows multiple devices to view the same stream by setting the option -multi_stream=true. That's neat.

Persistent Snapshots

In addition to video streaming, hkcam supports Persistent Snapshots. Persistent Snapshots is a way to take snapshots of the camera and store them on disk. You can then access them via HomeKit.

Persistent Snapshots are currently supported by Home+, as you can see from the following screenshots.

Live streaming

Snapshots

Taking snapshots in automations is also supported.

Automation

Web Interface

When running hkcam at a specific port (by specifying -port=...), you can access the web interface at the url http://{ip-address}:{port} with a browser. The web interface shows the recent snapshot and lets you install updates.

Web Interface

Raspberry Pi Zero W

I do get kernel panics when running hkcam with an ELP 1080P USB camera. Updating /boot/config.txt with the following changes resolve those kernel panics.

arm_freq=800
arm_freq_max=900
arm_freq_min=700

Raspberry Pi Zero W Enclosure

Desk mount

Wall mount

I've also designed an enclosure for the Raspberry Pi Zero W and standard camera module. You can use a stand to put the camera on a desk, or combine it with brackets of the Articulating Raspberry Pi Camera Mount to mount it on a wall. The 3D-printed parts are available as STL files here.

This enclosure is not waterproof and should not be used outside. Instead, you should use an ELP 1080P camera and connect it via USB to a Raspberry Pi.

Contact

Matthias Hochgatterer

Website: http://hochgatterer.me

Github: https://github.com/brutella

Twitter: https://twitter.com/brutella

License

hkcam is available under the Apache License 2.0 license. See the LICENSE file for more info.

More Repositories

1

hc

hc is a lightweight framework to develop HomeKit accessories in Go.
Go
1,741
star
2

chatheads

An implementation of Facebook's ChatHeads on iOS.
Objective-C
883
star
3

hap

The HomeKit Accessory Protocol (hap) implemented in Go
Go
289
star
4

can

Connect to a CAN bus in Go
Go
198
star
5

dnssd

This library implements Multicast DNS (mDNS) and DNS-Based Service Discovery (DNS-SD) for Zero Configuration Networking in Go.
Go
174
star
6

hklifx

LIFX HomeKit bridge
Go
106
star
7

hkknx-public

hkknx is a HomeKit KNX bridge for KNX.
89
star
8

swift-csv

Fast and memory-efficient CSV library in Swift.
Swift
80
star
9

hklight

Example project of a HomeKit light bulb using HomeControl
Go
39
star
10

canopen

Communicate with other CANopen nodes in Go
Go
26
star
11

TextViewLineNumbers

Implementation of NSTextView with line numbers
Swift
25
star
12

simplediff-swift

simplediff is a simple diff algorithm implementation in Swift.
Swift
16
star
13

nsuserdefaults-macros

Some handy NSUserDefaults macros/functions
Objective-C
15
star
14

NSTreePopUpButton

An NSPopUpButton subclass which supports binding to NSTreeController
Swift
15
star
15

uvr

Communicate with an UVR1611 over the CAN bus
Go
10
star
16

hkuvr1611

UVR1611 HomeKit bridge
Go
9
star
17

log-swift

Logging to console and file
Swift
9
star
18

Axt

A forgiving HTML SAX Parser for iOS
Objective-C
9
star
19

gouvr

Library to decode the data bus of UVR 1611 and similar devices
Go
7
star
20

hkuvr

HomeKit bridge for UVR1611
Go
5
star
21

hksymo

HomeKit bridge for Fronius Symo inverter
Go
3
star
22

Journal

Easy file logging in Swift
Swift
3
star
23

XCollectionData

Swift
2
star
24

comment-generator

Ruby classes to generate comment templates from source code
Ruby
2
star
25

git-diff

Compare git branches, tags and commits in your favourite diff tool.
Shell
1
star
26

C3Presentation

Slides about C++ programming built with deck.js and deckem
JavaScript
1
star
27

geizhals

Fetch prices from Geizhals
Go
1
star