This page covers setting up a simple multicamera home security system with
two features:
- saving video material to disk,
- live streaming through a web browser.
The system consists of multiple Raspberry Pi devices connected to a common
local network.
It’s recommended but not required to make a separate home network for this
purpose.
One device called the hub receives video streams and produces seekable HLS
streams and at the same time saves video segments to disk.
Other devices called camera devices transmit video streams from a camera
to the hub device.
The idea is that the hub device is installed inside and camera devices are
mounted outside in electrical boxes with Ethernet and PoE.
I have a system like this deployed with 2 camera devices and 1 hub device.
It’s working well.
Camera devices
Repeat the following steps for every camera device.
Hardware
Parts:
- a Raspberry Pi board, any generation, model B (with Ethernet)
- a microSD memory card
- mini radiators (optional)
- a Raspberry Pi Camera Module (possibly NoIR) with ribbon
- a RPi Camera mount (or standard ball mount and custom rig for the camera)
- M5 standoffs and screws
- a passive PoE injector/switch
- a 5V-3A-capable step-down converter board (e.g. LM2596)
- a bit of stranded copper wire with a 1x2 goldpin receptacle
- a fuse holder and a fuse (current rating depends on PoE voltage)
- an outdoor-rated RJ-45 receptacle
- a lot of network cable and plugs (cat. 5 unshielded is probably enough)
- an electrical box
Tools:
- drill
- small phillips head screwdriver
- small flathead screwdriver
- soldering iron and solder
- multimeter
- needle-nose pliers
- RJ-45 terminator
- tools for mounting the elecrical box in desired place
Wiring: TODO
Software
We will use the firmware interface for communication with the Raspberry Pi
camera.
It’s because the new blob-free libcamera interface is still experimental and I
wasn’t able to get it to work on Alpine Linux.
Download files with firmware blobs that contain camera drivers and codecs.
But first, to edit OS files, the partition needs to be remounted read-write.
mount /media/mmcblk0p1 -o rw,remount
apk add curl
curl 'https://raw.githubusercontent.com/raspberrypi/firmware/master/boot/start_x.elf' > /media/mmcblk0p1/start_x.elf
curl 'https://raw.githubusercontent.com/raspberrypi/firmware/master/boot/fixup_x.dat' > /media/mmcblk0p1/fixup_x.dat
Instruct to load firmware to the VideoCore GPU before boot.
echo "start_file=start_x.elf" >> /media/mmcblk0p1/config.txt
echo "fixup_file=fixup_x.dat" >> /media/mmcblk0p1/config.txt
If your camera is already connected, just reboot
.
Of not, poweroff
, disconnect power, connect camera and connect power.
Verification
After reboot, verify if you are able to obtain images from the camera.
apk add raspberrypi
/opt/vc/bin/raspivid -t 0
You should be able to see a video feed via Pi’s HDMI.
Raspivid service
We will use raspivid’s tcp output to send live video to the camera hub device.
Create /etc/init.d/raspivid
with the following contents.
#!/sbin/openrc-run
name=raspivid
command="/opt/vc/bin/$name"
command_args="$raspivid_opts"
command_user="raspivid:raspivid"
supervisor=supervise-daemon
respawn_delay=1
respawn_max=0
depend() {
need net
after firewall
}
start_pre() {
[ -n "$output_log" ] && checkpath -f "$output_log" \
-m 644 -o raspivid:raspivid
[ -n "$error_log" ] && checkpath -f "$error_log" \
-m 644 -o raspivid:raspivid
}
Make it executable and add this file to LBU.
chmod +x /etc/init.d/raspivid
lbu add /etc/init.d/raspivid
Create /etc/conf.d/raspivid
with the following contents.
These parameters can be tuned.
Set the --output
to the address of your hub device and adjust the
--annotate
option to reflect the name of the camera.
The H264 encoder that raspivid uses is limited to 1920x1080 px resolution.
In order to use higher resolutions one needs to use MJPEG instead.
raspivid_opts=" \
--nopreview \
--output tcp://192.168.1.10:8083 \
--timeout 0 \
--flush \
--codec H264 \
--rotation 180 \
--mode 4 \
--annotate $(( 1024 | 8 | 4 )) \
--annotate '%Y-%m-%d %X ROAD' \
--bitrate 0 \
--width 1640 \
--height 1232 \
--qp 25 \
--framerate 15 \
"
output_log=/var/log/raspivid.log
error_log=/var/log/raspivid.log
Add required raspivid user and add it to video
group.
addgroup -S raspivid
adduser -SDh/dev/null -s/sbin/nologin -Graspivid -graspivid raspivid raspivid
adduser raspivid video
Start the service, enable starting it on boot and save changes.
rc-service raspivid start
rc-update add raspivid
lbu commit -d
Hub devices
Firewall
Allow traffic on the following ports in the firewall configuration:
- TCP: 8081, 8082, 8083, … (for camera devices)
- TCP: 139, 445; UDP: 137, 138 (for samba)
- TCP: 80 (for http)
FFmpeg service
Create /etc/init.d/homesec
with mode 755
:
#!/sbin/openrc-run
name=homesec
command="/usr/bin/ffmpeg"
command_args="$ffmpeg_opts"
command_user="homesec:homesec"
supervisor=supervise-daemon
respawn_delay=1
respawn_max=0
depend() {
need net
after firewall
}
start_pre() {
[ -n "$output_log" ] && checkpath -f "$output_log" \
-m 644 -o homesec:homesec
[ -n "$error_log" ] && checkpath -f "$error_log" \
-m 644 -o homesec:homesec
}
Create user homesec
and group homesec
.
For each camera create /etc/conf.d/homesec-cameraname
:
ffmpeg_opts="-loglevel warning \
-r 15 \
-i tcp://192.168.1.10:8082?listen \
-c copy \
-f segment \
-segment_time 00:15:00 \
-segment_atclocktime 1 \
-strftime 1 \
/media/hdd/recordings/%Y-%m-%dT%H:%M:%S_cameraname.h264 \
-c copy \
-hls_time 1 \
-hls_list_size 900 \
/var/www/homesec/cameraname.m3u8 \
"
output_log=/var/log/homesec-cameraname.log
error_log=/var/log/homesec-cameraname.log
Web streaming
apk add nginx
rc-service nginx start
rc-update add nginx
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/homesec/;
location / {}
}
<!DOCTYPE html>
<link href="video-js.min.css" rel="stylesheet">
<script src="video.min.js"></script>
<style>
html, body {
height: 100%;
margin: 0;
}
video, .video-js {
width: 100%;
height: 50%;
}
</style>
<video class="video-js" controls autoplay muted data-setup='{"liveui": true}'>
<source src="camera1name.m3u8" type="application/x-mpegURL">
<p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
</video>
<video class="video-js" controls autoplay muted data-setup='{"liveui": true}'>
<source src="camera2name.m3u8" type="application/x-mpegURL">
<p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
</video>
Samba
[homesec]
path = /mnt/homesec/
apk add samba-server samba
TODO