Scheduled webcam motion detection surveillance with auto-email feature using Raspberry Pi (Arch Linux)

*Disclaimer: This post assumes that you’ve got Motion detection running on the Motion package using Arch Linux.*
UPDATED Oct 2, 2014: I’ve also included instructions on how to setup a surveillance using Motion package using Raspbian OS [here][10].

I started using the Raspberry Pi to learn Linux. About the same time, something was digging up dirt in our backyard in the middle of the night. I soon discovered that I could use the Pi to see what’s going on while we were sleeping! I stumbled on [this blog][1] on how to use the Pi with Arch Linux to setup a Webcam Monitoring system on a schedule. I only used the motion detection feature of Motion to save disk space.

Overtime, I expanded the project to include an auto-mailer feature with attachments of the captured jpegs. Depending on the surveillance’s activity, my motion target directory would sometimes grow to a few hundred MBs. One popular solution in my Google search is to use Google drive, Dropbox or some sort of server. But I wanted my recipients to be passive, so I decided to go with emailing my recipients with attachment(s) instead. Most mail providers have a [message limit][2] (including attachments) of about 20MBs. I created a script to split the target directory to manageable sizes if it grows to be bigger than my recipients’ email provider limit. If 3 directories are generated, 3 directories will be zipped, and 3 emails will be sent to the recipient.

I use [ssmtp][3] (automated email forwarding program) and [mutt][4] (text-based email client) to support my Pi’s auto-emailing feature. Here’s a step-by-step guide to help you setup ssmtp and mutt, and last but not the least, the bash script that automatically splits the files into smaller directories before zipping them up, and sending each zipped file to the recipient.

First, setup ssmtp:
——————-

Install the ssmtp package:

[root@alarmpi ~]# pacman -S ssmtp

After the installation is completed, edit the ssmtp.conf file as follow:

[root@alarmpi ~]# nano /etc/ssmtp/ssmtp.conf
#root=your_username@yahoo.com # Uncomment this line and comment the following if using yahoo
root=your_username@gmail.com

#mailhub=smtp.mail.yahoo.com:587 # Uncomment this line and comment the following if using yahoo
mailhub=smtp.gmail.com:587

#rewriteDomain=yahoo.com # Uncomment this line and comment the following if using yahoo
rewriteDomain=gmail.com

#hostname=your_username@yahoo.com # Uncomment this line and comment the following if using yahoo
hostname=username@gmail.com

# Use SSL/TLS before starting negotiation
UseTLS=Yes
UseSTARTTLS=Yes

# Username/Password
AuthUser=username # full gmail address or yahoo address
AuthPass=password # email password 

# Email 'From header's can override the default domain?
FromLineOverride=yes

Save your changes (for nano, press Ctrl-X, ‘Y’ and hit Enter), and change the permissions of the ssmtp.conf:

[root@alarmpi ~]# chmod 640 /etc/ssmtp/ssmtp.conf

Change the config file group to mail:

[root@alarmpi ~]# chown root:mail /etc/ssmtp/ssmtp.conf

Add user to mailgroup, in this case, I’m using root as my user:

[root@alarmpi ~]# gpasswd -a root mail

Then create/edit alias(es) for local user:

[root@alarmpi ~]# nano /etc/ssmtp/revaliases
#root:your_username@yahoo.com:smtp.mail.yahoo.com:587 # Uncomment this line and comment the following if using yahoo
root:your_username@gmail.com:smtp.gmail.com:587

Save your changes, and we’re ready to send a test mail with ssmtp!

[root@alarmpi ~]# echo test | mail -s "testing ssmtp app" recipient@domain.com

Most of what’s listed comes directly from [SSMTP-ArchWiki][3] and [here][5]. If you receive error(s), chances are you missed something above.

Setup mutt:
———–

First, install the package:

[root@alarmpi ~]# pacman -Sy mutt

Then make a local copy of Muttrc:

[root@alarmpi ~]# cp /etc/Muttrc ~/.muttrc

Edit the local muttrc file by changing the following variable settings (*tip: if using nano, use Ctrl-W to search for the lines*):

