For my BabySpy project, I need to detect motion with a Raspberry Pi Camera module. The previous posts handles the prototype hardware. This post will centre around the question, “How do I detect motion with my new PI Camera?”.
The hard way
With the search phrase “raspberry pi motion detection”, one can find numerous projects and posts about how motion detection is possible. Many posts are motivated by the exciting idea of understanding the basics of motion detection, mainly with the picamera project. For this project, motion detection is a tool and less of a learning objective. Therefore it is not the best path.
The lazy way
The other results point to the motion project. Motion is a program that takes multiple camera inputs and detects significate changes, e.q. Motion. It is highly configurable but overpowered, to be honest ;). I will use and explain only a tiny part of the project, but it will do the trick.
Installation
The installation of motion can be done in three different ways, the Linux standard trio.
- The Arch-user way means building from the source like cloning the git-repo, running automatic configuration, and compiling.
- The I want a specific version-user way: download the deb package from the releases via firefox or wget, install via your favourite package manager
- The I want the program-user way: this will be
sudo apt-get install motion
for most distros
I choose the last way because I don’t require the newest features and want the program. If you choose the first way, I recommend checking whether you need additional packages before starting motion the first time. Otherwise, weird errors may occur ;)
Configuration
As mentioned before, motion is highly configurable. All settings are within their documentation. The default configuration is located under /etc/default/motion.conf, I will point out every setting I’ve changed for SpiderBB.
Key | Description | SpiderBB value | default | |
:————- | :———- | :———– | :———– | a |
logfile | Define the location of motions log file | ./log/motion.log | stderr | |
stream_port | Defines the port where an http stream publish life images | 8090 | 0 e.q. off | |
stream_motion | Increase pictures per second if motion detected | on | off | |
stream_maxrate | Frame rate while if motion is detected | 15 | 1 | |
stream_localhost | Restrict stream access to localhost | off | on | |
stream_auth_method | 0 = disabled, 1 = Basic authentication, 2 = MD5 digest (the safer authentication) | 0 | 0 | |
stream_authentication | Username and password for authentification | wont tell | disabled | |
on_event_start | Action to execute when event startes | echo “Event start” | ||
on_event_end | Action to execute when event ends | echo “Event end | ||
on_camera_found | Action to execute when a camera is found | echo “Camera found” | ||
on_camera_lost | Action to execute when a camera is found | echo “Camera lost” | ||
event_gap | Seconds an lasts at least | 15 | 60 |
Later the on_ actions will manipulate the state in a state store. The echos are for testing purposes. The stream_ settings are required because SpiderBB requires live streaming.
Testing
The values for on_event_start
and on_event_end
tell motion to print to the log each event start and stop. If I run motion with this configuration, every time I wave at the camera, “Event start” is printed. After holding still for 15s, “Event end” is published. I call this a total success, and for today its time to call it a night,
Summary
In this post, I’ve shown how I installed and configured motion. Motion alerts every event with printing to the log perfectly. In the next post, I’ll show how I store the current state and then tie it all together.