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}