Create your own and free video surveillance system
Create your own and free video surveillance system

This video surveillance system is based on this requirements:

  • hardware
    • a raspberry pi
    • a network camera with MJPEG streaming
  • software
    • raspbian OS
    • motion
    • mutt
    • msmtp

I will show you in this tutorial how to create your own video surveillance with these features :

  • motion detection
  • send alerts and images by email
  • save locally pictures
  • create a daily timelapse video
OS/Packages installation

First install the raspbian OS on your raspberry pi : procedure (download image)

Remove unnecessary X11 libs : apt-get remove –auto-remove –purge libx11-.*

Install required packages : apt-get install motion msmtp ca-certificates mutt

Configure msmtp/mutt to send email using Gmail with TLS

Create/edit the configuration file /etc/msmtprc

tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default
port 587
auth login
password yourpassword
logfile /var/log/msmtp.log

Create/edit the configuration file /etc/Muttrc (important : the file is Muttrc and not muttrc) :

set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="Camera"
set envelope_from=yes

Test/try to send an email :
echo “Body message” | mutt -a /etc/hosts -s “Test email” —

Create and set rights on the folder where the images/videos will be saved :

mkdir /home/pi/capture
chmod 777 /home/pi/capture

Create the script that will send emails /home/pi/scripts/ :
temp=`/opt/vc/bin/vcgencmd measure_temp`
echo “Internal $temp – Uptime : $( uptime )” | mutt -a “$2” -s “Motion detected : $1” —

Create the script for the image/video retention /home/pi/scripts/ :

find /home/pi/capture -type f -mtime 7 -exec rm {} \;
Configure motion

Edit the file /etc/default/motion

# set to 'yes' to enable the motion daemon

Edit the file /etc/motion/motion.conf (backup the original first):

daemon on
setup_mode off
norm 0
frequency 0
rotate 0
width 1280
height 720
framerate 25
auto_brightness off
brightness 0
contrast 0
saturation 0
hue 0
roundrobin_frames 1
roundrobin_skip 1
switchfilter off
threshold 4500
threshold_tune off
noise_level 32
noise_tune on
despeckle EedDl
smart_mask_speed 5
lightswitch 8
minimum_motion_frames 2
pre_capture 1
post_capture 1
gap 60
max_mpeg_time 60
output_all off
output_normal best
output_motion off
quality 100
ppm off
ffmpeg_cap_new on
ffmpeg_cap_motion off
ffmpeg_timelapse 1
ffmpeg_timelapse_mode daily
ffmpeg_bps 1000000
ffmpeg_variable_bitrate 0
ffmpeg_video_codec mpeg4
ffmpeg_deinterlace off
snapshot_interval 0
locate preview
text_right %Y-%m-%d\n%T-%q
text_changes off
text_event %Y%m%d%H%M%S
text_double off
target_dir /home/pi/capture
snapshot_filename %v-%Y%m%d%H%M%S-snapshot
jpeg_filename %v-%Y%m%d%H%M%S-%q
movie_filename %v-%Y%m%d%H%M%S
timelapse_filename %Y%m%d-timelapse
webcam_port 0
webcam_quality 50
webcam_motion off
webcam_maxrate 1
webcam_localhost on
webcam_limit 0
control_port 0
control_localhost off
control_html_output on
track_type 0
track_auto off
track_motorx -1
track_motory -1
track_maxx 0
track_maxy 0
track_iomojo_id 0
track_step_angle_x 10
track_step_angle_y 10
track_move_wait 10
track_speed 255
track_stepsize 40
quiet on
on_picture_save /home/pi/scripts/ "%Y-%m-%d %T" "%f"
sql_log_image off
sql_log_snapshot off
sql_log_mpeg off
sql_log_timelapse off
sql_query insert into security(camera, filename, frame, file_type, time_stamp, event_time_stamp) values('%t', '%f', '%q', '%n', '%Y-%m-%d %T', '%C')
Configure cron

Create the script that will monitor/start/stop the motion process /home/pi/scripts/ :

ProcessInstances=`sudo ps aux | grep /usr/bin/'[m]'otion | wc -l`
if [ $ProcessInstances -eq 0 ];
        echo Process not running! Starting process
        sudo /etc/init.d/motion start
        echo Process already running! Nothing to do.

Configure the cron jobs :

> crontab -e
*/5 * * * * /home/pi/scripts/ 2>&1
0 12 * * * /home/pi/scripts/ 2>&1

Just wait the process launch and that’s all… If some steps are missing do not hesitate to leave a comment and I will update the proc.




My Powershell script categories

Create your own and free video surveillance system

Leave a Reply

Your email address will not be published.