2. Installation of Motion

Motion is the essence of the project. It is a wonderful program, which is easy to install and possesses a wide variety of functions that can be used to expand the project. The principle of Motion is the following: it constantly watches a stream of video and counts the amount of pixels that change the intensity. As soon as the change of the image exceeds a certain level (counted as the amount of changed pixels), a motion detection event is triggered. This event can be used for a number of different things.

Prerequisites

As you may expect, the core motion detection is done by the software. Although a PIR sensor can be used as an alternative, Motion offers a lot of options for interfacing of other pieces of software and thus the flexibility. Installing Motion and complementing it with some code is how the project operates.

Before we proceed with installing Motion, we need to pre-configure the Raspberry Pi to a certain extent. This is our "to do" list for now:

Obviously, connecting the hardware is extremely easy and well-documented in the web. Just for the sake of reference, I will list the configuration of the WiFi connection.

WiFi connectivity

In order to make the system more robust, it makes sense to set up the static IP, since knowing of the IP adress is vital for the system to function. The exact address of the Raspberry Pi may be different in your case, so in order to avoid confusion, I will just refer to the address as PiIP (e.g. 192.168.1.10). The configuration procedure for this part is best discussed here.

Installing Motion

Before installation, it is good to make sure the Raspberry is up to date. (If you use Linux, simply type ssh PiIP in the terminal. In Windows, you may use PuTTy instead):

sudo apt-get install rpi-update
sudo rpi-update
sudo apt-get update
sudo apt-get upgrade

Now, let us download the program Motion. There is a good tutorial available here. However, that link instructs to download Motion using wget as a .tar.gz file, which is later extracted. It did not work for me. That is why, I suggest you follow the apt-get way.

To install Motion, execute:

sudo apt-get install motion

Then, make sure that all libraries are set in place.

sudo apt-get install -y libjpeg62 libjpeg62-dev libavformat53
libavformat-dev libavcodec53 libavcodec-dev libavutil51 libavutil-dev
libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev

By the way, several tutorials prompt to set start_motion_daemon=yes under /etc/default/motion file. If you want to make Motion run in the background at all times, do it. I don't, since I would like to be able to switch the program on and off extarnally.

The Motion program is installed. In order for it to function, we need to change the file permissions.

sudo chmod 644 /etc/motion/motion.conf
sudo chmod 755 /usr/bin/motion
sudo touch /tmp/motion.log
sudo chmod 644 /tmp/motion.log

That makes sure that files can be read from, written to and executed correctly.

Finally, the essence of the program configuration. All that is done by writing lines into the /etc/motion/motion.conf file. Consult the Motion documentation for all the options and experiment yourself. In my case, I edited the following lines:

daemon on
rotate 180
width 640
height 480
framerate 2
threshold 6801
pre_capture 2
post_capture 2
event_gap 30
max_movie_time 300
quality 75
ffmpeg_video_codec msmpeg4
stream_port 8081
stream_quality 50
stream_maxrate 2
stream_auth_method 2
webcam_localhost off
stream_authentication USERNAME:PASSWORD
target_dir /home/USER/CAMERAPROJECT/INBOX
on_event_end sudo /bin/zip_and_send.sh

Don't worry if the lines look a bit misterious. If you open the motion.conf file, the meaning of all the lines is explained in the supplied comments. As I wrote before, consult the documentation of the program and experiment yourself.

The last two lines, are sort of special add-ons of this project. The target_dir specifies the output directory of all the images and videos to be saved to, while on_event_end supplies the expanson of the project regarding of what to do with the pictures/videos once motion is detected. We will discuss that extension on the next page. Note that I have written in capitals the names that will probably be different in your case.

Now it should be ready...

The Motion program can be switched on by executing the following command in the terminal window:

sudo motion start

For switching it off, use:

sudo pkill motion

Oh! One more thing. Some tutorials instruct to set disable_camera_led=1 line in /boot/config.txt file. Obviously, you don't want the others to know that their presence has activated your camera (or do you?). Personally, I prefer to wait with this kind of settings until the very end as it helps me with debugging. If everything works correctly, you should see the camera LED shine red if you switch the Motion on. Furthermore, opening a web browser and typping http://PiIP:8081 in the address field should display the real-time stream from the camera. Checking the content of the /home/USER/CAMERAPROJECT/INBOX directory should indicate the detected presence. If not, check /tmp/motion.log file for any error messages. Misspelled words or wrong paths/filenames are there to blame.

left right