Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
door-open-v2.py
import http.server
import socketserver
import RPi.GPIO as GPIO
import time
PORT = 31234
chan = 7
monitor = 14
doSleep = False
uuid = 'still-secret'
GPIO.setmode(GPIO.BCM)
GPIO.setup(chan, GPIO.OUT)
GPIO.output(chan, GPIO.LOW)
GPIO.setup(monitor, GPIO.IN, pull_up_down=GPIO.PUD_UP)
class MyHandler(http.server.SimpleHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
def do_GET(self):
if GPIO.input(monitor) != 1:
print("Door not in expected state -- possible wedged relay")
self.send_response(503)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(b'{"error": "door not in expected state -- possible wedged relay"}')
return
if self.path.startswith('/healthz'):
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(b'ok')
return
if not self.path.startswith('/' + uuid):
self.send_response(403)
self.end_headers()
return
global doSleep
print("Doing door...")
GPIO.output(chan, GPIO.HIGH)
time.sleep(0.5)
if GPIO.input(monitor) != 0:
GPIO.output(chan, GPIO.LOW)
print("Door actuation failed -- disconnected?")
self.send_response(502)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(b'{"error": "door actuation failed -- possibly disconnected"}')
return
print("Done door.")
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(b'{"error": null}')
doSleep = True
Handler = MyHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
while True:
doSleep = False
GPIO.output(chan, GPIO.LOW)
httpd.handle_request()
if doSleep:
time.sleep(7)
print("Door done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.