Refreshing the Pi Security Camera. Now with more Zeros!

newVold

[All the code used in this project is linked at the bottom of this article]

YouTube Video

[Sneaky pre-publishing edit]
I’ve changed the core setup to the camera.  I’ve played around with having the camera unit run Resilio Sync as compared to the SimpleHTTPServer/scrubber setup.  For now it appears that running Resilio Sync on the camera is more reliable than the duct tape and bubble gum method and doesn’t appear to have any performance cost.  Using Resilio Sync cuts the cost of this project in half(or more).  To run Resilio Sync on start up just add:

@reboot [path to resilio sync]/rslsync –webui.listen 0.0.0.0:8888

to your crontab.  I’m leaving the old instructions in because I think it’s still neat information.
[/Sneaky pre-publishing edit]

At just a few days over 3 years ago I made a security camera out of a Raspberry Pi 2.  I’ve got a few Pi Zeros lying around (I seem to pick one up every time I go to Micro Center), so why not redo the camera with a zero?  I cut out the PIR motion sensor.  In my experiences with the old camera, the PIR motion sensor is the way to go.  It’s been very reliable, made for cleaner code, and caused less work on the Pi 2’s processor.  I cut it out because I wanted to use the Zero’s small size to my advantage.  To sense motion I have the Zero take and store two images in RAM and compare the difference in color values per pixel.  Once enough pixels have changed(I have mine set to 5%) it decides that it has detected motion and captures 15 seconds of video.

All of that is useless unless you can easily get to the captured videos.  I have the camera also run a SimpleHTTPServer and another Zero scrubs all the files off this server every minute. The Zero doing the scrubbing also runs Resilio Sync so that I have secure access to the video files created by the camera.

I had originally planned to do a code walk through, but in all honesty, I don’t understand the important bits enough for a walk though to be helpful.  I have commented the code well enough to make adjusting it for your use easy enough.  The important bits that aren’t included in the download or video are how to set up the crontabs on the Pis.

On the pi running the camera script: type “crontab -e”

edit crontab

If it asks you which editor use, pick one.  I use choice 2(Nano) because it’s easy and doesn’t require knowing too many keyboard shortcuts. Once in, add the following lines to the bottom:

@reboot [path to StartUp.sh]/startUp.sh
[your own time] * * * * [path to wificheck.sh]/wificheck.sh
[your own time] * * [your own days] [path to reboot.sh]/reboot.sh

I would like to note here that the parts with [your own time] and ‘*’ can be changed and are only denoting how often a script is being run.  I use crontab guru to help me figure out what I should put in for specific results.

Your startUp.sh should look something like this”

cd [path to where your running the scripts from]
nohup python -m SimpleHTTPServer 8000 &
nohup python3 motionCameraRetyped.py &

wificheck.sh:

#!/bin/bash
echo “checking network…”
if ! [ “$(ping -c 1 192.168.0.1)” ]; then
sudo ifdown wlan0
sudo ifup wlan0
fi

If your wifi router has a different IP address you will need to change “192.168.0.1” to match your router.

And reboot.sh just needs this simple line:

sudo shutdown now -r 5m

Once you have it all set up and the python scripts installed, your camera is good to go.

Now its time to set up the Pi Zero running Resilio Sync and the scrubber.  After you install Resilio Sync according to their instructions, your crontab should look like this:

@reboot [path to resilio sync]/rslsync –webui.listen 0.0.0.0:8888
* * * * * [path to script]/runScrub.sh

using the “–webui.listen 0.0.0.0.:8888” on the rslsync command lets you point a browser on your network to the pi’s IP address on port 8888 to set up the folders to be shared and it allows you to share them.

runScrub.sh:

cd [path to scrubber script]/
nohup python beautifulSoupScrubber.py &

That’s all the “hard” stuff. it’s not that hard though.  Just put the required scripts in the places you specified, set up all the variables you need to on the camera script and scrubber script, set up Resilio sync to share the files, and you’re done.

Word of warning real quick.  The ZeroCamLog.txt and scrubLog.txt files can grow in size forever.  I suggest ssh-ing into the pis once in a while and deleting those files.  The scripts will start new ones when you do that.  It’s saves space(a little) and makes it easier to read the logs.

Github with motionCameraRetyped.py and beautifulSoupScrubber.py

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s