How to implement an alarm system with Asterisk and a webcam

(c) 2007 Servitux Servicio Informáticos S.L.

This document is GPL.

This simple manual explains how to run an alarm system using Asterisk PBX software and a webcam, under Debian Linux 4.0. The Asterisk and any other software instalation is not detailed, only the configuration.


This alarm system can be educational and fun to implement, but it should not be used as a single system of protection. Servitux Servicios Informáticos S.L. Is not responsible in any way for any mishap for using this system.


Motion settings:

Once the webcam is working, must edit the file /etc/motion/motion.conf to looks like:

daemon on
quiet on
width 640
height 480
framerate 25
quality 85
auto_brightness on
brightness 0
contrast 0
saturation 0
hue 0
webcam_localhost off
webcam_quality 30
webcam_maxrate 6
on_motion_detected /usr/local/bin/
text_right %Y-%m-%d\n%T
text_left SERVITUX CAM
webcam_port 8000
webcam_motion on
minimum_motion_frames 9
on_picture_save wput -b -q

The most important thing to emphasize is:

  • on_motion_detected : This command will be executed when the cammera detects movement;
  • on_picture_save : This command will be executed when the webcam generated images are stored when one movement is detected. For your example, the images will be stored on a remote ftp server. You must have to install the wput application using apt-get if it not already installed.

Script event management

This script is responsible for making a call through asterisk when it detects movement:


# first, play one audio file on your speakers
for i in `seq 1 15` ; do play /usr/local/bin/alarma.wav ; done

# making the call
cat << EOF > /tmp/
Channel: SIP/Provider/687XXXXXX
Callerid: 966160600
MaxRetries: 2
RetryTime: 20
WaitTime: 20
Context: mensajealarma
Extension: s
Priority: 1
chown asterisk:asterisk /tmp/
mv /tmp/ /var/spool/asterisk/outgoing/

Remember to change this file according to your needs. The played audio file on your speakers have two functions: scaring the potential intruder, and give us time to turn off the alarm when entering the office. If you do not want to reproduce audio, put for example a "sleep 60", this would leave us 60 seconds to turn off the alarm and not make the call.

Configuring the Asterisk dialplan.

Now the asterisk settings. The configuration consists of one command to activate the alarm, one commando to turn it off and actions to jump the alarm.

; context for the extensions, in example, default or from-internal
exten => *666,1,Answer
exten => *666,n,Wait(1)
exten => *666,n,Playback(activated)
exten => *666,n,Wait(120)
exten => *666,n,System(/usr/local/bin/ start)
exten => *666,n,Wait(1)
exten => *666,n,Hangup()

exten => *777,1,Answer
exten => *777,n,Wait(1)
exten => *777,n,System(/usr/local/bin/ stop)
exten => *777,n,Playback(de-activated)
exten => *777,n,Wait(1)
exten => *777,n,Hangup()

exten => s,1,Set(LANGUAGE()=es)
exten => s,n,Answer
exten => s,n,Wait(2)
exten => s,n,Playback(activated)
exten => s,n,Wait(1)
exten => s,n,Playback(activated)
exten => s,n,Wait(1)
exten => s,n,Playback(activated)
exten => s,n,Wait(1)
exten => s,n,Playback(activated)
exten => s,n,Wait(1)
exten => s,n,Playback(activated)
exten => s,n,Wait(1)
exten => s,n,Hangup


  • *666 is the code used to activate the alarm. The system will wait for 120 seconds and later active it, we can close and go out from the office;
  • *777 is the code used to activate the alarm;
  • the context mensajealarma is executed when the system make the phone call.

on/off script

This scrip will start the motion program.


case $1 in
sudo /usr/bin/motion
PID=`pidof motion`
sudo kill $PID
sudo killall
sudo rm -f /var/spool/asterisk/outgoing/

In conclusion

On this basis, you can change everything you need to adjust to your needs. You can make the motion to send and email, or that the asterisk send a SMS (if your provider allows it) instead of a call.

Shomething that I do not know whether we could make is that the asterisk can make a video call to a mobile phone and see live what happens in the office via cellphone, something like this:

PC + Softphone + Webcam ----- Asterisk ------ Mobile phone

If you have a phone with "intercom" support, you can use this feature to call to the office when the alarm is activated, and hear directly what is going on and talk with the "visitors" ;)

English translation by ==RazaMetaL==
Created by: razametal, Last modification: Mon 31 of Dec, 2007 (12:50 UTC) by PieterB
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+