Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ You can view a live stream by visiting the ip address of your pi in a browser on

Visit `<raspberrypi_ip>:5000` in your browser to view the stream.

The default password is pi/securehome. To change the credentials, please modify `auth.py`

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.
25 changes: 25 additions & 0 deletions auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from functools import wraps
from flask import request, Response


def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == 'pi' and password == 'securehome'

def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})

def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
5 changes: 4 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from camera import VideoCamera
import time
import threading
from auth import requires_auth

email_update_interval = 600 # sends an email only once in this time interval
video_camera = VideoCamera(flip=True) # creates a camera object, flip vertically
Expand All @@ -28,6 +29,7 @@ def check_for_objects():
print "Error sending email: ", sys.exc_info()[0]

@app.route('/')
@requires_auth
def index():
return render_template('index.html')

Expand All @@ -38,6 +40,7 @@ def gen(camera):
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.route('/video_feed')
@requires_auth
def video_feed():
return Response(gen(video_camera),
mimetype='multipart/x-mixed-replace; boundary=frame')
Expand All @@ -46,4 +49,4 @@ def video_feed():
t = threading.Thread(target=check_for_objects, args=())
t.daemon = True
t.start()
app.run(host='0.0.0.0', debug=False)
app.run(threaded=True, host='0.0.0.0', debug=False)