diff --git a/README.md b/README.md index 19860ed..83f743e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Smart-Security-Camera +# Smart-Security-Camera MQTT support IoT Raspberry Pi security camera running open-cv for object detection. The camera will send an email with an image of any objects it detects. It also runs a server that provides a live video stream over the internet. [Watch the original video here](https://youtu.be/Y2QFu-tTvTI) @@ -110,3 +110,20 @@ Visit `:5000` in your browser to view the stream. Note: To view the live stream on a different network than your Raspberry Pi, you can use [ngrok](https://ngrok.com/) to expose a local tunnel. Once downloaded, run ngrok with `./ngrok http 5000` and visit one of the generated links in your browser. Note: The video stream will not start automatically on startup. To start the video stream automatically, you will need to run the program from your `/etc/rc.local` file see this [video](https://youtu.be/51dg2MsYHns?t=7m4s) for more information about how to configure that. + +## MQTT + +Update following line with appropiate MQTT server, it can be any MQTT server + +``` +client.connect("192.168.0.xxx", 1883, 60) +``` + +Update the topics that will reveice the motion notification + +``` +client.publish("home/door/front/motion","ON",0,False) +client.publish("home/door/front/camera",frame,0,True) +``` + +The topic "home/door/front/camera" can be read as a MQTT camera stream https://home-assistant.io/components/camera.mqtt/ diff --git a/camera.py b/camera.py index 065a321..02c3545 100644 --- a/camera.py +++ b/camera.py @@ -6,7 +6,7 @@ class VideoCamera(object): def __init__(self, flip = False): - self.vs = PiVideoStream().start() + self.vs = PiVideoStream(resolution=(1280,720),framerate=30).start() self.flip = flip time.sleep(2.0) diff --git a/main.py b/main.py index fef7650..b32e1b9 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,21 @@ import cv2 import sys from mail import sendEmail +import paho.mqtt.client as mqtt from flask import Flask, render_template, Response from camera import VideoCamera import time import threading + +def on_connect(client, userdata, flags, rc): + print("Connected to MQTT server with result code :"+str(rc)) + +client = mqtt.Client() +client.on_connect = on_connect +client.connect("192.168.0.xxx", 1883, 60) # use your MQTT server name +client.loop_start() + email_update_interval = 600 # sends an email only once in this time interval video_camera = VideoCamera(flip=True) # creates a camera object, flip vertically object_classifier = cv2.CascadeClassifier("models/fullbody_recognition_model.xml") # an opencv classifier @@ -21,11 +31,14 @@ def check_for_objects(): frame, found_obj = video_camera.get_object(object_classifier) if found_obj and (time.time() - last_epoch) > email_update_interval: last_epoch = time.time() - print "Sending email..." + print("Sending email...") sendEmail(frame) - print "done!" + print("Sending MQTT message...") + client.publish("home/door/front/motion","ON",0,False) + client.publish("home/door/front/camera",frame,0,True) + print("done!") except: - print "Error sending email: ", sys.exc_info()[0] + print("Error sending email: ", sys.exc_info()[0]) @app.route('/') def index(): diff --git a/requirements.txt b/requirements.txt index a2ba31f..128965b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ imutils flask picamera[array] +paho-mqtt diff --git a/runserver.sh b/runserver.sh new file mode 100644 index 0000000..1897ebc --- /dev/null +++ b/runserver.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +HOME=/home/pi +VENVDIR=$HOME/.virtualenvs/cv +BINDIR=$HOME/Smart-Security-Camera + +cd $BINDIR +source $VENVDIR/bin/activate +python $BINDIR/main.py diff --git a/systemd/README.md b/systemd/README.md new file mode 100644 index 0000000..eae5ab0 --- /dev/null +++ b/systemd/README.md @@ -0,0 +1,3 @@ +1. Place picam.service inside /etc/systemd/system folder +2. Activate service sudo systemctl activate picam.service +3. Start the service sudo systemctl start picam.service diff --git a/systemd/picam.service b/systemd/picam.service new file mode 100644 index 0000000..8ce2200 --- /dev/null +++ b/systemd/picam.service @@ -0,0 +1,12 @@ +[Unit] +Description=Pi WebCam Service with CV +After=network.target + +[Service] +Type=simple +User=pi +ExecStart=/home/pi/Smart-Security-Camera/runserver.sh +Restart=always + +[Install] +WantedBy=multi-user.target