Skip to main content

Linux Security Cameras

Security Camera Management Programs

There are two decent Linux programs for managing security cameras: Motion and ZoneMinder.

For test purposes, I set a camera up inside my living room.  Once working, I'll install two outside, to watch the driveway and backyard.

 Streaming Camera with Fish Eye Lens

Motion is the simpler one of the two programs, but with almost 300 configurable options, it can do anything worth doing.  It works just as well as ZoneMinder, but it simply doesn't do things that are not needed.  For example, Motion supports a database, but Linux filesystem storage is usually good enough.  It doesn't have a fancy GUI, since it is intended to run on a server, stashed on a shelf in a dark corner.  There is a GUI which can be accessed with a web browser locally, or over the LAN, but I never used it.  One can also view real-time streams with a web browser.  Notifications can be sent and files uploaded to another machine with external programs such as mutt, wput and others.

While one can watch a TP Link security camera with a cell phone App,  I want to monitor what is going on when not at home, without spending money on a subscription service.  The simplest way to do this, is to use a free email account as an archive. Wherever I travel, I can log into gmail with a web browser and look at the stored security videos, to confirm that my house hasn't burned down and that there isn't an old Baba Yaga with a black cat and a white goat staying there.

In this guide, I describe how to configure Motion to handle a TP-Link C100 RTSP streaming camera and email a short video clip whenever motion is detected, using mutt with a gmail account.

Install Motion and Mutt

Mutt is a simple command line mail client which is fully supported and updated by Ubuntu Linux.  It provides the ability to send attachments using a free mail account from gmail, yahoo or similar service.  The parameter syntax of mutt is a little wonky - do yourself a favour and read the man page at least 3 times.

  • $ sudo su -
  • password
  • # apt install motion mutt ffmpeg 

Streaming Cameras

First of all, get the TP-Link camera to work.  You need to install an App on your cell phone to do that.   Using this App, create a user account with a password to use for the RTSP stream login.  With the App, you can also find the camera IP address - or look in your router DHCP list for it.

Use ffplay to verify that you can log in and play the camera streams:

  • $ ffplay rtsp://USERNAME:PASSWORD@IP.ADD.RE.SS/stream1

The above is the EXACT string that you need to use later in the camera configuration.

Security Considerations

For improved security, I configured mutt and motion to run under my user account on an Ubuntu Linux server.

Configure Mutt

  • $ cd
  • $ mkdir .mutt

Create a new gmail account for the security system, then go to the account security settings and set Support for Less Secure Apps ON.  Otherwise mutt will be blocked by Google. Simply send the attachments to this gmail address and leave it all there. Don't clog up your regular email account with security cam spam.  When Google eventually complains that the mailbox is full (you get 15GB for free), you can simply select all and delete it, then let it fill up again.

Create a file called mutrc.

Replace USERNAME, REAL NAME and PASSWORD with the exact details used when creating the gmail account. Put SINGLE QUOTES around the password to prevent the expansion of any $ signs in the password.

set from = "USERNAME@gmail.com"
set realname = "REAL NAME"
set imap_user = "
USERNAME@gmail.com"
set imap_pass = 'PASSWORD'
set smtp_url = "smtps://
USERNAME@smtp.gmail.com"
set smtp_pass = '
PASSWORD'
set folder = "imaps://imap.gmail.com/"
set spoolfile = "+INBOX"
set postponed="+[Gmail]/Drafts"
set trash = "+[Gmail]/Trash"

Configure Motion

  • $ cd 
  • $ mkdir .motion
  • $ cd .motion
  • $ cp /etc/motion/motion.conf .
  • $ cp /etc/motion/camera* .

Motion.conf

Make a file called motion.conf.  In the below example change USERNAME to suit.

# Rename this distribution example file to motion.conf
#
# This config file was generated by motion 4.3.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 off

# Start in Setup-Mode, daemon disabled.
setup_mode off

# File to store the process ID.
pid_file /home/USERNAME/.motion/pid

# File to write logs messages into.  If not defined stderr and syslog is used.
log_file /home/USERNAME/.motion/motion.log

# 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/USERNAME/Videos

# 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 1920

# Image height in pixels.
height 1080

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

# Text to be overlayed in the lower left corner of images
text_left "CAM1"

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

############################################################
# 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 1500 

threshold 10000

# 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 1
;minimum_motion_frames 5

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

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

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

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

# Command to be executed when an event starts.
; on_event_start value

# 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
on_movie_end "echo 'Security video'|mutt -i - -s 'Security Video' -a "/home/USERNAME/Videos/CAM1-%t-%v-%Y%m%d%H%M.mkv" -- USERNAME@gmail.com"

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

# Output pictures when motion is detected
picture_output off

# File name(without extension) for pictures relative to target directory
picture_filename %Y%m%d%H%M%S-%q
;picture_filename %Y%m%d%H%M-%q

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

# Create movies of motion events.
movie_output on

# Maximum length of movie in seconds.
movie_max_time 20

# 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
;movie_filename %t-%v-%Y%m%d%H%M

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

# Port number used for the webcontrol.
webcontrol_port 8080

# Restrict webcontrol connections to the localhost.
webcontrol_localhost on

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

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

# The port number for the live stream.
stream_port 8081

# Restrict stream connections to the localhost.
stream_localhost on

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

##############################################################
# Directory to read '.conf' files for cameras.
##############################################################
; camera_dir /usr/etc/motion/conf.d

Camera1.conf

Make a file called camera1.conf.  Set the picture size according to your camera resolution.

Set the USERNAME and PASSWORD to the RTSP stream account that you created in the camera App.

