I found an old webcam somewhere in a drawer and I decided to plug the webcam to my Raspberry IP in order to build a video-surveillance system with auto motion detection.
The Hardware is simple:
- Raspberry PI (with sdcard and power supply)
- Microsoft LifeCam VX-500
Contents
Install motion
Motion is an open source software that can collect images and video from a webcam when a movement is detected. It has also an embedded web server that can share the livestream.
sudo apt-get update sudo apt-get install motion
Setup motion
You also need to enable the motion daemon so that motion will always run:
sudo nano /etc/default/motion
and change the line to:
start_motion_daemon=yes
Set also the right image resolution (in my example, 640×480) and if you want change also these settings:
pre_capture 2 post_capture 2 ffmpeg_video_codec mpeg4
- pre_capture: how many frames record before the movement
- post_capture: how many frames record after movement stops
- ffmpeg_video_codec: video codec (default flv)
Web server (optional)
If you want to share your image over the web you can also change the config for “Live Webcam Server” and “HTTP Based Control”. The live stream page is not protected by username-password, so BE CAREFUL!!!
Another thing: the live stream page does not work on Chrome. In order to see the stream you have to embed the “http://host:port/” address in an HTML page (using IMG tag)
Set target dir
Raspberry PI has a very small storage on SD card. It’s better to use a network folder as destination for images and video. Configure the mount point in /etc/fstab
//YOURSERVERNAME/YOURSHAREDFOLDERNAME /media/camshare cifs username=YOURSHAREDFOLDERUSERNAME,password=YOURSHAREDFOLDERPASSWORD,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0
and change motion config file in order to use the new mounted network folder
target_dir /media/camshare
Schedule Webcam capture time
If you want to schedule motion start and stop, you can do it easily with crontab (as root)
crontab –e
For example, if you want to start motion at 11AM (from monday to friday) until 5PM:
00 11 * * 1-5 /usr/bin/motion 00 17 * * 1-5 killall motion
Don’t use “service motion start|stop” or “/etc/init.d/motion start|stop” as cron command because….doesn’t work. I don’t know why, maybe some weird environment variables configuration