Raspberry Pi – Surveillance WebCam in the Cloud

Here is my first project using the Raspberry Pi: Surveillance Camera connected to the cloud.
First of all, a short: Why ?
– Why Surveillance Camera: Well it’s fun to be in vacation and to check how the garden looks like, if a friend should be called and sent to wet the flowers, aso …
– Why Web Cam: Together with a Pi it’s flexible and very cheap compared with other solutions
– Why Cloud: I want to see my garden when I’m NOT at home. And I don’t want to open ports in my own firewall. Hence, if I cannot come IN to the content, I obviously have to put the content OUT.

Some other constraints I had to consider:
– The Web Cam will be placed about 10-15 m away from my Pi
– The cable to the Cam will have to go through some walls (physical- not fire-), so the smaller the holes I’ll have to do in the walls, the more peaceful will remain my family-life 😉
– I must avoid writing too many content on the SD Card; It would be a pitty to break it due to high number of Write operations.
– I considered being as “slim” as possible with using the Pi ressources. This meant to me scripting in Bash rather than other scripting languages.
– I somehow had to upload to the cloud: Best case: via some robust SDK API, hopefully implementing very efficiently the communication to the cloud. So here I had to give up the Bash and consider for example Java.

– Raspberry PI Model B
Logitech C525; I tried also the C270, but I got too many errors.
LogiLink USB 2.0 Hub 4-Port
– A LogiLink Extension Cable USB over CAT5/6 (something like: HDE USB over Cat5/5e/6 Extension Cable RJ45 Adapter Set)
– 15 m CAT 6 LAN cable

See below for a script calling fswebcam to take pics every n seconds. It creates several folders, some of them in PI’s RAM to avoid too many write accesses to the SD card. it also sets limits for the created folders, to make sure neither the created RAM folder, nor the SD card (used as backup, if the RAM folder is filled in) will explode.
For the upload to a cloud service, stay tuned for another post. 🙂

The core of the script is: get1Pic.sh

License: Please notice the license governing the source code below in the “License & About” page.

# This contains the configurable variables
source /home/pi/dev/getPic/params.sh

if [ ! -e "$dir" ]; then
echo "Create Folder:"$dir
mkdir "$dir"

df $dir | grep tmpfs
if [[ $? -ne 0 ]] ; then #the folder is already tmpfs
echo "mount the tmpfs folder"
sudo mount -t tmpfs -o size=$ramDiskSize,mode=0755,uid=pi,gid=pi tmpfs "$dir"

echo "start script" > $logFile

test -d "$picFolder" || mkdir -p "$picFolder"

test -d "$targetFolder" || mkdir -p "$targetFolder"

test -d "$backupDir" || mkdir -p "$backupDir"


while [ $counter -lt 1000 ]
let counter=counter+1
#echo $counter
if [ $counter -gt $zipSize ]; then
zipPattern=$(date +"%Y%m%d_%H%M%S")
zip -q -0 -j $zipName $files
mv $zipName $targetFolder"/."
rm $files
#ok, we zipped everything, we deleted the originals, now let's see how big the
#target folder is, not that we start to swap, as this does not make
#any fun
targetFolderSize=`du -s -b "$dir" | cut -f 1`
if [ $targetFolderSize -gt $backupTreshold ]; then
noOfFiles=`find "$targetFolder" -type f | wc -l`
noFiles2Move=$(($noOfFiles/3)) #one third
#echo "we move: "$noFiles2Move" out of: "$noOfFiles"files"
#we move 1/3 of the zips from RAM to the SD
for i in `seq $noFiles2Move`
mv $targetFolder"/"`ls $targetFolder -t |tail -1` $backupDir
backupFolderSize=`du -s -b "$backupDir" | cut -f 1`
if [ $backupFolderSize -gt $sdTreshold ]; then
#ok, so the sd card explodes as well. let's kick some files
noOfBackFiles=`find "$backupDir" -type f | wc -l`
noFiles2Remove=$(($noOfBackFiles/3)) #one third
#echo "we remove: "$noFiles2Remove
for j in `seq $noFiles2Remove`
#echo "we remove:"$backupDir`ls $backupDir -t |tail -1`
rm $backupDir`ls $backupDir -t |tail -1`

filePattern=$(date +"%Y%m%d_%H%M%S")

displayTime=$(date +"%Y%m%d_%H%M%S")
echo -n $displayTime" : " >> $logFile
cmd=$(fswebcam -c $paramPath -q --save $fileName.jpeg >> $logFile 2>&1)
sleep $sleepTime
exit 1

Next there is: params.sh setting the parameters used above
# This contains the configurable variables
backupTreshold=8000000 #in bytes

And of course the: /home/pi/dev/getPic/capt
resolution 640x480
skip 3
palette MJPEG
set "White Balance Temperature, Auto"="True"
set "Power Line Frequency"="50 Hz"
#set "Backlight Compensation"=0
#set "Sharpness"=100
#fps 20
#delay 1
jpeg 95

What’s next:
– Cloud client (Dropbox). Btw, if you have no Dropbox yet, but you want an account, start by visiting this link. I could use some free GB in my account 😉
– How to start the script in background and how to stop it
– … I have a couple of other ideas. If I get them done, I’ll post.
– And certainly, last but not least, putting the webCam in a case out in the garden. I’ll put some pics of this, too, once I’m ready.

Final considerations here:
– Why didn’t I use motion: I felt it’s too complex for my case. As long as I need ONLY pictures (movies would not be suitable for the “cloud”), I felt I could achieve better results if I a simpler program to take pics, and I script everything around. Once I’m ready with my ideas around this project, I might consider motion, too.
– Logitech C270 had problems when connected over the extension cable and the 15 m CAT6, as the extension cable is only USB 1.1; I therefore had to switch off it’s audio; I entered: blacklist snd_usb_audio in: /etc/modprobe.d/raspi-blacklist.conf. And it remained so even if I’m now using C525. I however do not need audio.

And btw, for comments and discussion, let’s use the thread @ Raspberry PI Forum.

Note: You may want to also check: Raspberry Pi – Surveillance WebCam in the Cloud – Improvements for details on how did I improve the stability for my “Surveillance WebCam in the Cloud”.

Tagged with: , , ,
Posted in Raspberry PI
%d bloggers like this: