/
RasPyno-domo.py
101 lines (80 loc) · 2.26 KB
/
RasPyno-domo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import datetime
from flask import Flask, render_template, Blueprint, Response, request, json
from time import sleep
from nanpy import(ArduinoApi, SerialManager)
app = Flask(__name__)
led = 13
sensor = 24
swch = 12
ledState = False
swchState = 0
try:
connection = SerialManager()
ard = ArduinoApi(connection)
# pinMode setup for arduino
ard.pinMode(led, ard.OUTPUT)
ard.pinMode(swch, ard.INPUT)
ard.pinMode(sensor, ard.INPUT)
except:
print("Failed To Connect to Arduino")
@app.route('/')
def home():
try:
estadoLuz = ard.digitalRead(led)
temp = ard.analogRead(sensor)
except:
estadoLuz = "Debugging"
temp=69
print("derp")
time = datetime.datetime.now()
timeStr = time.strftime("%Y-%m-%d %H:%M")
temp = (5.0 * temp * 100.0) / 1024
if estadoLuz:
estadoLuz='ON'
else:
estadoLuz='OFF'
return render_template("home.html", estadoLuz=estadoLuz, timeStr=timeStr, temp=temp)
@app.route('/lights/')
def luces():
try:
estadoLuz = ard.digitalRead(led)
except:
estadoLuz = False
print("derp")
if estadoLuz:
estadoLuz='ON'
else:
estadoLuz='OFF'
return render_template("Lights.html", estadoLuz=estadoLuz)
@app.route('/temp/')
def temp():
temperatura = (5.0 * ard.analogRead(sensor) * 100.0) / 1024
return render_template("temperature.html", temperatura=temperatura)
@app.route('/toggle/')
def toggle_light():
print("Eh")
try:
if ard.digitalRead(led):
ard.digitalWrite(led, 0)
else:
ard.digitalWrite(led, 1)
except:
print("Failed to turn light on")
return "Luz"
app.jinja_env.globals.update(toggle_light=toggle_light)
#@app.route("/hello/")
#def hello():
# time = datetime.datetime.now()
# timeStr = time.strftime("%Y-%m-%d %H:%M")
# swchState = ard.digitalRead(swch)
# print("Switch State: {}".format(swchState))
# ard.digitalWrite(led,swchState)
#
# templateData = {
# 'title' : 'HELLO!',
# 'time' : timeStr,
# 'status' : swchState
# }
# return render_template('main.html', **templateData)
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 80, debug = True)