from machine import Pin, Timer import time import wifi, settings import usocket as socket import select print("Adam Pressel") print("HTMAA 2023 Final Project") print("Smart Doorknob") ############################################################# # Initialize GPIO led = Pin("LED", Pin.OUT) b1 = Pin(15, Pin.IN, Pin.PULL_UP) b2 = Pin(14, Pin.IN, Pin.PULL_UP) b3 = Pin(13, Pin.IN, Pin.PULL_UP) b4 = Pin(12, Pin.IN, Pin.PULL_UP) b5 = Pin(11, Pin.IN, Pin.PULL_UP) b6 = Pin(10, Pin.IN, Pin.PULL_UP) b7 = Pin(9, Pin.IN, Pin.PULL_UP) b8 = Pin(8, Pin.IN, Pin.PULL_UP) b9 = Pin(7, Pin.IN, Pin.PULL_UP) b10 = Pin(6, Pin.IN, Pin.PULL_UP) b11 = Pin(1, Pin.IN, Pin.PULL_UP) b12 = Pin(0, Pin.IN, Pin.PULL_UP) LED_R = Pin(3, Pin.OUT) LED_W = Pin(2, Pin.OUT) db = Pin(22, Pin.OUT) # Global Variable Declaration button_pressed = False button_value = 0 cypher = "" count = 0 to_unlock = False # Initialize Timer reset_tim = Timer() deadbolt_tim = Timer() timer_count = 0 # global variable def reset_handler(): global cypher global count count += 1 print("timer count: ", count) if cypher != "": LED_R.off() for x in range(6): LED_R.toggle() time.sleep_ms(settings.FLASH_TIME) cypher = "" LED_R.off() print("cypher reset") def db_handler(): db.off() def unlock(deadbolt_tim): db.on() print("UNLOCK UNLOCK UNLOCK") deadbolt_tim.init( period=settings.BOLT_TIME, mode=Timer.ONE_SHOT, callback=lambda t: db_handler(), ) LED_W.on() for x in range(10): LED_R.toggle() LED_W.toggle() time.sleep_ms(settings.FLASH_TIME) LED_W.off() LED_R.off() def initializing(): for x in range(10): LED_W.toggle() time.sleep_ms(settings.FLASH_TIME) LED_W.off() # Initialize WiFi wlan = wifi.initialize(settings.SSID, settings.PASS) if wlan.isconnected(): for x in range(6): LED_W.toggle() time.sleep_ms(settings.FLASH_TIME) else: for x in range(6): LED_R.toggle() time.sleep_ms(settings.FLASH_TIME) LED_R.off() LED_W.off() db.off() return wlan def open_socket(ip): address = (ip, 80) connection = socket.socket() connection.bind(address) connection.listen(1) poll = select.poll() poll.register(connection, select.POLLIN) return connection, poll def webpage(state, unlock_request): # Template HTML html = f"""

Lock is: {state}

Lock Action: {unlock_request}

""" return str(html) def serve(connection, poll, to_unlock): # print("serve enter") events = poll.poll(100) print(events) if events: state = "off" print(str(connection)) client = connection.accept()[0] request = client.recv(1024) request = str(request) unlock_request = "NOT REQUESTED" print(request) try: request = request.split()[ 1 ] # second member of the array, split at each split except IndexError: pass if request == "/lock?": to_unlock = False state = "LOCKED" elif request == "/unlock?": state = "UNLOCKED" to_unlock = True if button_value == 11: print("UNLOCK REQUESTED FROM SERVER") unlock_request = "REQUESTED" elif button_value != 11: unlock_request = "NOT REQUESTED" html = webpage(state, unlock_request) client.send(html) print(request) client.close() return to_unlock # print("exit") # Set Up wlan = initializing() ip = wlan.ifconfig()[0] print(f"Connected on {ip}") connection, poll = open_socket(ip) print(str(connection)) while True: to_unlock = serve(connection, poll, to_unlock) # Polling if button_pressed == False: if not b1.value(): LED_W.on() button_pressed = True button_value = 1 if not b2.value(): LED_W.on() button_pressed = True button_value = 2 if not b3.value(): LED_W.on() button_pressed = True button_value = 3 if not b4.value(): print("pressed 4") LED_W.on() button_pressed = True button_value = 4 if not b5.value(): LED_W.on() button_pressed = True button_value = 5 if not b6.value(): LED_W.on() button_pressed = True button_value = 6 if not b7.value(): LED_W.on() button_pressed = True button_value = 7 if not b8.value(): LED_W.on() button_pressed = True button_value = 8 if not b9.value(): LED_W.on() button_pressed = True button_value = 9 if not b10.value(): LED_W.on() button_pressed = True button_value = 0 if not b11.value(): LED_W.on() button_pressed = True button_value = 11 if not b12.value(): LED_W.on() button_pressed = True button_value = 12 elif ( b1.value() and b2.value() and b3.value() and b4.value() and b5.value() and b6.value() and b7.value() and b8.value() and b9.value() and b11.value() and b10.value() and b12.value() ): LED_W.off() print("\n") print("Pressed: ", button_pressed) print("Value: ", button_value) if not (button_value == 11 or button_value == 12): cypher = cypher + str(button_value) button_pressed = False print("Cypher", cypher) print("restarted reset timer") reset_tim.init( period=settings.RESET_TIME, mode=Timer.ONE_SHOT, callback=lambda t: reset_handler(), ) if cypher == settings.SAFEKEY and button_value == 12: unlock(deadbolt_tim) cypher = "" if to_unlock: unlock(deadbolt_tim) to_unlock = False