Last active
August 29, 2015 13:57
-
-
Save dvas0004/9450845 to your computer and use it in GitHub Desktop.
web interface for raspi CC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask import render_template | |
from flask import redirect, url_for | |
import dhtreader | |
import cPickle as pickle | |
from subprocess import call | |
app = Flask(__name__) | |
@app.route('/toggle') | |
def toggle(): | |
try: | |
with open('/home/pi/flask/status.pickle', 'r') as f: | |
status = pickle.load(f) | |
except: | |
status = "Unarmed" | |
if status == "Unarmed": | |
status = "Armed" | |
call(["pkill","mjpg_stream"]) | |
call(["pkill","raspistill"]) | |
call(["supervisorctl","start","pir"]) | |
else: | |
status = "Unarmed" | |
call(["supervisorctl","stop","pir"]) | |
with open('/home/pi/flask/status.pickle', 'w') as f: | |
pickle.dump(status, f) | |
return redirect(url_for('index')) | |
@app.route('/securityFeedOff') | |
def securityFeedOff(): | |
try: | |
with open('/home/pi/flask/feedStatus.pickle', 'r') as f: | |
feedStatus = pickle.load(f) | |
except: | |
feedStatus = "off" | |
if feedStatus == "on": | |
feedStatus = "off" | |
call(["pkill","mjpg_stream"]) | |
call(["pkill","raspistill"]) | |
with open('/home/pi/flask/feedStatus.pickle', 'w') as f: | |
pickle.dump(feedStatus, f) | |
return redirect(url_for('index')) | |
@app.route('/securityFeedOn') | |
def securityFeedOn(): | |
try: | |
with open('/home/pi/flask/status.pickle', 'r') as f: | |
status = pickle.load(f) | |
except: | |
status = "Unarmed" | |
try: | |
with open('/home/pi/flask/feedStatus.pickle', 'r') as f: | |
feedStatus = pickle.load(f) | |
except: | |
feedStatus = "off" | |
if ((status == "Unarmed") & (feedStatus== "off")): | |
feedStatus = "on" | |
call(["/home/pi/LiveSecurityCam.sh"]) | |
elif ((status == "Armed") & (feedStatus== "off")): | |
status = "Unarmed" | |
call(["supervisorctl","stop","pir"]) | |
call(["/home/pi/LiveSecurityCam.sh"]) | |
feedStatus = "on" | |
with open('/home/pi/flask/status.pickle', 'w') as f: | |
pickle.dump(status, f) | |
with open('/home/pi/flask/feedStatus.pickle', 'w') as f: | |
pickle.dump(feedStatus, f) | |
html = """<html> | |
<img style="width:500px;" src="http://192.168.168.247:8080/?action=stream" /> | |
<h3><a href="./securityFeedOff">Turn Security Feed off</a></h3> | |
</html>""" | |
return html | |
@app.route('/') | |
def index(): | |
#try: | |
with open('/home/pi/flask/sensors.pickle') as f: | |
temp, humid = pickle.load(f) | |
try: | |
with open('/home/pi/flask/status.pickle', 'r') as f: | |
status = pickle.load(f) | |
except: | |
status = "Unarmed" | |
temp="{0:.2f}".format(temp) | |
humid="{0:.2f}".format(humid) | |
#except: | |
# temp = 0 | |
# humid = 0 | |
return render_template('index.html',temp=temp,humid=humid,status=status) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0',debug=True,port=80) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment