Send email when webcam detects movement

In my previous post I’ve explained how to configure a DIY IP camera using motion.

Now let configure an email alert system that send email upon motion detection and the video recorded. It’s very simple.

First of all, install some software:

sudo apt-get update
sudo apt-get install sendemail libio-socket-ssl-perl libnet-ssleay-perl

If you want to use GMAIL as smtp you need to fix SSL config. Open /usr/share/perl5/IO/Socket/SSL.pm and then change

m{^(!?)(?:(SSL(?:v2|v3|v23|v2/3))|(TLSv1[12]?))$}i

to:

m{^(!?)(?:(SSL(?:v2|v3|v23|v2/3))|(TLSv1[12]?))}i

Now change motion.conf file.

# email sent when an event starts
on_event_start sendEmail -f YOURFROMEMAIL@gmail.com -t YOURTOEMAIL@gmail.com -u "[Motion] Event detected" -m "Movement has been detected on: %d %m %Y. The time of the movement was: %H:%M (Hour:Minute). Video will follow" -s smtp.gmail.com -xu YOURGMAIL@gmail.com -xp YOURGMAILPASSWORD -o tls=yes

# send video as attachment
on_movie_end sendEmail -f YOURFROMEMAIL@gmail.com -t YOURTOEMAIL@gmail.com -u "[Motion] video" -m "Video" -s smtp.gmail.com -xu YOURGMAIL@gmail.com -xp YOURGMAILPASSWORD -o tls=yes -a %f
 

DIY IP camera with Raspberry Pi

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
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

 

Rsync : daily backup

This is a simple reminder.
I’m doing a lot of changes in my NAS and my “domotic” RaspBerry (One of these days I’m going to write down something about the Raspberry project) and I need a simple backup solution for a running linux machine.

It’s very simple, use this script and add to /etc/cron.daily

rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} --delete /* /path/to/backup/folder