Setup Raspberry Pi with Motion 4.2.2 from scratch

Move fast, break things

Download latest Raspbian image

Flash your SD card with Raspbian

Mount your freshly flashed SD card

  • unplug and replug the SD card
  • Navigate to the SD card from terminal
cd /Volumes/boot
  • I want to run this Pi headless, enable ssh by creating a file "ssh"
touch ssh
  • Prepare the headless Pi to join the Wi-Fi by creating a file "wpa_supplicant.conf"
nano wpa_supplicant.conf
country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
  ssid="TwoPointFourBillionHertz"
  psk="NETWORK-PASSWORD"
}
  • eject the SD card
  • slam the SD card into the Pi
  • power it up
  • watch your DHCP server for new leases or scan your LAN to discover the IP assigned to your freshly minted Pi
  • ssh into the Pi

If you are rebuilding the pi, you might need to wipe the 'known hosts' from your mac

rm -f .ssh/known_hosts
ssh pi@192.168.0.45 #You need to find your own Pi's IP address
  • default password is raspberry
  • you should change that
sudo raspi-config

From here you can:

  1. Change User Password

Change the Hostname

  • Network Options > Hostname

Enable the Camera

  • Interfacing Options > Camera

Expand the Filesystem

  • Advanced Options > Expand Filesystem

Exit and Reboot

  • Finish

Ok, looking good, sir!

ssh back into the Pi

sudo apt-get update
sudo apt-get upgrade

Congratulations, the Pi is all setup

Next up, test the camera

Find the latest version of Motion suitable for Raspberry Pi

https://github.com/Motion-Project/motion/releases

Look out for 'pi_' at the beginning
probably looks something like: pi_buster_motion_4.3.1-1_armhf.deb

copy its link URL

wget {copied URL}
sudo apt-get install gdebi-core
sudo gdebi {deb package name}

test motion

sudo motion

review output, if no errors, proceed...
If you get 'segmentation fault' you have installed the wrong version of Motion, not the specifically Pi version

Configure Motion for real now

sudo nano /etc/motion/motion.conf

The Motion 4.1.1 default config contained many many more additional lines of configuration. In 4.2.2 it has been cleaned up greatly, but I feel could lead to some config options being missed out. Below is an example config file similar to one I use, with the interesting options included.

# This config file was generated by motion 4.2.2
# Documentation:  /usr/share/doc/motion/motion_guide.html
#
# This file contains only the basic configuration options to get a
# system working.  There are many more options available.  Please
# consult the documentation for the complete list of all options.
#

############################################################
# System control configuration parameters
############################################################

# Start in daemon (background) mode and release terminal.
daemon on

# File to write logs messages into.  If not defined stderr and syslog is used.
; log_file value

# Level of log messages [1..9] (EMG, ALR, CRT, ERR, WRN, NTC, INF, DBG, ALL).
log_level 6

# Target directory for pictures, snapshots and movies
target_dir /home/pi/motion

# Video device (e.g. /dev/video0) to be used for capturing.
videodevice /dev/video0

# Parameters to control video device.  See motion_guide.html
; vid_control_params value

# The full URL of the network camera stream.
; netcam_url value

# Name of mmal camera (e.g. vc.ril.camera for pi camera).
; mmalcam_name value

# Camera control parameters (see raspivid/raspistill tool documentation)
; mmalcam_control_params value

############################################################
# Image Processing configuration parameters
############################################################

# Image width in pixels.
width 640

# Image height in pixels.
height 480

# Maximum number of frames to be captured per second.
framerate 12

# Text to be overlayed in the lower right corner of images.
text_right %Y-%m-%d\n%T-%q

# Rotate image this number of degrees. The rotation affects all saved images.
# Valid values: 0 (default = no rotation), 90, 180 and 270.
rotate 180

# Turns the text showing changed pixels on/off. By setting this option to 'on'
# the number of pixels that changed compared to the reference frame is
# displayed in the upper right corner of the pictures.
text_changes on

############################################################
# Motion detection configuration parameters
############################################################

# Always save pictures and movies even if there was no motion.
emulate_motion off

# Threshold for number of changed pixels that triggers motion.
threshold 15000

# Noise threshold for the motion detection.
; noise_level 32

