Setting up Wireless motion-detect cam UPDATED

ARTICLE UPDATE

I have received many requests for help with this project since I first wrote it over a year ago. So I decided to take another look at it. As it said at the beginning of the original article, the main input came from a posting by dozencrows, in the RaspberryPi Forums. He has in fact done further work on the original article, and a much better implementation can be downloaded from this post here. The actual download link for the modified motion file and associated config file is here. I have tried this out today on the very latest raspbian distro. As in the original article you should install motion and libjpeg62. Then download dozencrows modified motion and config file. For safety I suggest renaming HIS version of the motion file motion-mmal and using the set up and run command from my original article. That way you don’t run the installed motion binary by mistake.

./motion-mmal -n -c motion-mmalcam.conf

I tried this with his config file as downloaded, but you can of course modify it to meet your own requirements.

I also checked this out with my Synology NAS running DSM 5 with surveillance station 6.3 

Set up the camera as per the picture in the original article, and you can then record using that program. I hope that this update will make it easier for readers to get this going.
The original article follows below.

This project is based on a post by dozencrows here which explored the use of a Raspberry Pi camera board as a motion detecting video cam. It is based on the motion package in Debian. He has recompiled this with a slightly different configuration, making it convenient to work with the Raspberry Pi cam board. I take no credit for the code, but hope that details of how I have se it up on my system, and how I have connected it to a Synology NAS with built in Surveillance Station will be of interest to others. The original thread has multiple posts on it, and it can be confusing to get to grips with the code as a result.

What you need:

