forked from craguila/RaspberryPi_LightSwitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
41 lines (30 loc) · 990 Bytes
/
server.py
File metadata and controls
41 lines (30 loc) · 990 Bytes
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
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, Application
import RPi.GPIO as GPIO
def main():
"""Execute the application."""
try:
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT)
routes = [('/', ListHandler), ('/onoff', OnOff)]
app = Application(routes, debug=False)
app.listen(80)
IOLoop.instance().start()
except KeyboardInterrupt:
exit()
class ListHandler(RequestHandler):
def get(self):
"""Render the luz.html file."""
self.render('luz.html')
def post(self):
"""Change the pin 3 state and reload the page."""
GPIO.output(3, not GPIO.input(3))
self.get()
class OnOff(RequestHandler):
"""Allows the user to have a direct access to the relay."""
def get(self):
"""Show an empty page but changes the relay state."""
GPIO.output(3, not GPIO.input(3))
self.write('')
if __name__ == "__main__":
main()