# Despeckle the image using (E/e)rode or (D/d)ilate or (l)abel.
despeckle_filter EedDl

# Number of images that must contain motion to trigger an event.
minimum_motion_frames 12

# Gap in seconds of no motion detected that triggers the end of an event.
event_gap 20

# The number of pre-captured (buffered) pictures from before motion.
pre_capture 15

# Number of frames to capture after motion is no longer detected.
post_capture 0

lightswitch_percent 90

############################################################
# Script execution configuration parameters
############################################################

# Command to be executed when an event starts.
on_event_start python3 /home/pi/cat_flap_pi/motion_trigger.py %v

# Command to be executed when an event ends.
; on_event_end value

# Command to be executed when a movie file is closed.
; on_movie_end value

############################################################
# Picture output configuration parameters
############################################################

# Output pictures when motion is detected
picture_output on

# File name(without extension) for pictures relative to target directory
# YYYYMMDD HHMM SS-frame [event ID] (pixel change)
picture_filename %Y%m%d %H%M %S-%q [%v] (%D)

picture_quality 100

############################################################
# Movie output configuration parameters
############################################################

# Create movies of motion events.
movie_output off

# Maximum length of movie in seconds.
movie_max_time 60

# The encoding quality of the movie. (0=use bitrate. 1=worst quality, 100=best)
movie_quality 45

# Container/Codec to used for the movie. See motion_guide.html
movie_codec mkv

# File name(without extension) for movies relative to target directory
movie_filename %t-%v-%Y%m%d%H%M%S

############################################################
# Webcontrol configuration parameters
############################################################

# Port number used for the webcontrol.
webcontrol_port 8080

# Restrict webcontrol connections to the localhost.
webcontrol_localhost off

# Type of configuration options to allow via the webcontrol.
webcontrol_parms 2

############################################################
# Live stream configuration parameters
############################################################

# The port number for the live stream.
stream_port 8081

# Restrict stream connections to the localhost.
stream_localhost off

stream_quality 100

# This change will allow for real-time streaming but requires more bandwidth & resources.
# Needs to be added to the config file, default is 1
stream_maxrate 12

##############################################################
# Camera config files - One for each camera.
##############################################################
; camera /etc/motion/camera1.conf
; camera /etc/motion/camera2.conf
; camera /etc/motion/camera3.conf
; camera /etc/motion/camera4.conf

##############################################################
# Directory to read '.conf' files for cameras.
##############################################################
; camera_dir /etc/motion/conf.d
  • In case that is all too much, here are the really important lines to change:
daemon on
stream_localhost off
target_dir /home/pi/motion
  • Assuming you wish to save pictures / video, the target directory is important to pay attention to!
  • You need to make the directory for Motion
mkdir /home/pi/motion
  • You also need to setup the permissions for this folder
sudo chgrp motion motion
sudo chmod g+rwx motion
  • Finally, setup the daemon
sudo nano /etc/default/motion
  • change the line to read "yes"
start_motion_daemon=yes
  • Now we can test Motion
sudo motion
  • test via browser
  • Theoretically you can now browse to the ip address of your Pi, and view the live stream
IP ADDRESS OF YOUR PI:8081
  • If the page does not load, motion has got a problem, to take a look at that:
sudo service motion status
  • If you see errors relating to permissions, such as
[1:ml1] [NTC] [ALL] motion_detected: Motion detected - starting event 1
[1:ml1] [ERR] [ALL] myfopen: Error opening file ~/home/pi/motion/20191117 1042 42-10 [01] (0).jpg with mode w: Permission denied
[1:ml1] [ERR] [ALL] put_picture: Can't write picture to file ~/home/pi/motion/20191117 1042 42-10 [01] (0).jpg - check access rights to target directory
Thread is going to finish due to this fatal error: Permission denied

Possibilities:

The folder where you are attempting to save the images does not exist

  • in my example: ~/home/pi/motion is WRONG
  • it should have read /home/pi/motion
  • The permissions are insufficient for the user "motion" to write to the intended folder

Much credit and thanks to:

https://pimylifeup.com/raspberry-pi-webcam-server/
https://desertbot.io/blog/headless-raspberry-pi-4-ssh-wifi-setup