Raspberry PiZero Home Security Camera



Posted: 2022-12-22

Last weekend I found my old webcam lying in the back of a draw. It captures only 640x480 video and for some reason no longer works in colour. I have also been staring at my Raspberry PiZero2 sitting on my desk wondering what shall I do with it.

Well finally inspiration hit! If I simply grab my old Micro-USB to USBA OTG adaptor I once bought for a cellphone I could very easily plug the USB webcam directly into the PiZero and have a video feed. No need to buy the PiCam for example.

The Script

The next step was pretty simple I needed a script that could spin the camera up, take a single .jpg image and then sleep till I wanted it to take another picture. I generally find myself writing bash scripts when it comes to quick and easy scripts like this. So I fired up vscodium and got scripting.

Keeping it super simple this is how it turned out:

#!/bin/bash
username="$USER"

output_location=/home/$username/imagecapture
mkdir -p $output_location

while true
do
    stored_date=$(date +"%Y-%m-%d-%H-%M")
    ffmpeg -y -f video4linux2 -i /dev/video0 -frames:v 1 $output_location/$stored_date-image.jpg
    curl -T $output_location/$stored_date-image.jpg -u "ftpusername:ftppassword" ftp://ftp.server.com
    sleep 1m
done

This was then saved to a file called capture_script.sh in /home/username and simply call it at every reboot via the crontab by opening the Terminal and running crontab -e as the user not as sudo.

Just add the line @reboot /home/username/capture_script.sh & to the bottom of the crontab and save the file.

Thats it, plug in the webcam and the power source and sit back whilst the webcam shoots a new image every minute and uploads it to your FTP for safe keeping. In my case the files are only 12kb per image, so it doesn't use a lot of server space.

Next Steps

I am considering using syncthing to sync the files over to my home network storage. I may still keep the pi uploading a single file to the web, just so if I am away from the house I can remotely see the current activity.

If I had more free time I would probably try playing with OpenCV to run some computer vision on the files or video stream and detect movement to then send me a notification.

Lets see how it goes for now though! ;)