[root@alarmpi ~]# nano ~/.muttrc
set sendmail="/usr/sbin/ssmtp"

# replace with your email address, should be the same address as what you've used when setting ssmtp and mutt
set from="your_username@domain.com" 

set use_from=yes

Again, save the changes, and now we’re ready to send a test mail with attachment(s)!
Send test mail with attachment:

[root@alarmpi ~]# mutt -s "Test mail with attachment" recipient@domain.com -a /path/to/your/attachment.zip

These links also provide the ssmtp and mutt setup for [Verizon][6] and [Csoft][7], although I’ve not tested them.

Bash script to Auto-split, Zip directories, attach and send Email:
————————

First, create the script file. I call it zipandmail.sh:

[root@alarmpi ~]# nano /bin/zipandmail.sh

Copy and paste the following code to the newly created file:

 
#!/bin/bash
#Yahoo and Gmail only allow attachment size of 25MB. 
#For example, 800x608 @ 50% motion detection jpegs, each file is 33~43KB.
#Each folder should about 500 files so as not to exceed 20MB when zipped.
#Change the default 500 to a smaller value if your recipient's email provider's limit is smaller than 25MB
#splitsize=250 
splitsize=500

#Email recipient(s), separated by space
#recipient="foo@gmail.com" # for single recipient
recipient="foo@gmail.com foo@yahoo.com" # for multiple recipients

#Email subject and message 
subject="This is the subject heading of the mail"
message="This is the body of the mail"

#Motion target files path
targetfilepath="/data/webcam/motion"

#Motion target directory path
targetdirpath="/data/webcam"

####################################
echo "Running zipandmail.sh..." `date`
#count number of files in motion directory
numfiles=$( ls -l $targetfilepath | egrep -c '^-')

numfolders=`expr $numfiles / $splitsize`
lastfoldercount=`expr $numfiles % $splitsize`

echo $numfiles
#echo $numfolders
#echo $lastfoldercount

#Move all files from motion directory to newly created directories based on $numfolders
COUNTER=0
while [ $COUNTER -le $numfolders ]; do
  echo "Creating directory... " `date`
  destination="$targetfilepath$COUNTER"
  rm -rf $destination
  mkdir -p $destination
#move multiples of $splitsize files to new folder(s) except the remainder files (that's less than $splitsize)
  if [ "$COUNTER" -lt "$numfolders" ]; then
    for file in $(ls -p $targetfilepath | grep -v / | tail -$splitsize); do
      mv $targetfilepath/$file $destination
    done
#move remainder files in last new motion folder
  else
    for file in $(ls -p $targetfilepath | grep -v / | tail -$lastfoldercount); do
      mv $targetfilepath/$file $destination
    done
  fi
  
  echo "Creating zip file... " `date`
  pushd $targetdirpath
  zip -9 -r -q motion$COUNTER.zip ./motion$COUNTER/
  popd

  echo "Sending mail to recipient... " `date`
  echo "content of the email" | mutt -s "subject of the email" $recipient -a $targetfilepath$COUNTER.zip
  let COUNTER=COUNTER+1
done

If you followed [Liam’s][1] Motion target directory path (/data/webcam/motion), you should only need to change:

#Yahoo and Gmail only allow attachment size of 25MB. 
#For example, 800x608 @ 50% motion detection jpegs, each file is 33~43KB.
#Each folder should about 500 files so as not to exceed 20MB when zipped.
#Change the default 500 to a smaller value if your recipient's email provider's limit is smaller than 25MB
#splitsize=250 
splitsize=500

#Email recipient(s), separated by space
#recipient="foo@gmail.com" # for single recipient
recipient="foo@gmail.com foo@yahoo.com" # for multiple recipients

You may have to play around with **splitsize** depending on your Motion setup.

If you’re using different Motion target directory path, you will need to change:

#Yahoo and Gmail only allow attachment size of 25MB. 
#For example, 800x608 @ 50% motion detection jpegs, each file is 33~43KB.
#Each folder should about 500 files so as not to exceed 20MB when zipped.
#Change the default 500 to a smaller value if your recipient's email provider's limit is smaller than 25MB
#splitsize=250 
splitsize=500