# /usr/etc/motion/camera1.conf
#
# This config file was generated by motion 4.3.2

###########################################################
# Configuration options specific to camera 1
############################################################
# User defined name for the camera.
camera_name CAM1

# Numeric identifier for the camera.
camera_id 101

# The full URL of the network camera stream.
netcam_url rtsp://USERNAME:PASSWORD@10.10.10.78/stream1

# Image width in pixels.
;width 1280
width 1920

# Image height in pixels.
;height 720
height 1080

# Text to be overlayed in the lower left corner of images
text_left CAM1

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

Ditto for Camera 2.

Motion Configuration Tricks

Enable the Despeckle filter to reduce the amount of motion events. Set the maximum length of the movie clip to 20 seconds, to keep the file smallish and ensure that the email system will accept it.  

Remove the seconds parameter from the file names, since the system is already processing the next event when the current event is handled, which makes it impossible to attach the correct file down to the second.  Sometimes there will still be a problem when the minutes roll over, but most video files will get sent successfully.

Always specify the complete filename path.

Email Test

First test mutt interactively.  Run it from the console and play with it to ensure that you can send and receive email and that gmail is not blocking it.  

Motion Test

Run motion from the command line and play with it, to ensure that it works properly and will send emails with mutt.  If you get too many messages, increase the pixel change trigger level.

You need to find a happy medium where the system will not email a deluge of messages, since Google will block the account if you do. If you find that there is just too much movement in your situation, then you could also use the snapshot feature to email a picture once per hour or so and leave the movies on your home server.

Another option is to make the event gap much longer, so that it will detect motion, but not all motion.

You can view the real time video with a web browser on port 8081:

  • $ firefox http://localhost:8081/101 

Mailbox Cleanup

Deleting a few thousand messages manually using gmail with a webbrowser, would be extremely tedious.  When the mailbox gets full, it may be easier to delete the account and create a new one.

To clean the gmail box in one swell foop, you can try the following command with mutt:
  • mutt -e "push D~d>30d<enter>qy<enter>"
However - it may cause Google to disable your mail account for an hour, since this kind of mass mail delete with the IMAP protocol may look suspicious to their system.


La voila!

Herman


Comments

  1. Very cool post on Linux cameras. I can attest to it's utility. I set up a Linux security system on a remote solar powered system on my farm using an old Raspberry Pi and USB webcams. I have motion configured to take images periodically and when triggered by movement. The Linux system been sending me an email with photos attached every hour for about 8 years now without fail.

    ReplyDelete
  2. Home and Away routines can turn your camera off when you’re at home so that no video is streamed or recorded until you leave.

    ¹ Up to 3 hours recording. Learn more at g.co/nest/3hours.
    ² Camera is weather resistant. Some features, including mobile notifications, remote control, video streaming and video recording, require working internet and Wi-Fi.
    Google Nest Cam

    ReplyDelete

Post a Comment

On topic comments are welcome. Junk will be deleted.

Popular posts from this blog

Parasitic Quadrifilar Helical Antenna

This article was reprinted in OSCAR News, March 2018:  http://www.amsat-uk.org If you want to receive Satellite Weather Pictures , then you need a decent antenna, otherwise you will receive more noise than picture. For polar orbit satellites, one needs an antenna with a mushroom shaped radiation pattern .  It needs to have strong gain towards the horizon where the satellites are distant, less gain upwards where they are close and as little as possible downwards, which would be wasted and a source of noise.  Most satellites are spin stabilized and therefore the antenna also needs circular polarization, otherwise the received signal will flutter as the antennas rotate through nulls. The helical antenna, first proposed by Kraus in 1948, is the natural solution to circular polarized satellite communications.  It is a simple twisted wire - there seems to be nothing to it.  Various papers have been published on helix antennas, so the operation is pretty well understood. Therefore,

Weather Satellite Turnstile Antennas for the 2 meter Band

NEC2, 2 m band, 146 MHz, Yagi Turnstile Simulation and Build This article describes a Turnstile Antenna for the 2 meter band, 146 MHz amateur satcom, 137 MHz NOAA and Russian Meteor weather satellites.  Weather satellite reception is described here .  A quadrifilar helical antenna is described here .   Engineering, is the art of making what you need,  from what you can get. Radiation Pattern of the Three Element Yagi-Uda Antenna Once one combine and cross two Yagis, the pattern becomes distinctly twisted. The right hand polarization actually becomes visible in the radiation pattern plot, which I found really cool. Radiation Pattern of Six Element Turnstile Antenna Only a true RF Geek can appreciate the twisted invisible inner beauty of a herring bone antenna... Six Element Turnstile Antenna Essentially, it is three crosses on a stick.  The driven elements are broken in the middle at the drive points.  The other elements can go straight throug

Patch Antenna Design with NEC2

The older free Numerical Electromagnetic Code version 2 (NEC2) from Lawrence Livermore Lab assumes an air dielectric.  This makes it hard (but not impossible) for a radio amateur to experiment with Printed Circuit Board Patch antennas and micro strip lines. Air Spaced Patch Antenna Radiation Pattern You could use the free ASAP simulation program , which handles thin dielectrics, you could shell out a few hundred Dollars for a copy of NEC4 , You could buy GEMACS if you live in the USA, or you could add distributed capacitors to a NEC2 model with LD cards (hook up one capacitor in the middle of each element.), but that is far too much money/trouble for most. More information on driving an array antenna can be found here: https://www.aeronetworks.ca/2019/03/driving-quad-patch-array-antenna.htm l Air Dielectric Patch   The obvious lazy solution is to accept the limitation and make an air dielectric patch antenna. An advantage of using air dielectric, is that the antenn