This a tutorial about how to setup a Raspberry Pi Zero W to display in Homekit. This tutorial is not complete. It doesn't show how to set up homebridge in example.
- Raspberry Pi Zero W
- SD card (with a minimum of 4GB)
- Raspberry Pi Zero W Adapter
- Raspberry Pi Zero W Camera Cable
- Raspberry Pi Camera Board v2
- Raspberry Pi Camera Enclosure (i.e. You can 3D print Raspberry Pi Enclosure by @brutella from the hkcam project)
- Download the latest Raspbian Lite image from: https://www.raspberrypi.org/downloads/raspbian/
- Download and install the latest Etcher flash tool from: https://www.balena.io/etcher/
- Flash Raspbian Lite with Etcher
- Create an empty ssh file on the root of the sd card
$ touch ssh
- Create an file called: wpa_supplicant.conf with content as below, change SSID name + PASSWORD
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="<<SSID>>"
psk="<<PASSWORD>>"
}
- Detect it's ip address (default assigned by dhcp)
$ nmap -sn 10.0.100.1-100
ssh pi@<pi_ip_address>
- Set passed for root
$ sudo passwd root
- Make it possible to login via ssh for root
$ sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
- Restart ssh
$ sudo /etc/init.d/ssh reload
- Login as root
$ ssh root@<ipadres>
- Change username and make home directory
$ usermod -l <username> -d /home/<username> -m pi
- Change name in the group to username
$ groupmod -n <username> pi
- Generate new SSH Key (anser defaults with enter)
$ ssh-keygen
- Add your public keys to .ssh/authorized_keys file
$ echo '<public key>' > ~/.ssh/authorized_keys
-
Test your key, exit the connection and log back in. There should be no prompt for password!
-
Disable root login and password based login
$ sudo sed -i 's/PermitRootLogin yes/#PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
$ sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
$ sudo sed -i 's/UsePAM yes/usePAM no/' /etc/ssh/sshd_config
$ sudo sed -i 's/PrintMotd no/PrintMotd yes/' /etc/ssh/sshd_config
$ sudo /etc/init.d/ssh reload
$ sudo apt-get update && sudo apt-get -y upgrade && sudo apt auto-clean && sudo rpi-update
sudo reboot now
$ sudo raspi-config
$ Network Configuration
- change hostname (i.e camera-1)
- WIFI SSID + WW
$ Localisation options:
- change locale: en_GB.UTF-8 UTF-8 and en_US.UTF-8 UTF-8
- locale default: none
- change timezone: Europe/Amsterdam
$ Interfacing options:
- enable camera: yes
sudo reboot now
$ cd ~
$ raspistill -o cam.jpg
- On your local computer:
$ scp pi@raspberry.local:~/cam.jpg
- Check if the image contains the image you expect, where the camera is pointed at.
$ sudo apt-get install cmake libjpeg8-dev git gcc g++
$ cd ~
$ git clone https://github.com/jacksonliam/mjpg-streamer.git
$ cd mjpg-streamer/mjpg-streamer-experimental
$ make
$ sudo make install
$ export LD_LIBRARY_PATH=.
$ ./mjpg_streamer -o "output_http.so -w ./www" -i "input_raspicam.so"
- http://<>:8080
- If you see the MJPG Streamer welcome page, it works!
$ nano /home/<username>/mjpg-streamer.sh
- Copy this text in the mjpg-streamer.sh file
#!/bin/bash
# chmod +x mjpg-streamer.sh
# Crontab: @reboot /home/pi/mjpg-streamer/mjpg-streamer.sh start
# Crontab: @reboot /home/pi/mjpg-streamer/mjpg-streamer-experimental/mjpg-streamer.sh start
MJPG_STREAMER_BIN="/usr/local/bin/mjpg_streamer" # "$(dirname $0)/mjpg_streamer"
MJPG_STREAMER_WWW="/usr/local/share/mjpg-streamer/www"
MJPG_STREAMER_LOG_FILE="${0%.*}.log" # "$(dirname $0)/mjpg-streamer.log"
RUNNING_CHECK_INTERVAL="2" # how often to check to make sure the server is running (in seconds)
HANGING_CHECK_INTERVAL="3" # how often to check to make sure the server is not hanging (in seconds)
VIDEO_DEV="/dev/video0"
FRAME_RATE="30"
QUALITY="80"
RESOLUTION="1280x720" # 160x120 176x144 320x240 352x288 424x240 432x240 640x360 640x480 800x448 800x600 960x544 1280x720 1920x1080 (QVGA, VGA, SVGA, WXGA) # lsusb -s 001:006 -v | egrep "Width|Height" # https://www.textfixer.com/tools/alphabetical-order.php # v4l2-ctl --list-formats-ext # Show Supported Video Formates
PORT="8080"
YUV="yes"
################INPUT_OPTIONS="-r ${RESOLUTION} -d ${VIDEO_DEV} -f ${FRAME_RATE} -q ${QUALITY} -pl 60hz"
INPUT_OPTIONS="-r ${RESOLUTION} -d ${VIDEO_DEV} -q ${QUALITY} -pl 60hz --every_frame 2" # Limit Framerate with "--every_frame ", ( mjpg_streamer --input "input_uvc.so --help" )
if [ "${YUV}" == "true" ]; then
INPUT_OPTIONS+=" -y"
fi
OUTPUT_OPTIONS="-p ${PORT} -w ${MJPG_STREAMER_WWW}"
# ==========================================================
function running() {
if ps aux | grep ${MJPG_STREAMER_BIN} | grep ${VIDEO_DEV} >/dev/null 2>&1; then
return 0
else
return 1
fi
}
function start() {
if running; then
echo "[$VIDEO_DEV] already started"
return 1
fi
export LD_LIBRARY_PATH="$(dirname $MJPG_STREAMER_BIN):."
echo "Starting: [$VIDEO_DEV] ${MJPG_STREAMER_BIN} -i \"input_uvc.so ${INPUT_OPTIONS}\" -o \"output_http.so ${OUTPUT_OPTIONS}\""
${MJPG_STREAMER_BIN} -i "input_uvc.so ${INPUT_OPTIONS}" -o "output_http.so ${OUTPUT_OPTIONS}" >> ${MJPG_STREAMER_LOG_FILE} 2>&1 &
sleep 1
if running; then
if [ "$1" != "nocheck" ]; then
check_running & > /dev/null 2>&1 # start the running checking task
check_hanging & > /dev/null 2>&1 # start the hanging checking task
fi
echo "[$VIDEO_DEV] started"
return 0
else
echo "[$VIDEO_DEV] failed to start"
return 1
fi
}
function stop() {
if ! running; then
echo "[$VIDEO_DEV] not running"
return 1
fi
own_pid=$$
if [ "$1" != "nocheck" ]; then
# stop the script running check task
ps aux | grep $0 | grep start | tr -s ' ' | cut -d ' ' -f 2 | grep -v ${own_pid} | xargs -r kill
sleep 0.5
fi
# stop the server
ps aux | grep ${MJPG_STREAMER_BIN} | grep ${VIDEO_DEV} | tr -s ' ' | cut -d ' ' -f 2 | grep -v ${own_pid} | xargs -r kill
echo "[$VIDEO_DEV] stopped"
return 0
}
function check_running() {
echo "[$VIDEO_DEV] starting running check task" >> ${MJPG_STREAMER_LOG_FILE}
while true; do
sleep ${RUNNING_CHECK_INTERVAL}
if ! running; then
echo "[$VIDEO_DEV] server stopped, starting" >> ${MJPG_STREAMER_LOG_FILE}
start nocheck
fi
done
}
function check_hanging() {
echo "[$VIDEO_DEV] starting hanging check task" >> ${MJPG_STREAMER_LOG_FILE}
while true; do
sleep ${HANGING_CHECK_INTERVAL}
# treat the "error grabbing frames" case
if tail -n2 ${MJPG_STREAMER_LOG_FILE} | grep -i "error grabbing frames" > /dev/null; then
echo "[$VIDEO_DEV] server is hanging, killing" >> ${MJPG_STREAMER_LOG_FILE}
stop nocheck
fi
done
}
function help() {
echo "Usage: $0 [start|stop|restart|status]"
return 0
}
if [ "$1" == "start" ]; then
start && exit 0 || exit -1
elif [ "$1" == "stop" ]; then
stop && exit 0 || exit -1
elif [ "$1" == "restart" ]; then
stop && sleep 1
start && exit 0 || exit -1
elif [ "$1" == "status" ]; then
if running; then
echo "[$VIDEO_DEV] running"
exit 0
else
echo "[$VIDEO_DEV] stopped"
exit 1
fi
else
help
fi
- Make the file executable
$ chmod +x mjpg-streamer.sh
- Edit conjob configuration
$ crontab -e
- Add line for reboot
$ @reboot /home/pi/mjpg-streamer.sh start
- Install plugin homebridge-camera-ffmpeg
- Click on 'plugin'
- Search for homebridge-camera-ffmpeg
- Click on ‘install’
- Add to homebridge config:
"platforms": [
{
"name": "Camera ffmpeg",
"cameras": [
{
"name": "Default Camera or <other name>" ,
"videoConfig": {
"source": "-re -f mjpeg -i http://<ip-or-dns-of-your-camera>:8080/?action=stream",
"stillImageSource": "-f mjpeg -i http://<ip-or-dns-of-your-camera>:8080/?action=snapshot",
"maxStreams": 2,
"maxWidth": 1280,
"maxHeight": 720,
"maxFPS": 30,
"debug": false,
"audio": false,
"motion": false
}
}
]
}
],
- Click on ‘+’
- Add accessory
- Select your device in your the overview
- Enter pin manually
- Pin is known in your homebridge instance
- https://www.raspberrypi.org/documentation/installation/sd-cards.md
- https://www.raspberrypi.org/documentation/usage/camera/README.md
- https://www.raspberrypi.org/documentation/usage/camera/raspicam/raspistill.md
- https://www.raspberrypi.org/documentation/hardware/camera/
- https://github.com/KhaosT/homebridge-camera-ffmpeg/wiki/Raspberry-PI
- https://github.com/jacksonliam/mjpg-streamer
- https://github.com/oznu/docker-homebridge#parameters
- https://github.com/cncjs/cncjs/wiki/Setup-Guide:-Raspberry-Pi-%7C-MJPEG-Streamer-Install-&-Setup-&-FFMpeg-Recording