Skip to content
/ pircam Public

Motion detection sensor triggered capture and combination of video and audio from D-Link network cameras and Raspberry Pi Camera

Notifications You must be signed in to change notification settings

nykfank/pircam

Repository files navigation

The motion detection capabilities of D-Link DCS-936L and DCS-2670 network cameras in outdoor settings are nice in principle, but fog, snow or wind cause almost permanent false positives. PIR motion sensors solve this issue by only detecing motion of warm objects. Sensors from Tinkerforge were used, because they relay motion detection signals by TCP/IP. 
Recording video by RTSP, triggered by the PIR motion detector, has a delay of at least 3 seconds before recording starts on both cameras. So it is not very useful because whatever moved may already have left by then. 
The solution is to use MJPEG recording, which starts almost immediately. 
Audio is streamed by RTSP separately. The delay for RTSP audio is not as noticable as for video, because it is inherent to the h264 implementation of these cameras. 

This python program runs as a daemon to performs motion detections sensor triggered capture and combination of MJPEG video and audio from D-Link network cameras, as well as notification by the xmpp instant messaging protocol. 
It is tested with ejabberd running on the same machine. 
It combines and converts MJPEG video and AAC audio to the ogg vorbis/theora format using ffmpeg and uploads it to a separate webserver by rsync.

Additionally, it acts as a TCP server which processes messages from Raspberry Pi units taking videos running 
the pipir.py program and then downloads the videos to integrate them with those from the other camera.

The length of the video is specified by the rec_sec variable. For audio the desired length can be specified by RTSP. 
But the MJPEG stream has to be downloaded by size, so an initial guess is made.
After the number of downloaded frames are counted, the MJPEG download size is corrected to result in the length specified by rec_sec.
It is possible to have multiple motion sensors activating each camera and you can combine version 1 with version 2 motion detectors.

A simple web-interface based on photoswipe (pircam.html) is updated with the latest events using the file pircam.js.
It enables browsing though the still images by swiping on a mobile device or using the keyboard or mouse on a PC.
Clicking on the shopping-cart links to the video. 

Options in the config file:

GLOBAL
logfile     = Path to file where log will be written to.
stop_file   = If this file exists, the daemon stops.
pause_file  = If this file exists, no recording is done, but motion still logged.
data_dir    = Base directory for downloaded video and audio.
webRoot     = Path on webserver to upload logfile and pircam.js and where content of photoswipe directory is
brick_port  = Default tinkerforge port (4223)
webserver   = .ssh/config name for webserver
rec_sec     = Seconds to record.
max_queue   = Maximum queue size before converting while recording.
sleep_sec   = Seconds to sleep in mainloop
reconf_sec  = Seconds before sensor status is checked
xmpp_to     = XMPP account to send notification to
xmpp_from   = XMPP sender account 
xmpp_pass   = XMPP sender password
xmpp_server = XMPP server
scriptPath  = Path to photoswipe_script.txt used for the web-interface.
size_x      = Width in pixels of captured video/images
size_y      = Height in pixels of captured video/images
numLast     = Number of last images (motion events) to be included in the web-interface.

CAMERA
camera_identifier
mjpeg = HTTP URL for mjpeg stream can be found in the web-interface of the network camra. 
For example http://IP/video/mjpg.cgi for DCS-936L or http://IP/video1.mjpeg for DCS-2670. Disabling authentication is only possible for the DCS-936L, otherwise use http://user:password@IP.
audio = RTSP URL for AAC audio stream can also be found in the web-interface. None for no audio.
For example rtsp://IP/play1.sdp for DCS-936L or rtsp://IP/live2.sdp for DCS-2670.
read  = Byte to read from MJPEG stream. Will be adapted by the program to result in 20 seconds of video.
fps   =	Framerate in frames per seconds as defined in the network camera web-interface for the selected stream.
sun   = HH:MM, HH:MM, ... minutes to ignore sensor because of sun - plant_moving_in_wind - sensor alignment

SENSOR
sensor_uid	
cam   = camera_identifier
ip    = brick IP
vers  = Motion Detector Bricklet Version 1 or 2
sensi = Sensitivity for motion detection (only version 2)

--- End of config file description section.


Dependencies
On debian install the following packages:
# apt install ffmpeg ejabberd python-xmpp python-pip
# pip install tinkerforge

Webserver
# apt install lighttpd certbot
- Symlink output directories (containing the produced videos) into webroot
- Copy contents of photoswipe directory to webroot
- Use lighttpd_example.conf to only serve https and create /etc/lighttpd/lighttpdpassword with one username:password per line
- pircam will upload logfile and pircam.js to webroot, so pircam.html will display these images/videos.

Lets Encrypt Certificate
# certbot certonly
- Enter email address, hostname of webserver and use the standalone webserver method 
- Create daily cronjob:
# certbot renew
# cat /etc/letsencrypt/live/example.org/cert.pem /etc/letsencrypt/live/example.org/privkey.pem >/etc/letsencrypt/live/example.org/web.pem
# service lighttpd restart (needed or will it reload the cert at some point anyway?)

Recommended Android XMPP client: Conversations from F-Droid, or the slightly improved fork blabber.im

About

Motion detection sensor triggered capture and combination of video and audio from D-Link network cameras and Raspberry Pi Camera

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published