print("Debug")
import machine
import utime
from pico_i2c_lcd import I2C_LCD
from memb_keypad import KEYPAD
from wifiesp import ESP
##############################
# Constants
# I2C Address (wokwi-lcd1602 specific)
LCD_ADDR = 0x27
# Pins connected
SCL_PIN = machine.Pin(19) # refers to GP17 - I2C0 SCL
SDA_PIN = machine.Pin(18) # refers to GP16 - I2C0 SDA
# Refresh Interval (per second)
REFRESH = 1
# Temporary fixed pin_code
PIN_CODE = "ABCD"
DEBUG = True
device_id = 8266
poll_rate = 30
mac = "00:00:6C:00:00:01"
hostname = "beginner"
target = "your.ip.addr.here"
ssid = "Wokwi-QUEST"
password = ""
##############################
# Settings
# Initialize I2C connection and store it to variable
i2c = machine.I2C(1, scl=SCL_PIN, sda=SDA_PIN)
# Some I2C implementations require "frequency" parameter e.g. 100000 or 400000 (hz)
i2c_lcd = I2C_LCD(i2c, LCD_ADDR, 2, 16)
# WIFI Config
wifi_conn = ESP(uart=0, baud=115200, txPin=0, rxPin=1, debug=DEBUG)
wifi_conn.setMAC(mac)
wifi_conn.setHostname(hostname)
wifi_conn.connectAP(ssid=ssid, pwd=password)
##############################
# Functions
def debug_log(msg):
if DEBUG == True:
print(msg)
return None
def lcd_write(message):
debug_log(message)
return None
#region LOCK LOGIC
def showPin(currentPin):
i2c_lcd.clear()
i2c_lcd.move_to(0, 0)
i2c_lcd.putstr(" Insert Pin(#): ")
i2c_lcd.move_to(0, 1)
i2c_lcd.putstr(" ")
for i in range(len(currentPin)):
i2c_lcd.putstr("* ")
return None # return pin code
LoadingStatus = 0
def showLoading():
global LoadingStatus
if LoadingStatus == 3:
LoadingStatus = 1
else:
LoadingStatus += 1
i2c_lcd.clear()
i2c_lcd.move_to(0, 0)
i2c_lcd.putstr("Loading" + ("." * LoadingStatus))
i2c_lcd.move_to(0, 1)
utime.sleep(1)
return None
def checkCode(current_pin):
for i in range(3):
showLoading()
OK = False
# TODO Check pincode from cloud
_pin = PIN_CODE.rstrip("#")
print("Current pin: " + _pin)
if current_pin.rstrip("#") == PIN_CODE:
OK = True
return OK
def unlock(seconds):
debug_log("Unlocked!")
i2c_lcd.clear()
i2c_lcd.move_to(0, 0)
i2c_lcd.putstr("Unlocked!")
utime.sleep(seconds)
return None
def failedCode(msg, seconds):
debug_log(msg)
i2c_lcd.clear()
i2c_lcd.move_to(0, 0)
i2c_lcd.putstr(msg)
utime.sleep(seconds)
return None
#endregion LOCK LOGIC
def main():
"""
cmd_mode(LOCK LOGIC):
1 - pin inserting (being inserted)
2 - check code (wait for response)
0 - error?
codes 3 and 4 falls back after x amount of seconds to asking pin
"""
cmd_mode = 1 # initialize to inserting pin mode
i2c_lcd.backlight_on()
i2c_lcd.blink_cursor_on()
# Initializing keypad
keypad = KEYPAD()
keypad.setAutocollectMode(5)
currentPin = ""
showPin(currentPin)
change = False
while cmd_mode != 0:
_buf = keypad.getBuffer()
change = True if _buf != currentPin else False
currentPin = _buf
if cmd_mode == 1:
if "*" in currentPin: # clear
keypad.clearBuf()
elif len(currentPin) == 5: # full
print(currentPin)
if currentPin[4] == "#":
cmd_mode = 2
else: # failed
cmd_mode = 3
elif change == True: # render pin again
showPin(currentPin)
elif cmd_mode == 2:
isOK = checkCode(currentPin)
keypad.clearBuf()
if isOK == True:
cmd_mode = 1
keypad.clearBuf()
unlock(3)
else:
cmd_mode = 1
keypad.clearBuf()
failedCode("Wrong code", 3)
elif cmd_mode == 3:
cmd_mode = 1
keypad.clearBuf()
failedCode("Failed code", 3)
debug_log(currentPin)
utime.sleep(REFRESH)
return None
main()Loading
esp-01
esp-01