User Tools

Site Tools


Writing /var/lib/dokuwiki/data/meta/tutorials/assyra/alarm_security_system_with_raspberry_pi.meta failed
tutorials:assyra:alarm_security_system_with_raspberry_pi

This is an old revision of the document!


Assyra

Alarm Security System with Raspberry Pi

Created by:

  • Nohelia Alpízar
  • Guillermo Rivera

Introduction

This project is born from the necessity of an affordable article that gives users the possibility to monitor and get notice of any event that may happen in places that are important to prevent any vandalism or burglary.This article has to have a high performance and provide the option to check the cameras from any location through Internet. The Raspberry Pi was chosen because of its low cost and because the new camera module gives good images with high resolution.

Objectives

  • Create a connection between the Raspberry Pi and a computer that has ZoneMinder, in order to access the video from the camera from any location.
  • Create an script to initialize the video stream when the Raspberry Pi boots.
  • Ensure the robustness of the connection of the server and the camera (RPi) using “watchdogs” that check continuously that the server, camera, and streaming are working as expected.

Video transmission from the Raspberry Pi to the server

Initialization script for streaming

In first place, because the RPi has to function as camera all the time, a script was created to initialize the video streaming when it's turned on.

#!/bin/sh
###BEGIN INIT INFO
# Provides: Raspivid-netcat
# Required-Start:$remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Description: It starts the Raspberry Pi Camera using raspivid command
#			and sends it to a server using netcat directed to an especific
#			IP.
###END INIT INFO

. /lib/lsb/init-functions

RETVAL=0
prog="/etc/init.d/streaming"

start() {
                echo "Starting video streaming"
                /usr/bin/raspivid --nopreview -t 0 -o - | /bin/nc [server IP] 5001
                RETVAL=$?
                return $RETVAL
}

stop() {
                echo "Stopping streaming"
                killall raspivid
                RETVAL=$?
                return $RETVAL
}

restart() {
                stop
        	start
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                restart
                ;;
	status)
	        status_of_proc $prog
		RETVAL=$?
	        ;; 
  	*)
    	echo "Usage: /etc/init.d/streaming {start|stop|status|restart}"
        RETVAL=1
        ;;

esac
exit $RETVAL

This code has to be saved in the directory /etc/init.d as an executable that starts working when the system boots up.

chmod 755 /etc/init.d/streaming
update-rc.d streaming defaults

Video capture of the Raspberry Pi stream in the server

Initialization script for video capture

"Watchdogs" monitoring the entire system

Monitoring of the connection of the RPi with the server and the streaming

The monitoring in the RPi is achieved through a script written in Python that checks the connection with the server by doing a “ping” constantly. In case the “ping” is not possible, it reestablishes the connection to the network and sends a restart to the script that starts the video stream from the RPi camera. If the network is working fine, it just checks the status of the stream and repairs it if it's broken.

#!/usr/bin/env python
import subprocess
import os
import time

while True:
a = subprocess.call(['/bin/ping', '-c1', '[server IP]'])
	if(a != 0):{
		os.system('sudo ifdown eth0')
		b = os.system('sudo ifup eth0')
		if(b == 0):
			c = os.system('sudo /etc/init.d/streaming status')
			if(c != 0):
				os.system('sudo /etc/init.d/streaming restart')
			else:
				c == 0
		else:
			b != 0
	else:
		d = os.system('sudo /etc/init.d/streaming status')
			if(d != 0):
				os.system('sudo /etc/init.d/streaming restart')
			else:
				d == 0

Monitoring of the connection of the server with the Rpi and the capturing of the incoming video stream

This script checks if the server has connection with the network. If the connection fails, it reestablishes it and restarts the program that captures the video stream coming from the RPi. If the connection works, it just checks the status of the capture program and restarts it in case it's not working.

#!/usr/bin/env python
import subprocess
import os
import time

while True:
	a = subprocess.call(['/bin/ping', '-c1', '[RPi IP]'])
	if(a != 0):
		os.system('sudo ifdown wlan0') 
		b = os.system('sudo ifup wlan0')
		if(b == 0):
			c = os.system('sudo /etc/init.d/capturing status')
			if(c != 0):
				os.system('sudo /etc/init.d/capturing restart')
			else:
				c == 0
		else:
			b != 0
	else:
		d = os.system('sudo /etc/init.d/capturing status')
		if(d != 0):
			os.system('sudo /etc/init.d/capturing restart')
		else:
			d == 0

Results

Conclusions

References

tutorials/assyra/alarm_security_system_with_raspberry_pi.1456509315.txt.gz · Last modified: 2022/09/20 00:08 (external edit)