Pi camera motion
Jonni Bidwell does his bit for the surveillance state by setting up motiontriggered recording on the Raspberry Pi. He’s got his beady eyes on you!
Jonni Bidwell does his bit for the surveillance state by setting up motiontriggered recording on the Raspberry Pi. He’s got his beady eyes on you!
There are all sorts of things you can do with a Pi and a camera – some of which we dare not mention, and some of which we’ve already covered. Back in LXF265, for example, we showed you how to make a Pi bodycam. We’ve also done stopmotion video (LXF249) and a neat Pi camera booth (LXF248). Way, way back in LXF195 we also made a motion-detecting camera, and we thought it’d be good to revisit this because it’s actually much simpler to set up now. One application for this project is filming wildlife in your garden. Depending on your local climate, you’ll want to waterproof your Pi beforehand and there are a number of external cases that can do just this. You’ll need to have some means of powering it too. We used a Pijuice HAT, which you can connect to an external USB battery or solar panel to top up.
There are a few options software wise, but we’re going to use the same motion software used by Kent Elchuk in our previous motion-detecting tutorial. This will connect to your home wireless network, and enable you to stream via a web browser. By default, the stream will be quite low quality (one Jpeg image per second, which will save battery power). When motion is detected (when a pre-determined number of pixels in the image, subject to some filtering, have changed) the camera will start recording until the motion stops and save a higherquality video file.
Choose your Pi
This will work on a Pi 2 or Pi Zero, at least at low resolutions, but we’d recommend a Pi 3 or 4. That way you get wireless for free (you can use motion offline or via a wired network though). We used a one of the original 5MP Pi Cameras, specifically the NO-IR 1.3, but you’ll achieve better results if you have a newer one. Motion works on other Linux distros, so you can use it if you have a network camera, or any V4L- or Uvccompatible camera, you can use it on your Linux box.
We’ll start with a vanilla Raspberry Pi OS Lite (formerly Raspbian Lite) install, which occupies around 1GB of an SD card. Bear in mind that video we’re going to save video on here too, so we’d recommend at least an 8GB SD card to start with. Connect your camera using the ribbon cable and make sure it’s sitting snugly in there. Even if you have a monitor and keyboard to get your Pi set up, we’d still recommend setting up SSH and wireless connectivity before doing anything else. It would be terribly annoying to have everything else working and then be let down by dodgy wireless signals, after all your efforts.
New versions of Raspberry Pi Linux disable the SSH server by default, but you can enable it by putting an empty file named ssh on the FAT32 partition (the one visible in Windows) on the SD card, where all the boot files are stored. To automatically connect to a wireless network on boot, create a wpa_supplicant.conf file in the same location, with contents of the form: ctrl_interface=dir=/var/run/wpa_supplicant
Group=netdev update_config=1 country=gb
network={ ssid="your network name/ssid” psk="your WPA/WPA2 security key” key_mgmt=wpa-psk }
Save the file, unmount the card, pop it into the Pi and boot it up. If your OS supports .local name look ups, and there are no other Pis on your network, you should be able to SSH to your Pi by opening a terminal and running $ ssh pi@raspberrypi.local
otherwise you can use Nmap, your router, or if you must, a monitor and keyboard, to locate your Pi by IP address and then use that after the @ in the above command. Either way, log in using the default raspberry password. You should update your Pi at this stage, with: $ sudo apt update $ sudo apt upgrade
If you’re using a HAT to provide power to your Pi, set that up now. Pijuice provides a handy Python script that shows you power levels and charging status. It also gives you the luxury of a power button so you can cleanly shut it down if network connectivity is lost. You’ll also want to activate the camera by running
$ sudo raspi-config
and visiting the Interfacing Options menu. Check that it is working by doing raspistill -o test.jpg . If the camera’s red light turns on, but you see an error message, the connection might be dodgy (or your camera might be broken). Turn it off, reseat the connection and try again in this case.
There’s an older version of Motion in the repos, but it’s straightforward enough to get the new version from Github (which supports more hardware), so let’s do that. Check the project page to see if a newer version than 4.3.1 is available, and if so change the digits in the command below. Grab the latest deb with:
$ wget https://github.com/motion-project/motion/ releases/download/release-4.3.1/pi_buster_ motion_4.3.1-1_armhf.deb
We could install the dependencies by hand (all 730MB of them), but it’s easier to use Gdebi:
$ sudo apt install gdebi-core
$ sudo gdebi pi_buster_motion_4.3.1-1_armhf.deb
Enable the Motion daemon by editing /etc/default/ motion and changing ‘no’ to ‘yes’. Motion has an extensive array of options (see them all at https:// motion-project.github.io/motion_config.html), but we only need to configure a few to get started. Edit the /etc/motion/motion.conf file and then set the following options: target_dir /var/lib/motion stream_localhost off
This saves data in Motion own directory, and enables us to connect from the stream from another device on our LAN. Now start Motion with:
$ sudo systemctl start motion
and check things are working by visiting http:// raspberrypi.local:8081 (or use your Pi’s IP address). You may need to tweak the rotation parameter in the configuration file and restart the service if the stream is upside down. The default motion-detecting settings should work well.
So if you now go and wave like a crazy person in front of your camera, then you should find a .mkv file has been saved in the /var/lib/motion directory. These are helpfully named by date, and it’s handy to browse these graphically by pressing Ctrl+l in your file manager and entering sftp://pi:raspberry@raspberrypi.local/var/lib/ motion (again use the IP address if .local addressing isn’t working for you). Hit F5 periodically to refresh this, then copy new files to your home folder and watch the recorded activities.
If your Pi has enough grunt to handle it and you want better quality recordings, look at boosting the framerate and movie_quality parameters.