DIY security system - the basics
securitymotionraspberrypi2018-03-29 - carpie

I have always liked the idea of having a security system, but never really liked the idea of paying monthly fees or the idea of unscrupulous people in control of cameras in my house. The solution?

Roll my own of course!

In the wonderful world of open source, it's quite easy and inexpensive to do so! In the article, we'll stick with the basics and learn how to set up motion, an open-source motion detection program, with a single simple USB webcam. We'll use the inexpensive but wonderful Raspberry Pi as our controller. In future articles, we'll add remote cameras and upload our captures to the cloud.

Disclaimer: The system we're creating here is not a "proper" security system! Specifically:

  • It has no guarantee of effectiveness or reliability.
  • The hardware and software were not specifically designed for such a purpose.
  • Any risk you take in using the system as a security system is totally your own.
  • You shouldn't use this system to protect anything without understanding the risk.

Materials

For this project you will need at least:

  • A Raspberry Pi (with a microSD card and power supply)
  • A webcam for your Pi - for this article I used the Logitech C310.

Setting up the Pi

The first step is to install Raspbian Lite on your Pi. You can do that on your own, or follow my instructions here. Once that is done, ssh into your Pi and install the open source motion detection software, motion.

(My Pi's address is 192.168.0.5. If yours is different, substitute your address where ever I use mine.)

ssh pi@192.168.0.5

sudo apt install motion

Plug up the webcam

Let's make sure the webcam is recognized. Plug in your webcam and type the following command:

lsusb

You should get a list of USB devices, one of which should be similar to the following:

Bus 001 Device 004: ID 046d:081b Logitech, Inc. Webcam C310
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

If you don't, your webcam may not be compatible. Check the compatibility list and replace your webcam if necessary.

Configuring motion

Now that the webcam is recognized, let's configure motion. The main configuration file is /etc/motion/motion.conf. We will edit this file for some of our settings, but since in the future we are going to add multiple cameras, we will go ahead and use a separate configuration file for our one camera.

Edit (with sudo) /etc/motion/motion.conf and add a block at the bottom.

##############################################################
# My settings
##############################################################
framerate 15
output_pictures best
target_dir /opt/motion
snapshot_filename %Y-%m-%d__%H-%M-%S__%t-%v-snapshot
picture_filename %Y-%m-%d__%H-%M-%S__%t-%v
movie_filename %Y-%m-%d__%H-%M-%S__%t-%v

camera /etc/motion/camera1.conf

Let's run through the settings. First we set the frame rate up to 15 frames per second. That's a personal preference based on my experience with motion and my specific webcam. You may experiment and configure as desired. Next we tell motion to only output images with the "most" motion detected in them. This prevents a ton of image files from being dumped to our file system. Next we set the target directory for motion captures to /opt/motion and configure the names of the files that will be captured. These settings are again personal preferences and you may use the defaults if you desire.

Finally, we tell motion that we want to use a separate configuration file for our camera. This is not strictly necessary but will be handy when we add additional cameras in a future article.

Note: I'm using motion version 4 in this article. Older versions of motion use the term thread instead of camera for including additional camera configuration files, but it works the same way.

Create or edit /etc/motion/camera1.conf as follows:

# /etc/motion/camera1.conf
videodevice /dev/video0
text_left OFFICE CAM
width 640
height 480

The videodevice line specifies the first USB webcam as our desired camera. The text_left lines says to overlay our video with the text OFFICE CAM. Obviously, you may change the text to whatever you want. The width parameter sets the width of the captured images in pixels. Likewise, the height parameter sets the height of the captured images in pixels. Obviously, these need to be set to a size your camera supports.

We told motion to write our output to /opt/motion but that does not yet exist. Also, motion needs permission to write there. By default motion runs as the user motion so let's create the directory and set its owner to motion.

sudo mkdir /opt/motion
sudo chown motion /opt/motion

There is one more thing we need to change before we can try this out. Edit /etc/default/motion and set start_motion_daemon to yes. By default motion will attempt to start when your Pi boots. The parameter in /etc/default/motion is simply a "switch" for this behavior. We're ready for Pi to run, so we flip the switch on by setting the parameter to yes.

Trying it out

Now start motion with:

sudo systemctl restart motion

Assuming there were no errors in the configuration and the webcam is working, motion should now be running. You can check the status by entering:

sudo systemctl status motion

You should see something like:

● motion.service - LSB: Start Motion detection
   Loaded: loaded (/etc/init.d/motion; generated; vendor preset: enabled)
   Active: active (running) since Sun 2018-02-11 15:43:55 EST; 12s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 17934 ExecStop=/etc/init.d/motion stop (code=exited, status=0/SUCCESS)
  Process: 17979 ExecStart=/etc/init.d/motion start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/motion.service
           └─17985 /usr/bin/motion

Feb 11 15:43:55 raspberrypi systemd[1]: Starting LSB: Start Motion detection...
Feb 11 15:43:55 raspberrypi motion[17979]: Starting motion detection daemon: motion.
Feb 11 15:43:55 raspberrypi systemd[1]: Started LSB: Start Motion detection.
Feb 11 15:43:55 raspberrypi motion[17985]: [0:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /et
Feb 11 15:43:55 raspberrypi motion[17985]: [0:motion] [NTC] [ALL] config_camera: Processing camera config file /et
Feb 11 15:43:55 raspberrypi motion[17985]: [0:motion] [NTC] [ALL] motion_startup: Motion 4.0 Started
Feb 11 15:43:55 raspberrypi motion[17985]: [0:motion] [NTC] [ALL] motion_startup: Logging to file (/var/log/motion
lines 1-16/16 (END)

Also, the light on your camera should turn on after a few seconds (if your camera has one). Now move around in front of the camera and check your output in /opt/motion. You should see .jpg files there containing snapshots of the detected motion and .avi files containing short movies of the captured motion.

pi@raspberrypi:~ $ ls /opt/motion
2018-02-11__15-44-20__1-01.avi  2018-02-11__15-46-54__1-02.avi
2018-02-11__15-44-21__1-01.jpg  2018-02-11__15-46-55__1-02.jpg

Congratulations! You now have a working motion detection system!

Turning it off

To stop motion simply enter:

sudo systemctl stop motion

But remember, it'll start itself back up automatically on the next boot! If you want to prevent this, change start_motion_daemon to no in /etc/default/motion.

Conclusion

In a few simple steps, we have an inexpensive motion-detecting security camera setup that is firmly under our control. Next time we'll expand our DIY security system by adding remote cameras!