Upgrade 3CX to v18 and get it hosted free!

How to implement an alarm system with Asterisk and a webcam

Author image

(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.

==IMPORTANT==

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.

Requisites:

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/cam_event.sh
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 ftp://user:[email protected]

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 cam_event.sh

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

#!/bin/sh

# 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/alarmevent.call
Channel: SIP/Provider/687XXXXXX
Callerid: 966160600
MaxRetries: 2
RetryTime: 20
WaitTime: 20
Context: mensajealarma
Extension: s
Priority: 1
EOF
chown asterisk:asterisk /tmp/alarmevent.call
mv /tmp/alarmevent.call /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/control_motion.sh 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/control_motion.sh stop)
exten => *777,n,Playback(de-activated)
exten => *777,n,Wait(1)
exten => *777,n,Hangup()

[mensajealarma]
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

Explanation:

  • *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 control_motion.sh

This scrip will start the motion program.

#!/bin/sh

case $1 in
start)
sudo /usr/bin/motion
;;
stop)
PID=`pidof motion`
sudo kill $PID
sudo killall cam_event.sh
sudo rm -f /var/spool/asterisk/outgoing/alarmevent.call
;;
esac

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.

Something 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==

Related Posts:

Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.