Raspberry Pi Nestbox Camera – Setting up the Motion Detection Software

The previous part of this guide got our Raspberry Pi up and running without the need for a separate keyboard, mouse or monitor. We updated the Pi to make sure we had the latest software installed. Our next step is to set up the motion detection software for our Rapsberry Pi nestbox camera.

Set the correct timezone

One vital aspect of recording footage of wildlife is knowing when the action took place. All good recording software has some way of time-stamping the footage so we can look back and see when it was recorded. To make sure our software is going to display the correct time, we need to ensure the time on our Raspberry Pi is correct.

sudo cp /usr/share/zoneinfo/Europe/London /etc/localtime

My timezone is Europe/London but yours might be different. This should set the timezone properly.

Installing motion detection software

To constantly record footage from the nestbox camera would quickly fill up the space on our storage drive so rather than have it recording all the time, we only want to activate recording when movement is detected. Fortunately, there is a great piece of software called motion freely available that suits our needs well. Let's install it on our Raspberry Pi.

sudo apt-get install motion

We also need to install a couple of other packages:

sudo apt-get install libv4l-0 sudo apt-get install uvccapture

Ok, we've got all the required software on our Pi now. For my particular set up, I want to be able to power up my Pi and have it start the motion detection without having to do anything; I'm not going to have a monitor or keyboard attached or even a ethernet cable to connect headlessly. To allow this we have to allow motion to start when we boot up the Pi. For this, and the next step, we're going to edit some text files using the command line - don't worry, it's really simple. Raspbian comes with a text editor called nano which works just like text edit on Windows but you have to use the keyboard. Let's give it a shot:

sudo nano /etc/default/motion

All this command does is open a file using nano (the sudo part just means we're doing it with administrator privileges). You should see something like this: Raspberry Pi Nano screenshot

Move along and replace 'no' with 'yes' like in the image above. To save press Ctrl+O, then Enter. Next press Ctrl+X to exit nano. Great, next we need to do something similar to the motion configuration file:

sudo nano /etc/motion/motion.conf

This is a much longer file with many more options. Take your time and look through them. The majority can be left as they are but there are a couple of important ones:

  • Daemon: set this to on
  • Width: I have mine set to 640 but this depends on what your webcam is capable of displaying
  • Height: Again, settings will vary but I use 480
  • Framerate: I use 2, the lowest available setting. The idea here is not to record too much since this will fill up the storage quickly and if I'm using battery power (more on that later) I want to keep the power consumption as low as possible. If you're plugged into the mains, try 10 to begin with.
  • Minimum Frame Time: 3 - again to do with power consumption. If you're plugged into the mains 0 is probably fine.
  • Quality: 90 - I'm not trying to record HD video, just some stills of wildlife so the quality doesn't need to be 100%, this will also save some space on the storage device.
  • Webcam Port: 8080 you can use this to view your camera like a webcam as long as it's connected via WiFi or ethernet cable. It's also useful to use to test the camera.
  • Webcam Localhost: off - This means you can access your camera from outwith your home network although you might have to use some port forwarding which is outside the scope of this guide.
  • Control Port: 8081 - something other than 8080 really.
  • Target Dir: This is where the saved footage will be stored. Let's set this to something easy and memorable just now: /home/pi/Desktop/footage. We'll revisit this later.

Again, Ctrl+O, then Enter then Ctrl+X to exit. Now we need to make the folder we just specified in the configuration file and allow motion to write images to it (give it permission to save the footage in the folder). First we make the directory:

cd Desktop mkdir footage sudo chmod 777 footage

All we did there was change directory to the Desktop, make a directory (folder) called footage change the permissions to 777 (allowing motion to write the files to the directory). The chmod command is a bit more cryptic! Ok, it's a good time to reboot our Raspberry Pi to make sure all the settings have taken place like the time zones etc.

sudo reboot

Use your browser to see a live feed from your webcam

Give your Raspberry Pi a few minutes to reboot. If you're using the Logitech C210 and have set everything up properly, after your Pi has booted up, the light on the webcam should come on. This means the daemon flag we changed to 'on' is doing it's job and you should be able to navigate to the IP address of your Pi, in the guide's case: 192.168.1.87 add on :8080  (the port we specified to show the webcam output) to get 192.168.1.87:8080 and type that into the address bar of your browser (note: Chrome and Internet Explorer don't seem to like this, but Firefox plays nicely). If you're unlucky, you might see this: Motion screengrab on webcam But hopefully you'll be able to see what your webcam is pointing at. When deployed, you'll only be able to check out your live feed if your raspberry pi is connected to your network (either with wifi or an ethernet cable). Congratulations, it looks like things are working. If, however, after a few seconds, the light on your webcam goes out, or you're not able to view your camera's feed from your browser, there's a problem. A good starting point to see what might have gone wrong would be to check the system log;

tail -f /var/log/syslog

Wait! What about all the images we're supposed to be saving. Yep, they're being saved too. The next part of this guide will show you how to access them from the directory we created earlier but for now, let's just check they're being written ok.

cd ~/Desktop/footage ls

We've changed directory to the footage folder we create and listed any items within. If everything is working, you should see some files with names consisting of numbers and dashes. These names can be modified in the motion.conf file to your liking in your own time. Let's move on to the next part of the guide where we can look at viewing these images properly by storing them on a removable drive.