DIY security system - remote cameras

In a previous article, we set up a basic DIY security system using motion and a webcam connected to a Raspberry Pi. That's a good start, but let's not stop there. If we have our one camera watching the front door, what about the back door? The garage? Or maybe, we need to catch the thief that's stealing our left over pizza from the refrigerator in the middle of the night! Um, that last scenario is purely hypothetical of course!

We need more cameras!

While it is possible to connect another USB camera to our already configured Pi, that is not feasible for all the places we need other cameras. Fortunately, motion supports streaming cameras. No need to buy a special purpose streaming camera (although you can if you prefer), we'll create our remote stations using additional Raspberry Pis and regular USB webcams.

Materials

For this project you will need at least:

  • Your original Raspberry Pi with motion configured.
  • An additional Raspberry Pi (with a microSD card and power supply) for each remote station
  • An additional webcam for each remote station - again I used the Logitech C310 for my stations.

We'll be doing work on both the original motion Pi setup and the new remote Pi. For the purposes of this article, I will call the original motion Pi, the base station Pi and the remote camera Pi, the remote station Pi. My IPs for each are 192.168.0.5, and 192.168.0.6 respectively. Your IPs may be different, so you should substitute your IPs where appropriate.

Setting up the Pi

The first step is to install Raspbian Lite on your remote station Pi. You can do that on your own, or follow my instructions here.

Installing the streaming package

For streaming video from the remote station to your base station Pi, we'll use mjpgstreamer. Sadly, this program is not in the official debian/raspbian repositories. You can build it yourself on the Pi by following these instructions on github (build the debian package). Alternatively, you can download pre-built packages below. I have releases for both jessie and stretch. (Run cat /etc/os-release | grep VERSION= on your Pi if you're not sure which you need.)

OS SHA-256
stretch 0ce00ad655636fc45c3528cf0dc29029d4fccbd8067157a3f646825c4e9e641d
jessie de17fb1d48cd1096e6dd94739a7f157a1855696e2e1674367edcefc774233c6f

If you build the package, copy it to your remote station Pi from your computer as follows before proceeding:

scp mjpg-streamer_2.0_armhf.deb [email protected]:/home/pi/

If you are downloading the package, you can download to your Pi directly with wget.

ssh [email protected]
wget https://github.com/carpie/mjpg-streamer/releases/download/2.0-ddd97f0/mjpg-streamer_2.0-ddd97f0-stretch_armhf.deb

Once you have the package on your remote station Pi, install it:

sudo dpkg -i mjpg-streamer_2.0-ddd97f0-stretch_armhf.deb

Test the webcam

Let's make sure the webcam is recognized. Plug in your webcam in to your remote station Pi 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.

Testing mjpgstreamer

Let's see if it's working. On your remote station Pi, run:

sudo systemctl start mjpg_streamer@video0

Now open a browser on your computer and browse to http://192.168.0.6:8080.

You should see something like the following:

mjpgstreamer Test Page

Congratulations! Your remote station is working!

Configuring motion to capture data from the remote station

Now we need configure motion on our base station to include our remote station stream. So ssh into your base station Pi:

ssh [email protected]

Edit /etc/motion/motion.conf and add the following line at the bottom (below the configuration we did in the previous article):

camera /etc/motion/camera2.conf

This tells motion that we have another camera and to read its configuration from camera2.conf. Create or edit /etc/motion/camera2.conf as follows:

# /etc/motion/camera2.conf
netcam_url http://192.168.0.6:8080/?action=stream
text_left PIZZA CAM

The netcam_url line specifies the web address of our streaming camera. The text_left lines says to overlay our video with the text PIZZA CAM. Obviously, you may change the text to whatever you want. Now restart motion with the new configuration:

sudo systemctl restart motion

Assuming there were no errors in the configuration, 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-05-16 21: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

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

Now move around in front of the remote 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. You will see that outputs have the camera number in the names so you know which camera the motion was captured on. If that is not enough organization for you, you can specify the target_dir option in each camera configuration file to place that camera's output in a specific directory. Alternatively, you can specify the snapshot_filename, picture_filename, and movie_filename options in each camera configuration file to add an explicit prefix or post-fix to the output file naming scheme.

Making the remote station autonomous

So that's pretty cool, but we need to a little more to make the remote station autonomous. As it is right now, we need to login and start the mjpg_steamer service for it to work. It's a remote camera right? We should not have to do anything manually. Let's fix it.

On your remote station Pi, enter the following:

sudo systemctl enable mjpg_streamer@video0

That's it! mjpgstreamer will now run on startup. Also, the systemd service is set up so that if mjpgstreamer ever crashes on us, it will restart within three seconds. So, we can simply plug up our remote station when we want to use it and not worry about it. Nice!

Conclusion

Congratulations! You now have a working motion detection system with a remote camera! The process can be repeated for any number of remote stations you want to add. Next time we'll protect our motion captures from loss (by theft or other means) by uploading them to the cloud!