I just managed to get my hands on the Raspberry Pi (Model B+), so now i have a spare Model B, so what to build? I also have a USB Webcam (Trust Spotlight Webcam), so a DIY CCTV sounds like a great start. Turns out there’s several different applications to using the Rpi for home automation and other similar tasks. One of the most exciting projects is to use the camera for motion detection record video/images when motion is triggered. This blog posts provides a step by step guide on how I set up my PI and cameras, well as a live webcam server, and couple of other interesting things.
I’ll assume that you already have an Rpi working (if not, help on doing this can be got from http://www.raspberrypi.org/help/noobs-setup/). I’ll also be assuming that you have a basic understanding of Linux terminal commands.
I shall be controlling the Rpi using PuTTY on windows (SSH Client, download from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html). This just allows me to control the Rpi using it’s terminal from a windows machine. There’s nothing to prevent you from using this guide from an actually Rpi, in-fact that would actually be simpler.
Boot up the Rpi. The first step is to ensure that the OS is up-to-date. In the terminal (for those of you using the desktop version, open up a terminal first) type in the following lines and hit enter:
sudo apt-get update sudo apt-get upgrade
We shall now install motion, which is the motion detection software that we shall utilise.
sudo apt-get install motion
Motion essentially utilises a .conf (.conf for configuration) file that supplies it with the parameters and settings that dictate the motion detection and the corresponding output/response. We shall now edit this .conf file. In the command window type:
sudo nano /etc/motion/motion.conf
You will quickly realise that this is a large file with over 600 lines of text for several settings. Feel free to read through. The file itself is quite well organised, with sections for different blocks of properties such as images, video, etc. They also provide us with descriptions on what each property does, which is quite handy.
For now, we shall focus on a couple of things:
daemon on #default off (This allows the motion to run in the background) framerate 30 #default 2 (increased framerate) width 640 #default 320 (changed width to match that of the webcam) height 480 #default 240 (same as above but for height) threshold 2000 #default 1500 (*explained in detail below) pre_capture 2 #default 0 (captures 2 frames before motion was detected and adds that to the videos to make them smoother) post_capture 5 #default 0 (same as above but captures frames after) output_normal off #default on (this disables storing images, since we only require video) ffmpeg_video_codec msmpeg4 #default swf (msmpeg4 is accepted by windows media player, hence easier to play on Windows) target_dir /mnt/motionvideos #default /tmp/motion (changed the directory where videos will be stored) webcam_maxrate 5 #default 1 (increase the max framerate on lie stream) webcam_localhost off #default on (allows you to set up a live stream of the webcam)
*threshold: This property is essentially how motion operates. It compares 2 frames and if more than x pixels have changed, it concludes that motion has occurred. The value of x is what we are setting here, although I have no proof, i suspect that increasing the resolution will require this number to be increased as well.
I would highly recommend reading through the file to get a better understanding of what each of the properties above does.
Tip: You can use ‘Cntrl+W’ to search for items in the file since we have used the ‘nano’ command to open the file.
Use Cntrl+X to close the file. You will be prompted to save, type Y (for yes) and hit enter for the file name.
Well need to also create the directory we specified in targer_dir above and change the owner of that directory to motion. Use the following command to do this:
sudo mkdir /mnt/motionvideos sudo chown motion /mnt/motionvideos
We’ll now enable daemon by using the following command:
sudo nano /etc/default/motion
Once the file is opened, change start_motion_daemon=no to yes. Then hit Ctrl+X to close and Y to save (same as above). Enabling daemon also means that motion will start on boot by default.
This completed the process of setting up motion. Now all we need to do is plug in the USB Webcam and start motion.
Note: If using an older version of The Rpi (Model A or B with 2 USB ports) if you have something plugged into the ports, you may require a USB hub. It is recommended that you use a powered hub as there is only a limited amount of power that the Rpi can supply.
To start motion, type:
ubuntu@RPI:~$ sudo service motion start * Starting motion detection daemon motion [ OK ]
While motion is running, if you want to check the status via the log, type (this will show you the last 50 lines of the syslog file):
tail -n50 /var/log/syslog
To view your live stream, open Firefox (IE and Chrome will require plug-ins to view a live webcam) and in the address bar type [RPI’s IP Address]:8081. You can also view the stream via VLC.
Once you are satisfied and want to stop motion, use:
sudo service motion stop
Now navigate to /mnt/motionvideos and you should see some video(s) of what you just recorded. If your webcam was pointed at a still frame and no motion was detected, you may not have any files, so try again with the webcam pointed at an active location.
Videos tend to take up a lot of storage memory. If you don’t have the luxury of a large SD card, it might be worth storing the videos on network storage, Not only does this save some valuable space but also allows you to access the files from elsewhere, and in our case on a Windows machine. You may have realised that viewing the videos on the Rpi is not the best of options as it really struggles with video output. The next steps in this guide show how to store your files on a shared folder in a Windows machine.
Note: The videos stored in the /mnt/motionvideos folder will get hidden once we mount some storage.
Firstly, set up a new Windows account with basic rights and a password. The main reason behind this will be explained later. Once this is done, create a new folder where you would like to store the videos. Then share this folder by right-clicking on it and clicking on ‘Properties’. Next, go to the ‘Sharing’ tab and click on ‘Advanced Sharing’. Tick the ‘Share this folder’ option. Then click on ‘Permissions’ and allow ‘Everyone’ to have ‘Full Control’ i.e. change and read the contents. Once this is done, click on ‘OK’ for the ‘Permissions’ and ‘Advanced Sharing’ windows.
For screenshots on the process, visit http://www.howtogeek.com/176471/how-to-share-files-between-windows-and-linux/. Note that only the second half of the tutorial on that page is slightly different to what I do below on Linux.
Finally take note of the IP Address of the machine.
We have now configured Windows to share the folder. The next step is to access the folder from the Rpi. Firstly, we need to find the UserID (uid) and GroupID (gid) of the motion user. To find this, in the terminal, type:
cat /etc/passwd [SNIP] motion:x:103:106::/home/motion:/bin/false
Find ‘motion’ in that file. Take note of the 2 numbers after ‘motion:x:’. In this case they are 103 and 106 respectively.
Next, we will add the shared folder to our mount list. Use the command:
sudo nano /etc/fstab
Add a new line in the list and insert the following:
//[Windows machine IP Address]/[Shared folder] /mnt/motionvideos cifs auto,_netdev,username=[Windows Username],password=[Windows Password],uid=[First number from previous step],gid=[Second number from previous step] 0 0
Note: Make sure you remove the  once you have entered the relevant information.
Save and close the file.
Now we will mount the shared folder.
sudo mount -a
To check that you have actually mounted it, ask the machine to show you all mounted drives/devices. Do this by:
You should see a line with the IP Address of your Windows machine.
Once this is set up, we can further test by using the following command:
cd /mnt/motionvideos sudo touch test
Open up the shared folder in Windows and you should see a file names test. This shows that you have correctly configured the share. Run motion and check that video files are now being written to the shared folder.
Congratulations! That completes the guide. You should have successfully set up a motion detection webcam and a live server to view your webcam’s output by now. Feel free to play around with the motion.conf file and tweak it to suit your needs. You will see that there are several handy options such as image output, the ability to trigger external commands when motion is detected and other quite interesting features.
Figure 3: Screenshot showing some of the videos motion has captured
Article by: Umang Rajdev
Other links/Background reading: These are some of the links I referenced during the process. Feel free to browse through the pages.