Ever want to keep an eye on your house while your away? Maybe observe backyard wildlife close-up? Or my favorite for IT, keep an eye on your server room? Low-end wireless IP cameras start at $50-$75 USD. Higher-end units can run into the hundreds of dollars. Add motion detection and the price raises even further. How about a lower-cost solution? Using a Raspberry Pi with an inexpensive webcam, a wireless WiFi Module, and optional battery pack, you can a remote, motion-activated camera solution, at a fraction of the cost. Best of all, you won’t need to write a single line of code or hack any electronics to get started.
There are many good articles on the Web that demonstrate how to build a RaspPi-based motion-activated video solution. One of the more popular software solutions is Motion. According to their website, ‘Motion is a program that monitors the video signal from one or more cameras and is able to detect if a significant part of the picture has changed; in other words, it can detect motion‘. Motion has all the capabilities to stream images from your webcam to a built-in web server, where you can reach them through your browser, with little or no configuration. Motion is easily configured to work with streaming video apps like FFmpeg, save images to a database like mySQL or PostgreSQL, and execute external scripts such as python or shell. We are going to use Motion’s most basic features in this post, motion detection and streaming.
Before installing Motion, I recommend ensuring your Raspberry Pi is up-to-date with the latest software and firmware. Updating firmware is not necessary; however I had an experience while helping someone else with their RaspPi, where their camera would not work correctly. After finding a few folks online with similar problems, we updated the firmware on the RaspPi and fixed the problem. Installing firmware can sound a bit intimidating; however, Liam McLoughlin (hexxeh) has made the process easy with rpi-update. I have used it successfully on three RaspPi’s. Three commands to update your RaspPi.
To update your RaspPi’s software, execute the following command. Note if you don’t do this on a regular basis, as recommended, these steps could take up to 5 minutes or more. Watch for errors. If there are any, just run it again. Sometimes, the RaspPi cannot connect to all sources for updates.
sudo apt-get update && sudo apt-get upgrade
Once the updates are completed successfully, install Motion by issuing the following command:
sudo apt-get install motion
As the installation completes, you should see a warning in the command shell about Motion being disabled by default.
... Adding user `motion' to group `video' ... Adding user motion to group video Done. [warn] Not starting motion daemon, disabled via /etc/default/motion ... (warning). Setting up ffmpeg (6:0.8.4-1) ... pi@garyrasppi ~ $
To enable Motion (the motion daemon), we need to edit the
sudo nano /etc/default/motion
Change the ‘
start_motion_daemon‘ parameter to ‘yes’.
Motion is easy to customize, with a plethora of parameters you can tweak based on your needs. Since Motion has no GUI, configuration is all done through Motion’s configuration file (
Before editing the configuration file we need to change the permissions on it, so Motion can access it. While we’re at it, we will also change permissions on the folder where Motion stores images.
sudo chmod -R 777 /etc/motion/motion.conf sudo chmod -R 777 /tmp/motion
Open the configuration file in Nano with the following command.
sudo nano /etc/motion/motion.conf
Motion’s configuration file is lengthy, but broken down into sections to make finding the setting you are looking for, easy. First, we need to change the ‘Live Webcam Server’ section of configuration. Below are the default settings:
############################################################ # Live Webcam Server ############################################################ # The mini-http server listens to this port for requests (default: 0 = disabled) webcam_port 8081 # Quality of the jpeg (in percent) images produced (default: 50) webcam_quality 50 # Output frames at 1 fps when no motion is detected and increase to the # rate given by webcam_maxrate when motion is detected (default: off) webcam_motion off # Maximum framerate for webcam streams (default: 1) webcam_maxrate 1 # Restrict webcam connections to localhost only (default: on) webcam_localhost on # Limits the number of images per connection (default: 0 = unlimited) # Number can be defined by multiplying actual webcam rate by desired number of seconds # Actual webcam rate is the smallest of the numbers framerate and webcam_maxrate webcam_limit 0
The first thing you will want to change is Motion’s default setting that restricts image streaming to ‘
localhost‘, only. This means you can only view images in a web browser on the RaspPi, not remotely on your LAN. Change that line of code to read ‘
The next setting I recommend changing for security purposes is the default port Motion uses to stream images, port 8081. Change this port to another random port, for example, 6789 (‘
webcam_port 6789‘). This means if your RaspPi’s wireless IP address is 22.214.171.124, images from your webcam should be accessible at 126.96.36.199:6789. If you plan on making the webcam’s images available over the Internet, I definitely recommend changing the port. Security through obscurity is better than no security at all.
The other two settings in this section you can play with are the webcam quality and maximum frame-rate. You will have to adjust this based on your network speed and the processing power of your RaspPi. The default settings are a good place to start. I changed my quality from the default of 50 to 80 (‘
webcam_quality 80‘), and I changed my max frame-rate to 2 (‘
Speaking of quality, the other two settings you may want to change are the width and height of the image being captured by motion. This is done in the ‘Capture device options’ section. As the file’s comments suggest, these settings are dependent on your camera. Check the camera’s available image sizes; you will need to use one of those size combinations. I have mine set to an average size of 352 x 288. This is a good size for those of us with a slower network, or when streaming video over the Internet to mobile web browser. Conversely, a larger image is better for viewing on your local LAN. Image size, like compression quality, and frame-rate are dependent on processing power of your RaspPi and its OS (Raspbian, Debian, Arch, etc.). You may need to play with these settings to get the desired results. I couldn’t stream images larger than 352 x 288 over the Internet, even though my webcam could go up to 640 x 480.
<pre># Image width (pixels). Valid range: Camera dependent, default: 352 width 352 # Image height (pixels). Valid range: Camera dependent, default: 288 height 288
It’s important to remember, each time you make changes to Motion’s configuration file, you must restart Motion, using the following command.
sudo /etc/init.d/motion restart
Viewing Your Webcam Remotely
To view your webcam’s output from another device on your LAN, point your web browser to the IP address of your RaspPi and the port you assigned in Motion’s configuration file. Motion may take up to 15-20 seconds to start responding in the browser. If it takes much longer, you probably have your image size, frame-rate, and compression settings to high for your RaspPi.
Viewing Your Webcam Over the Internet
Viewing your webcam’s output over the Internet can be relativity easy or totally impossible, depending on your router and your Internet provider. In my case, I configured port-forwarding on my Netgear Wireless Router. In the example below, I created port-forwarding rule, which allows all external HTTP requests to port ’3456′ to be forwarded to internal port ’6789′, which is the streaming port for Motion on my RaspPi.
For example, let’s say the internal address of my RaspPi on my LAN is 192.168.1.9, and my external Internet IP address if 188.8.131.52. I go down to the local coffee shop and decide to check if the mailman has delivered my new Raspberry pi to my front porch. I enter http://184.108.40.206:3456 in my phone’s web browser. My home router receives the request and forwards it to 192.168.1.9:6789, which returns a stream of still images to my phone at the coffee shop. Still no RaspPi, so sad…
So what is the external address of my network? I checked the Internet port’s IP address in the Netgear’s Router Status window. Since I do not pay my Internet-provider for a static IP address, the address I am assigned is dynamic. It can and will change, sometimes almost never, sometimes daily; it depends on your provider. To view your webcam’s images, you will have to know your current Internet port’s IP address. User this address, with the external port you used for port-forwarding.
Here are some example from a Microsoft LifeCam VX-500 and Logitech Webcam C210 webcams. The highest quality I could consistently stream over the Internet, from my RaspPi 512Mb Model B, with both Soft-float Debian “wheezy” and Raspbian “wheezy”, was 352 x 288 at 80% compression and 2 fsp max. Locally, on my LAN I could achieve 640 x 480.
In this example, I’ve placed the RaspPi in a plastic container to protect it, and mounted the attached webcam in a flower box. Viewing the feed over our local network, we are able to watch the hummingbirds without spooking them.
In these two images, I’ve turned on Motion’s ‘locate box’ option, which tracks the exact area within the image that is moving. As I come into view of the camera, Motion detects and outlines the area of the images that are changing.
In the next video, you see the view from a Google Nexus 7 tablet. My wife and I use the RaspPi surveillance system to monitor our pool area or backyard when the kids have friends over (the camera is no substitute for adult supervision when the kids are in the pool).
This last image is from my iPhone while shopping at the local grocery store. My wife was pretty impressed with my port-forwarding knowledge. OK, not really…
Here are a few links to other useful articles on the use of Motion with the Raspberry Pi:
motion(1) – Linux man page (good source for understand Motion config)
Linux UVC Supported Devices (a good starting point for buying a webcam)