-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·154 lines (136 loc) · 6.08 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import usocket
from machine import Pin
from os import listdir
from ujson import dump, load
from time import ticks_diff, ticks_ms, sleep
#Config of I/O Pins
'''
D1mini
bomba = Pin(5, Pin.OUT)
quintal = Pin(4, Pin.OUT)
scozinha = Pin(12, Pin.OUT)
quarto = Pin(14, Pin.OUT)
'''
#ESP03
bomba = Pin(14, Pin.OUT)
quintal = Pin(12, Pin.OUT)
scozinha = Pin(5, Pin.OUT)
quarto = Pin(4, Pin.OUT)
# TIMER
TIMER = 0
#Write in Json last states of Pins
def writelastcfg():
lastcfg = {'BOMBA': 0, 'QUINTAL': quintal.value(), 'SCOZINHA': scozinha.value(), 'QUARTO': quarto.value()}
with open('lastcfg.json', 'w') as f:
dump(lastcfg, f)
#Retrive last Pin states saved in Json
def getlastcfg():
try:
with open('lastcfg.json', 'r') as f:
lastcfg = load(f)
bomba.value(lastcfg['BOMBA'])
quintal.value(lastcfg['QUINTAL'])
scozinha.value(lastcfg['SCOZINHA'])
quarto.value(lastcfg['QUARTO'])
except:
bomba.value(0)
quintal.value(0)
scozinha.value(0)
quarto.value(0)
def ok(socket, query):
socket.write("HTTP/1.1 OK\r\n\r\n")
socket.write("<!DOCTYPE html>")
socket.write('<head><meta charset="utf-8"><link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">')
socket.write('<meta name="viewport" content="width=device-width, initial-scale=1" />')
socket.write("<title>Smart House</title></head>")
socket.write('<html lang="pt-br" dir="ltr"><body>')
socket.write('<div class="jumbotron" align="center"><h2>Smart House</h2><h5>By Henrique Miranda</h5>')
socket.write('<ul class="list-group">')
if bomba.value():
socket.write('<li class="list-group-item"><span>Bomba</span> <span style="color:green">Ligada</span> <form method="POST" action="/bombaoff?"'+query.decode()+'"><button type="submit" class="btn btn-danger">Desligar</button></form></li>')
else:
socket.write('<li class="list-group-item"><span>Bomba</span> <span style="color:red">Desligada</span> <form method="POST" action="/bombaon?"'+query.decode()+'"><button type="submit" class="btn btn-primary">Ligar</button></form></li>')
if quintal.value():
socket.write('<li class="list-group-item"><span>Quintal</span> <span style="color:green">Ligada</span> <form method="POST" action="/quintaloff?"'+query.decode()+'"><button type="submit" class="btn btn-danger">Desligar</button></form></li>')
else:
socket.write('<li class="list-group-item"><span>Quintal</span> <span style="color:red">Desligada</span> <form method="POST" action="/quintalon?"'+query.decode()+'"><button type="submit" class="btn btn-primary">Ligar</button></form></li>')
if scozinha.value():
socket.write('<li class="list-group-item"><span>Sala/Cozinha</span> <span style="color:green">Ligada</span> <form method="POST" action="/scozinhaoff?"'+query.decode()+'"><button type="submit" class="btn btn-danger">Desligar</button></form></li>')
else:
socket.write('<li class="list-group-item"><span>Sala/Cozinha</span> <span style="color:red">Desligada</span> <form method="POST" action="/scozinhaon?"'+query.decode()+'"><button type="submit" class="btn btn-primary">Ligar</button></form></li>')
if quarto.value():
socket.write('<li class="list-group-item"><span>Quarto</span> <span style="color:green">Ligada</span> <form method="POST" action="/quartooff?"'+query.decode()+'"><button type="submit" class="btn btn-danger">Desligar</button></form></li>')
else:
socket.write('<li class="list-group-item"><span>Quarto</span> <span style="color:red">Desligada</span> <form method="POST" action="/quartoon?"'+query.decode()+'"><button type="submit" class="btn btn-primary">Ligar</button></form></li></ul></div></body></html>')
def err(socket, code, message):
socket.write("HTTP/1.1 "+code+" "+message+"\r\n\r\n")
socket.write("<h1>"+message+"</h1>")
def handle(socket):
(method, url, version) = socket.readline().split(b" ")
if b"?" in url:
(path, query) = url.split(b"?", 2)
else:
(path, query) = (url, b"")
while True:
header = socket.readline()
if header == b"":
return
if header == b"\r\n":
break
if version != b"HTTP/1.0\r\n" and version != b"HTTP/1.1\r\n":
err(socket, "505", "Version Not Supported")
elif method == b"GET":
if path == b"/":
ok(socket, query)
else:
err(socket, "404", "Not Found")
elif method == b"POST":
if path == b"/bombaon":
bomba.value(1)
TIMER = ticks_ms()
ok(socket, query)
elif path == b"/bombaoff":
bomba.value(0)
ok(socket, query)
elif path == b"/quintalon":
quintal.value(1)
ok(socket, query)
elif path == b"/quintaloff":
quintal.value(0)
ok(socket, query)
elif path == b"/scozinhaon":
scozinha.value(1)
ok(socket, query)
elif path == b"/scozinhaoff":
scozinha.value(0)
ok(socket, query)
elif path == b"/quartoon":
quarto.value(1)
ok(socket, query)
elif path == b"/quartooff":
quarto.value(0)
ok(socket, query)
else:
err(socket, "404", "Not Found")
writelastcfg()
else:
err(socket, "501", "Not Implemented")
#When the board turn on get last Pin states from json file
if 'lastcfg.json' not in listdir():
writelastcfg()
getlastcfg()
server = usocket.socket()
server.bind(('', 80))
server.listen(1)
while True:
if bomba.value():
if ticks_diff(ticks_ms(), TIMER) >= 600000:
bomba.value(0)
TIMER = 0
try:
(socket, sockaddr) = server.accept()
handle(socket)
except:
socket.write("HTTP/1.1 500 Internal Server Error\r\n\r\n")
socket.write("<h1>Internal Server Error</h1>")
socket.close()