A common use case for a Raspberry Pi* is monitoring one or more network or webcams. With the RPi 3, this now also works quite well. I myself use an RPi 3 together with a network camera to monitor my driveway. In this configuration, the RPi detects a change in the monitored area and saves the webcam image. It would also be possible to record a short video. The software used here, "motion", is widely used and very powerful, so that many applications can certainly be covered. Here I will show the commissioning and the most important configuration settings so that you can then build on them to configure your own solutions.
- ffmpeg for rtsp (optional)
- Install motion
- Configure motion
- Start motion
Overview: Raspberry Pi Cam, USB Camera, Webcam or Network Camera?
"motion" can monitor almost all cameras. Also the RPi cams. It is important that there is a Linux driver for this camera, then the camera appears in the file directory under /dev/video0.
If you have a network camera that is addressed via its own IP, this is also no problem, these streams can also be monitored. Even RTSP formats are possible, which is very practical since most cheap, but also better, Chinese devices now support this.
We will compile motion and required dependencies ourselves. This has the advantage that we have more functions than in the precompiled version.
Before we start, however, we should first clean up our system. To do this, we need to execute the following commands. The easiest way is to copy the commands into a Bash file and then execute it. Alternatively, the commands can of course also be executed individually:
sudo bash apt-get update apt-get upgrade apt-get remove x264 apt-get remove libx264-dev apt-get remove libavutil apt-get remove libpostproc apt-get remove libswresample apt-get remove libswscale, ffmpeg apt-get remove libavcodec apt-get remove libavfilter apt-get remove libavresample apt-get remove libavcodec apt-get remove libavfilter
Now we install the necessary tools to compile motion itself:
sudo bash apt-get install autoconf automake build-essential libtool libjpeg8-dev libzip-dev
apt-get install libavformat-dev libavcodec-dev libavutil-dev libav-tools libswscale-dev cd / git clone git://git.videolan.org/x264.git cd x264 ./configure --enable-shared --disable-opencl make sudo make install && ldconfig
3. ffmpeg for RTSP (optional)
If we want to be sure that motion supports our camera or for RTSP, we now have to install ffmpeg. This takes almost 2 hours on the RPi 3 B:
cd / sudo bash git clone git://source.ffmpeg.org/ffmpeg.git cd ffmpeg ./configure --arch=armhf --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree --enable-shared make sudo make install && ldconfig
4. Install motion
Now we install the actual monitoring programme, which is much faster again:
cd sudo bash git clone https://github.com/Mr-Dave/motion.git cd motion autoreconf -fiv ./configure make sudo make install && ldconfig
5. Configure motion
Finally, we only have to adapt motion to our needs. This is done with the motion.conf file which we still have to set up:
sudo mv /usr/local/etc/motion/motion-dist.conf /usr/local/etc/motion/motion.conf
Then we can open the file for editing. Important settings are:
If you have a USB or RPi cam, you must enter the path here. If you have a network camera, this setting must be completely commented out.
If you have a network camera, the path to the stream (e.g. rtsp) must be entered here.
The optional access data to the network camera in the format username:password
width and height
The width and height of the webcam image. You should set these values to the same settings as the camera delivers the image. Otherwise motion converts the image, which costs quite a bit of computing power on the RPi.
Specifies how many pixels must change in the image for a change to be detected. Caution: This is strongly dependent on the video resolution! The specified 1500 are not usable with HD resolutions.
Specifies how much a pixel must deviate in order to be recognised as changed. This is useful to prevent noise from being recognised as movement.
Draws the number of detected changed pixels in the upper right corner. Very useful to find the right threshold.
Folder in which photos and videos are to be saved.
Specifies on which port an RTSP stream is provided with the image to be monitored.
If enabled, the stream can only be retrieved from localhost
Indicates on which port a small web server can be reached for control purposes.
If activated, the web server is only accessible from localhost.
Defines the location of the logfile. A very useful helper, especially at the beginning!
6. Start motion
Motion can now be gestured