A Raspberry Pi, with PSU, camera board and WiFi adaptor (It would work with an ethernet connection, but it makes it less convenient for placing the camera is a suitable location. An SD card containing an uptodate copy of the wheezy-raspian distribution. If you have a blank card you can put the distribution on it together with configuring the wifi dongle by following the first section of my article here. You should also remember to check that the camera code support is enabled by typing  sudo raspi-config and setting option 5
For convenience I have ssh enabled on my card so that I can carry out the work on it via a link to a terminal on my main computer.

My wireless lan port has ip address 192.168.1.98, so I connect to the pi using
ssh pi@192.168.1.98 from a terminal window on my mac (you can install and use  putty on Windows)

Now to get going:

First make sure (if you haven’t already) that your raspian is up to date.
type
sudo apt-get update
sudo apt-get upgrade
it is also a good idea to make sure your firmware is up to date

sudo apt-get install rpi-update
and run it sudo rpi-update
a reboot is required when this is finished
sudo shutdown -r now
(you will have to reconnect via ssh when the Rpi has restarted if you are using a remote terminal)

Now install the motion program
sudo apt-get install motion

We don’t actually use this version, but it does insure that all the necessary dependencies are installed as well

sudo apt-get install libjpeg62

create and enter a directory called mmal in the user pi home directory using
cd ~/
mkdir mmal
cd mmal
download dozencrow’s version of the motion program using
wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-mmal.tar.gz
and extract the contents with
tar -zxvf motion-mmal.tar.gz
you now modify the configuration file, by opening it with the editor nano
nano motion-mmalcam.conf
nano hints
To insert a blank line, move to the beginning of a line and press return
To delete a line position the cursor on the line and press ctrl+k
use the backspace arrow to delete characters
you can search for a string by typing ctrl+w and inserting the string you wish to find.
When you have finished amending the file use ctrl+o to write out the amended file, selecting the existing filename to overwrite by pressing return. Then select ctrl+x to exit the editor.

you are going to alter the lines for 
width
height
target_dir
output_pictures
text _left
logfile
search for them in turn using ctrl+w and set them as follows

width 640
height 480
target_dir /home/pi/m-video
output_pictures off
text_left Pi-cam %t
logfile  /home/pi/mmal/motion.log

then write the changes out using ctrl+o push return and exit the editor with ctrl+x

So that there is no confusion as to which motion binary is running I rename the one in the mmal directory using mv motion motion-mmal

Assuming your camera board is correctly installed, you are now in a position to test it out
In your terminal window, from the mmal directory type
./motion-mmal -n -c motion-mmalcam.conf
where upon the camera board red led should light, and if you start a browser on another computer on your local network (Internet Explorer doesn’t work but Chrome, Firefox and Safari are ok) you should see live pictures at http://192.168.1.98:8081
You quit the process by typing ctrl+c

The start stop process is hardly ideal, so I have written a couple of scripts to start and stop the process.

type nano startmotion
In the editor window type
#!/bin/sh
nohup ~/mmal/motion-mmal -n -c motion-mmalcam.conf 1>/dev/null 2>&1 </dev/null &

type ctrl+o then push the return key to write the text, followed by ctrl+x to exit the editor
chmod 755 startmotion to make it executable

type nano stopmotion
In the editor window type

#!/bin/sh
ps -ef | grep motion-mmal | awk '{print $2}' | xargs kill

note: I put the script above in a code block as wordpress changes the quotes to smart ones otherwise which don’t work if you copy and paste

type ctrl+o then push the return key to write the text, followed by ctrl+x to exit the editor
chmod 755 stopmotion to make it executable

now you can type ./startmotion to start the camera and ./stopmotion to stop it
The videos created are stored in the m-video in the mmal home directory and the process is logged in the motion.log file
The command used in the start motion script was suggested by adayforgotten in the original topic thread. As you have seen, if you type the ./motion-mmal command to start the camera directly you cannot type subsequent commands into the terminal, and if you type ctrl+c or exit the terminal then the process terminates. Wrapping the command in nohup……& makes it run in the background and be independent of the terminal closing, which would normally stop dependent processes  The >/dev/null redirects the standard output to /dev/null, throwing it away. The “2>&1” part redirects the “standard error” output of the command to the standard output. the “</dev/null” unattaches the console keyboard and uses “</dev/null” instead. Thus the script can start the process without producing any screen output.
The stopmotion script uses ps-ef to give a formatted list the process id of running processes, pipes the output to grep to find the line for the process motion-mmal pipes that to awk ‘{print $2}’ which extracts the second field or process ID and finally pipes to xargs which feeds in turn any matching ID to kill which terminates them. It is instructive to build up this command line manually trying each step in turn. Thus
ps -ef
ps -ef |grep motion-mmal
ps -ef | grep motion-mmal | awk ‘{print $2}’
etc to see the output of each stage in turn (you should have the process running first of course!)

Connecting the the Synology NAS surveillance module
If you have such a NAS you can log in to it with a web browser and start the supplied surveillance station. The trick is in how to setup the camera configuration.
The photo below shows the settings which have to be entered
pi-cam1

Although the frame rate is rather slow, the system works and in the time line of the Surveillance program you can inspect video where there has been motion detected.
The surveillance program has a full help file.

Quck install files
I have put the start and stop scripts plus my amended motion-mmalcam.conf file in a zip file which you can download here into your Pi home directory using

wget http://r.newman.ch/mmal-bits.zip
followed by unzip mmal-bits.zip
quick install instructions are included in a README file

99 thoughts on “Setting up Wireless motion-detect cam UPDATED

  1. Hello there, great stuff!
    I have a question about this. When i start the cam it initially captures a motion, no matter if there was one or not. I think it’s because there’s no picture to compare with. But i have no idea how to change that. Do you have the same issue?
    Sincerely,
    S.

  2. Hi,
    When I run “./motion-mmal -n -c motion-mmalcam.conf” it looks for specific versions of the libraries. The latest debian repositories have moved up the versions of libavutil, libavcodec and libavformat. I tried creating softlinks but it now complains about “version `LIBAVUTIL_51′ not found (required by ./motion-mmal)”. Can I fix this without having to recompile his version of motion?

  3. Mmm… Getting this error:

    pi@vadelmapii:~/mmal $ ./motion-mmal -n -c motion-mmalcam.conf
    ./motion-mmal: error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or directory

  4. Hmm… Getting this error:

    pi@vadelmapii:~/mmal $ ./motion-mmal -n -c motion-mmalcam.conf
    ./motion-mmal: error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or directory

    • That is very odd, and I don’t see at all why this should be.
      As regards the article it is now quite old, and I suspect that some of the libraries may have moved on to newer versions whereas the version of motion used here was fixed from a download from a different site as you will see in the article. May I suggest you could look at the motioneye project instead https://github.com/ccrisan/motioneye/wiki/Install-On-Raspbian I have used this and it works very well.

      • That’s fine James. I only suggested motion eye as an alternative. I’m afraid my version is so old now that it is not viable to maintain further.
        As you will see from teh rest of my blog, most of my energies go into Sonic Pi now :-)

    • I merely suggested Motion Eye as an alternative, because as I have pointed out this article is very old now, and some of the code on which it is based has not been updated, so it is no longer maintained or in working order.

Leave a reply to Cédric Verstraeten Cancel reply