Raspberry Pi Security Webcam with Motion Detection and Email

We’ve been wanting to put together a how-to for a Motion Detection Raspberry Pi Security Webcam for some time, and suddenly it appears that the time is now. So let’s get to it.

How to set up a Raspberry Pi as a Motion Detection Security Webcam

Preface: Back in the heady days of 2011-2012, back when the Raspberry Pi was a new thing, life a little bit more complicated in terms of installing the OS to an SD card and configuring all of the software you need to get your webcam behaving like a motion detection security camera. In those days, the inexperienced Linux user had to jump through a few more hoops to install the Raspberry Pi OS (Raspbian at the time) image on the SD card by a combination of fdisk and Win32DiskImager. Well, no more since basic Raspberry Pi setup has evolved to become much easier for the average user.

1. Install Raspberry Pi OS

Similar to how Unity now has it’s own application for managing versions, Raspberry Pi OS now has the Raspberry Pi Imager which makes it easy to install a basic Raspberry Pi OS or you can choose to install RetroPie as we’ve done previously. Simply install Raspberry Pi OS to your SD card like so:

a) Download Raspberry Pi Imager and install it.
b) Click Choose OS. There a lot of options but I decided to chose Raspberry Pi OS (Other) and then selected Raspberry Pi OS Lite since I will not need the GUI.
c) Click Choose Storage and point to where you SD card is. The Imager should select your card by default but double check that you have indeed selected the correct drive.
d) If you’re 100% sure about the previous selections, click Write.

Next, simply insert your SD card into your Raspberry Pi and power it on. Note that additional OS config is out of scope for this tutorial but if everything looks good and you can log in, you should be ready for the next steps.

Note: Someone asked me about this endless message that never seems to complete after 1st boot with the Pi:

Started Daily apt upgrade and clean activities.

Before moving on to Step 2, you may want to disable that and opt for manual updates/upgrades. You can disable daily apt updates/upgrades like so:

sudo systemctl disable apt-daily-upgrade.timer

2. Install fswebcam and Motion

Once your Raspberry Pi is up and running, it’s time to install some webcam and motion detection software.  Motion is the software that we’ll use to detect movement and capture images/video along with fswebcam. Install both of them with this command:

sudo apt-get install -y fswebcam motion

When the above has completed, test that the camera is able to capture images.

3. Testing webcam with fswebcam

You can easily test with fswebcam by running this simple command:

pi@raspberrypi:~ $ fswebcam test_image.jpg

--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 384x288 to 352x288.
--- Capturing frame...
Captured frame in 0.00 seconds.
--- Processing captured image...
Writing JPEG image to 'test_image.jpg'. 

You should have a similar output and, after noting that the video device is at /dev/video0, you can view the image to confirm that the test was satisfactory.

If you want to run further tests with a different resolutions, you can try something like the following:
fswebcam -r 1280x720 test_image.jpg

4. Install postfix and related mail utilities

Next we’ll install postfix, mailutils and mpack so that your Pi can email images of events to you. You can also use sendmail but for this tutorial we will be working with postfix. Run the following commands to install these utilities:

sudo apt-get install postfix
sudo apt-get install mailutils
sudo apt-get install mpack

5. Configure Motion to detect movement, i.e. turn your USB webcam into a motion detector

First off, we want to ensure that motion runs in daemon mode and and that the video device is properly set.

a) Edit /etc/motion/motion.conf and ensure that the following is set to ensure that your camera will be set to detect movement by default:

daemon on
videodevice /dev/video0
<– this value may vary depending on your results in Step 3

Set the following values to your preferences. We used the following and chose to just save jpegs and turn movies off.

width 640
height 480
framerate 50
quality 75
picture_type jpeg
ffmpeg_output_movies off

Assuming you want to email each capture to yourself and assuming that you installed the mail utilities in Step 4, you will also want to configure this key:

on_picture_save echo 'webcam alert' | mail -A %f -s "Webcam Alert" your.email@test.com

Note that after making any changes in motion.conf, you should restart the service:

sudo service motion restart

Of course Motion has a TON of other options including configuration for a live streaming server under the “Live Stream Server” section. We won’t cover this today but the options are very self explanatory.

6. Configuring Motion with Postfix

Postfix configuration

Postfix generally works out of the box but you should consider adding a relay host so that you can send mail from your Pi. As well, I specified ipv4 for inet_protocols so that it would not attempy to connect via ipv6:
relayhost = relay.isp.example
inet_protocols = ipv4

Final Notes

And that is pretty much all you need to do for setting up the basic components to have a functional Raspberry Pi Security Camera with Motion detection and Email. You can continue to tweak and add improvements to your Raspberry Pi security cam as you go along. At a bare minimum, I like to include a cronjob to delete images older than 1 week so as not to completely fill up the root partition.

I edit the crontab with a cronjob that runs every Monday at 1am and deletes files older than 7 days in /var/lib/motion:

0 1 * * 1 find /var/lib/motion -type f -mtime +7 -exec rm {} \;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.