Introduction

I’ve covered a quick getting started guide for the Raspberry Pi camera here and now wanted to detail how to get motion working with a custom version that adds support for this camera with the MMAL API.  This post covers the basic motion setup, customising options, writing to a Windows/Samba share and also getting round the Chrome issue of streaming straight from the motion URL.

motion can detect motion using your camera device (through stills or video frames).  In addition it can also notify you when particular motion events have occurred.

Installation

Install motion with the following command the packages dependencies below should also be installed.

$ sudo apt-get install motion

ffmpeg libav-tools libavcodec53 libavdevice53 libavfilter2 libavformat53 libavutil51 libdc1394-22 libdirac-encoder0 libgsm1 libmp3lame0 libmysqlclient18 libopencv-core2.3 libopencv-imgproc2.3 libpostproc52 libpq5 libraw1394-11 libschroedinger-1.0-0 libspeex1 libswscale2 libtheora0 libva1 libvpx1 libx264-123 libxvidcore4 motion mysql-common

To complete the installation and support custom motion version added below, install these additional packages:

$ sudo apt-get install libjpeg62 libjpeg62-dev libavformat-dev libavcodec-dev libavutil-dev libc6-dev zlib1g-dev libmysqlclient-dev libpq-dev

Move to your /tmp directory and pull down the custom motion version.

$ cd /tmp

$ wget https://www.dropbox.com/s/xdfcxm5hu71s97d/motion-mmal.tar.gz

NOTE: Visit the GitHub page here for the latest prebuilt binary link.  The one above is the latest stable version at time of writing but there’s some more work being done (see appendix A at the bottom of this article)

gunzip and untar the downloaded file and you’ll have two additional files – motion-mmalcam.conf and motion.  Backup the original motion binary and move these new files to the locations specified below.  Lastly reset the owners of the motion binary.

$ tar zxvf motion-mmal.tar.gz

$ sudo mv /usr/bin/motion /usr/bin/motion.orig

$ sudo mv motion /usr/bin/motion

$ sudo mv motion-mmalcam.conf /etc/motion.conf

NOTE: this version of motion looks for motion.conf in /etc and NOT /etc/motion.

$ sudo chown root:root /usr/bin/motion

Configuration

Now we’ve got the motion binaries and custom version installed, there’s two configuration files that need updated.

$ sudo nano /etc/motion.conf

Browse or search (with Ctrl+W) these parameters and update as below (or with your own values).  All these parameters have a description in the motion.conf file.

Ensure the motion daemon starts automatically:

daemon on

Change the log file location to a suitable area:

logfile /tmp/motion.log

If you’re using a camera mount that rotates the camera (e.g. the Pimoroni mount) then enter your rotation value here:

rotate 180

The supported values are shown on the GitHub page mentioned above but the following gives your Pi some breathing room on CPU usage until further optimisations appear.  Adjust and test and required though.

width 768

height 432

Capture a specific number of frames before or after motion detection and add these to the output motion detection images and video:

pre_capture 2

post_capture 2

mpeg4 is fine, but msmpeg4 also ensure compatibility with Windows Media Player:

ffmpeg_video_codec msmpeg4

There a re few line items to change if you prefer your dates in a particular format (like me!).  I’m UK based so I prefer the dd/mm/yyyy type format so the following lines cater for this in my config.

text_right %d-%m-%Y\n%T-%q

text_event %d%m%Y%H%M%S

snapshot_filename %v-%d%m%Y%H%M%S-snapshot

picture_filename %v-%d%m%Y%H%M%S-%q

movie_filename %v-%d%m%Y%H%M%S

Ensure this setting is off – this is the default in the version of motion.conf supplied by the custom motion version but double check it anyway as this allows you to view the stream from another client.

stream_localhost off

Specify a location that the motion user (or more open) has access to write the output images and video.  See later in this article for connecting to a Windows/Samba share for this.

target_dir /tmp

Save and exit this file then open the motion default file and update the daemon here too.

$ sudo nano /etc/default/motion

start_motion_daemon=yes

Save and exit the file then reboot the Pi.

$ sudo reboot

You should now be able to connect http://<ip_address_of_pi>:8081 and view the stream from motion.  If the Pi camera LED goes on and then off again (or doesn’t come on at all) check out the motion log (location updated above).

The output in the target folder is typically .jpg files and when motion is detected there will be .avi files created also.

Viewing in Chrome

If you’re running Chrome there’s a known issue with connection to this type of stream and I’ll detail a workaround here.

Install the lighttpd web server and create a small HTML file.  Obviously name this HTML file whatever you like and adjust your link accordingly.

$ sudo apt-get install lighttpd

$ sudo nano /var/www/stream.html

<html>
	<title>
	motion stream on Raspberry Pi
	</title>
	<head>
	<h1 style="font-family:calibri;">motion stream on Raspberry Pi</h1>
	</head>
	<body>
	<br /><img src="http://<ip_address_of_pi>:8081">
	</body>
</html>

Save and exit this file.

Open Chrome and browse to http://<ip_address_of_pi>/stream.html

Connect to Windows/Samba share for motion output

NOTE: This configuration was set up connecting to a Samba share.

First, create a file that contains the secure credentials for connecting to your share:

$ sudo nano /etc/smbcreds

Enter two lines – one with username= and one with password= ; e.g.

username=rpiuser
password=password

Save and exist the file.  Adjust the security of the file so only root can read and write.

$ sudo chmod 600 /etc/smbcreds

Create a directory for mounting the remote share into and add the mount command parameters into /etc/fstab

$ sudo mkdir /camdata

$ sudo nano /etc/fstab

//<name_or_ip_of_server>/<name_of_share>	/camdata cifs creds=/etc/smbcreds,uid=motion	0	0

NOTE: Making the uid=motion ensured that the owner was the motion user so as long as the owner has rights on the destination directory then motion will have the same ones.

If you’re using a name of a remote Windows or Samba server and not using DNS, ensure your hosts file is up to date:

$ sudo nano /etc/hosts

Finally, mount your share and update your motion.conf file

$ sudo mount -a

$ sudo nano /etc/motion.conf

Update the paramater

target_dir /camdata

Save and exit the file.

$ sudo service motion restart

If you’re having issues with motion starting before the drive is mapping you can add a small sleep command into the /etc/init.d/motion file:

$ sudo nano /etc/init.d/motion

Browse down to the line that starts chown motion:motion and add sleep 10 on the next line (indented too).

Appendix A

There is still development going on with the custom version of motion for the Raspberry Pi camera.  You can find tuning and optimisation information on a new version here.  In addition the current status of this new version here.  Thanks to Dozencrows for making this available and accessible!

 

 

Working with the Raspberry Pi Camera, Raspbian and motion
Tweet about this on TwitterShare on Google+0Share on Facebook0Email this to someone
Tagged on:         

2 thoughts on “Working with the Raspberry Pi Camera, Raspbian and motion

  • 7 April 2017 at 00:43
    Permalink

    i got one question, i dont understand this command ;
    Change the log file location to a suitable area:
    logfile /tmp/motion.log
    sorry my english is kind of bad and im nod good programing, i got octoprint on my raspberry pi3 and a 3d printer
    will you please explain to me how i do that command?
    i dont understand CHANGE THE LOG FILE LOCATION. HOW I DO THAT.
    THANKS SO MUCH

    Reply
    • 14 April 2017 at 07:52
      Permalink

      As per article, all the parameters for motion are in the file /etc/motion.conf – each parameter is on its own line, just search or browse for the logfile one.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *