-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_tornado_input.py
More file actions
63 lines (46 loc) · 1.41 KB
/
11_tornado_input.py
File metadata and controls
63 lines (46 loc) · 1.41 KB
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
from tornado.web import RequestHandler, Application
from tornado.ioloop import IOLoop, PeriodicCallback
from RPi import GPIO
last_test = 0
def test_night():
global last_test
night = GPIO.input(22)
if night and not last_test:
GPIO.output(4, 1)
if not night and last_test:
GPIO.output(4, 0)
last_test = night
class Handler(RequestHandler):
def get(self):
self.write(
"<head>"
" <meta name='viewport'"
" content='width=device-width,"
" height=device-height'>"
"</head>"
"<form action='/' method='post'>"
" <input type='submit' name='encender'"
" value='Encender' />"
" <input type='submit' name='apagar'"
" value='Apagar' />"
"</form>"
)
def post(self):
encender = self.get_argument('encender', '')
apagar = self.get_argument('apagar', '')
if encender and not apagar:
GPIO.output(4, 1)
if not encender and apagar:
GPIO.output(4, 0)
self.get()
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(22, GPIO.IN)
Application([('/$', Handler)]).listen(50000)
PeriodicCallback(test_night, 100).start()
IOLoop.instance().start()
except KeyboardInterrupt:
exit()
finally:
GPIO.cleanup()