-
Notifications
You must be signed in to change notification settings - Fork 0
/
GarageDoor.py
52 lines (42 loc) · 1.49 KB
/
GarageDoor.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
#!/usr/bin/python
## SET YOUR TOKEN HERE (uuidgen on Linux)
token = "50060667-7fad-4d9d-8b06-8d62a8012fad"
import json
import RPi.GPIO as GPIO
import time
import ssl
from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
self.timeout = 30
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
self.request.settimeout(30)
response = BytesIO()
response.write(body)
receivedtoken = response.getvalue()
resp_dict = json.loads(receivedtoken)
resptoken = resp_dict['token']
if token == resptoken:
GPIO.setmode(GPIO.BCM)
# Set GPIO Pin Here (I Used 17)
pin = 17
# Set up GPIO
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
# Set Time in Seconds for Door to Open / Close
DoorTimeToClose = 12
# Initiate Door Toggle
GPIO.output(pin, GPIO.LOW)
print ("Toggling Garage Door..")
time.sleep(DoorTimeToClose);
# Reset
GPIO.cleanup()
httpd = HTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket,
keyfile="/home/pi/CurtBrazKey2.pem",
certfile='/home/pi/CurtBrazCert.pem', server_side=True)
httpd.serve_forever()