Eye o'Spy

Email Notification

The core part of the project is actually ready. If all is configured correctly, the presence of an intruder will be detected automatically, and the recorded videos will be saved on the local drive (SD card). However, we may not get the access to our board in order to retrieve the files or simply, we’d like to know sooner. In order to transfer the files, I use Part II from this tutorial as it provides an easy way for future expansion and excellent explanation, except for scheduling the activity of Motion using crontab.

General Idea

The general idea of this approach is to “emulate” a mail box using the Raspberry and send an email to oneself using the attachment (recorded videos of the intruder). I will recall the instruction for the sake of completion, but it essentially comes from the link I stated above.

As you remember, the on_event_end line inside motion.conf file provides the “entry point” here (or exit rather… nevermind). By typping sudo zip_and_mail.sh there, we are going to execute the script, which will collect all the content of the INBOX directory, compress it and send it to us by email.

Customized Implementation

In my implementation, I have modified the script slightly in order to fit with my needs better. I do not use the startmotion.sh and stopmotion.sh scripts as instructed. Instead, I only use the zip_and_send.sh.

Following the link, I assume that you have installed the ssmtp and mutt packages as well as configured them just as stated therein. Now, our task is to complete the code of zip_and_send.sh.

Important! Gmail is constantly improving the security, so in order to actually make it work using Gmail, make sure that you lower the security level for the email account. Personally, I advice you to create a separate and dedicated account for this job.

The content of the zip_and_send.sh script in my implementation is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash

splitsize=500
recipient="EMAIL@GMAIL.COM"
message="Thief entered your living room."
INBOX="/home/USER/CAMERAPROJECT/INBOX"
PROG="/home/USER/CAMERAPROJECT"
OUTBOX="/home/USER/CAMPERAPROJECT/OUTBOX"

echo "Target file path: $INBOX"
echo "Running zip_and_send.sh..." `date`
numfiles=$( ls -l $INB0X | egrep -c '^-')

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

COUNTER=0
while [ $COUNTER -le $numfolders ]; do
    echo "Creating directory... " `date`
    DESTINATION="$INBOXCOUNTER"
    echo "Directory $DESTINATION created."
    sudo rm -rf $DESTINATION
    sudo mkdir -p $DESTINATION

    if [ "$COUNTER" -lt "$numfolders" ]; then
        for file in $(ls -p $INBOX | grep -v / | tail -$splitsize); do
            echo "File to be moved: $INBOX/$file to: $DESTINATION"
            sudo mv $INBOX/$file $DESTINATION
            done
    else
        for file in $(ls -p $INBOX | grep -v / | tail $lastfoldercount); do
            echo "File to be moved: $INBOX/$file to: $DESTINATION"
            sudo mv $INBOX/$file $DESTINATION
            done
    fi

    echo "Creating zip file... " `date`
    pushd $DESTINATION
    sudo zip -9 -r -q $OUTBOX/motion$COUNTER.zip $DESTINATION/*
    popd

    # sending email
    echo "Sending email to the recipient: $recipient" `date`
    echo $message | mutt -s "Catch the Thief!" $recipient -a
    $OUTBOX/motion$COUNTER.zip

    # cleaning the old files
    echo "Removing file: $OUTBOX/motion$COUNTER"
    sudo rm $OUTBOX/motion$COUNTER.zip
    sudo rm $DESTINATION/*
    sudo rmdir $DESTINATION
    echo "Removing $OUTBOX/motion$COUNTER"
    sudo rmdir $OUTBOX/motion$COUNTER

    let COUNTER=COUNTER+1
    done

Here, remember to create the INBOX and OUTBOX directories before executing this script. The zip_and_send.sh must also be executable (permissions!). If everything is done correctly, warning emails should start appearing in your mailbox as soon as some movement is detected.