Skip to content

Instantly share code, notes, and snippets.

@patrickxia
Created January 26, 2020 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickxia/e05eeb32ee01c0d1df59c53e7a60994a to your computer and use it in GitHub Desktop.
Save patrickxia/e05eeb32ee01c0d1df59c53e7a60994a to your computer and use it in GitHub Desktop.
Initial code
import http.server
import socketserver
import RPi.GPIO as GPIO
import time
PORT = 31234
chan = 7
doDoor = False
uuid = 'some-random-uuid-token-here'
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 not self.path.startswith('/' + uuid):
self.send_response(403)
self.end_headers()
return
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(b"[]")
global doDoor
doDoor = True
GPIO.setmode(GPIO.BCM)
GPIO.setup(chan, GPIO.OUT)
GPIO.output(chan, GPIO.LOW)
Handler = MyHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
while True:
doDoor = False
GPIO.output(chan, GPIO.LOW)
httpd.handle_request()
if doDoor:
print("Actuating door...")
time.sleep(1)
GPIO.output(chan, GPIO.HIGH)
time.sleep(7)
GPIO.output(chan, GPIO.LOW)
print("Door done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment