Videovigilancia MMS

De Open movilforum wiki

Programa en PHP para enviar una imagen o vídeo a nuestro teléfono mediante MMS cuando se haya detectado movimiento mediante el programa motion (para Linux)

Videovigilancia MMS utiliza las APIs de envio de MMS mediante HTTP.

A continuación podéis seguir las instrucciones necesarias y descargar el código necesario para tener un sistema de video-vigilancia casero con una webcam (aunque valdría también para cámaras IP y para cualquier señal de video que se pueda conectar mediante la interfaz video4linux)

Las instrucciones están basadas en ubuntu, aunque se pueden extrapolar fácilmente a otros sistemas basados en Debian u otras distribuciones de Linux.

Estos son los pasos:

1. Instalar motion en ubuntu:

sudo apt-get install motion

2. Configurar motion (modificando los archivos de /etc/motion/motion.conf y /etc/motion/thread0.conf) Podéis utilizar éstos como ejemplo

Archivo motion.conf:

# Minimal motion example config file provided by the
# Debian motion package - for basic webcam operation.
#
# You most certainly want to investigate
# /usr/share/doc/motion/examples/motion-dist.conf.gz
# for further configuration options. Also, refer to the
# motion man page and /usr/share/doc/motion/motion_guide.html
# for detailed information on configuration options.

daemon on
quiet on

# You may very well need to change this (check with 'dmesg'
# after plugging in your webcam).
videodevice /dev/video0

# Image size in pixels (valid range is camera dependent). 
width 320
height 240

noise_level 64

# Initial brightness, contrast, hue (NTSC), and saturation.
# 0 = disabled (valid range 0-255).
brightness 0
contrast 0
saturation 0
hue 0

# Encode movies in real-time (install ffmpeg before enabling).
ffmpeg_cap_new off 

# Codec to be used by ffmpeg for the video compression.
# Supported formats: mpeg4, msmpeg4.
ffmpeg_video_codec msmpeg4

# Target base directory for pictures and films (you may need
# to change this (or change its permissions) depending on
# which system user runs motion).
target_dir /var/lib/motion/snapshots

# Define a port number (e.g. 8000) to enable the mini-http server.
# 0 = disabled. 
webcam_port 0

# Set to 'off' to allow anybody (not just localhost) to view the
# webcam via the mini-http server (http://hostname:port).
webcam_localhost on

webcam_quality 50
webcam_maxrate 8

# Solamente guardamos la mejor imagen (la que mas cambios tiene)
output_normal best

# Al guardar cada imagen, enviamos un email
on_picture_save /usr/local/bin/surveillanceMMS.php "%Y-%m-%d %T"

Archivo thread0.conf:

# /etc/motion/thread0.conf
#
# File path for motion triggered images (jpeg or ppm) relative to target_dir
# Default: %v-%Y%m%d%H%M%S-%q
# Default value is equivalent to legacy oldlayout option
# For Motion 3.0 compatible mode choose: %Y/%m/%d/%H/%M/%S-%q
# File extension .jpg or .ppm is automatically added so do not include this
# Set to 'preview' together with best-preview feature enables special naming
# convention for preview shots. See motion guide for details
jpeg_filename gt-%v-%Y%m%d%H%M%S-%q

3. Agregar motion al proceso de inicio:

sudo cp /usr/share/doc/motion/examples/motion.init-Debian  /etc/init.d/motion
sudo chmod 755 /etc/init.d/motion
sudo update-rc.d motion defaults

4. Crear un script de videovigilancia mediante MMS

Requiere php5-cli, php5-common, php5-curl:

#!/usr/bin/php

<?php

include "APIMMS.php"; 

set_time_limit(600);

// Obtenemos los parámetros necesarios
$time = $argv[1];
$attachment = $argv[2];

$mms = new MensajeriaMultimediaWeb();

$log = "NNNNNNNNN";	// MSISDN
$passw = "XXXXXXX";	// password de acceso a la web de copiagenda

$user = $mms->Login($log, $passw);
if($user == "")
{
    print "Error de Login";
}
else
{
    // Determinamos el tipo de fichero que vamos a adjuntar 

    $finfo = finfo_open(FILEINFO_MIME); // return mime type ala mimetype extension
    $ftype = finfo_file($finfo, $attachment);
    finfo_close($finfo);

    $ftype_parts = split('/', $ftype);

    switch($ftype_parts[0]) {
          case 'image':
              $nombreImg = "foto"; // nombre de la imagen que vamos a insertar
              $pathImg = $attachment; // path de la imagen que vamos a insertar
              $mms->InsertaImagen($nombreImg, $pathImg);
              break;
          case 'video':
	      $nombreVid = "video"; // nombre del vídeo que vamos a insertar
	      $pathVid = $attachment; // path del vídeo que vamos a insertar
	      $mms->InsertaVideo($nombreVid, $pathVid);
              break;
          default:
              die('invalid file type');
    }

    $dest = "NNNNNNNNN"; // destinatario del mensaje
    $subject = "Motion detected"; // asunto del mensaje
    $msg = "Motion has been detected at " . $time; // texto del mensaje
    $mms->EnviaMensaje($subject, $dest, $msg);

    $mms->Logout();
}
?>

5. Crear script para vigilancia por email (opcional)