#Email recipient(s), separated by space
#recipient="foo@gmail.com" # for single recipient
recipient="foo@gmail.com foo@yahoo.com" # for multiple recipients

#Motion target files path
targetfilepath="/data/webcam/motion"

#Motion target directory path
targetdirpath="/data/webcam"

The following settings are optional:

#Email subject and message 
subject="This is the subject heading of the mail"
message="This is the body of the mail"

After you’re done editing, save the script.

Before you can run the script, you’ll have to change the permission of the script first:

[root@alarmpi ~]# chmod 755 /bin/zipandmail.sh

Now, you can execute the script from any directory:

[root@alarmpi ~]# zipandmail.sh

You should see something like this if all goes well:

Running zipandmail.sh  Thu Dec 26 22:48:43 PST 2013
58
Creating directory...  Thu Dec 26 22:48:44 PST 2013
Creating zip file...  Thu Dec 26 22:48:45 PST 2013
/data/webcam ~
~
Sending mail to recipient...  Thu Dec 26 22:48:48 PST 2013
[root@alarmpi ~]# 

The above example result shows only one new directory and zip file created, and one email sent, since there were only 58 files in my Motion target directory. If 3 new directories are created, the process will show 3 iterations of the process above, which also means 3 emails will be sent to the recipient.

To view the result of the script, go to your Motion target directory, in my example:

[root@alarmpi ~]# ls -l /data/webcam/
total 996
drwxr-xr-x 2 root root  40960 Dec 27 23:47 motion
drwxr-xr-x 2 root root   4096 Dec 27 23:47 motion0
-rw-r--r-- 1 root root 970365 Dec 27 23:47 motion0.zip
[root@alarmpi ~]#

Setup crontab:
————–

I’ve also added zipandmail.sh to crontab (Linux’s native scheduler) after I stop the Motion program script. To set this up:

[root@alarmpi ~]# crontab -e 

Press ‘i’ key to insert text, and type:
(*tip: **Press ‘ESC’ first before you use any arrow keys** if you made a typo*)

15 20 * * * /bin/zipandmail.sh
# This sets crontab to run zipandmail.sh every day at 8:15 PM.

I found this [crontab link][8] to be very helpful.
(*tip: crontab uses [vim editor][9]. Here are the vim commands I used:

– **Edit: insert text before cursor**: Press ‘i’ key

– **Edit: insert text after cursor**: Press ‘a’ key

– **Delete current line**: Press ‘ESC’, and type ‘dd’ (without quotes)

– **Delete current character**: Press ‘ESC’, and press ‘x’ key

– **Arrow navigation keys**: Press ‘ESC’ first, then use the arrow keys

– **Save and quit**: Press ‘ESC’, and type ‘:wq’ (without quotes), and hit Enter.* )

To list your scheduled task(s) in crontab:

[root@alarmpi ~]# crontab -l 

 

00 20 * * * /bin/startmotion.sh 
00 08 * * * /bin/stopmotion.sh 
02 08 * * * /bin/zipandmail.sh 
30 10 * * * /bin/delmotion.sh
00 12 1 1-12 * root shutdown -r +5 

Happy coding!

[1]: http://experimentswithlinuxrelatedtech.blogspot.com/2014/04/arch-linux-raspberry-pi-monitoring.html
[2]: https://members.swcp.com/wiki/Maximum_Email_Message_Sizes
[3]: https://wiki.archlinux.org/index.php/SSMTP
[4]: https://wiki.archlinux.org/index.php/mutt
[5]: http://nekhbet.com/ssmtp_yahoo.shtml
[6]: http://nekhbet.com/ssmtp_verizon.shtml
[7]: http://nekhbet.com/ssmtp_csoft.shtml
[8]: http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/
[9]: http://www.fprintf.net/vimCheatSheet.html
[10]: http://codrspace.com/audiojava/setup-webcam-and-raspberry-pi-as-motion-detect-surveillance/

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s