from machine import Pin, I2C
import ssd1306
import random
import umail
import network
from time import sleep
from servo import Servo
# ESP32 Pin assignment
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
servo_pin = Pin(4)
my_servo = Servo(servo_pin)
KEY_UP = const(0)
KEY_DOWN = const(1)
row_pins = []
col_pins = []
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
rows = [13,12,14,27]
row_pins = [Pin(pin_name, mode=Pin.OUT) for pin_name in rows]
cols = [26,25,33,32]
col_pins = [Pin(pin_name, mode=Pin.IN, pull=Pin.PULL_DOWN) for pin_name in cols]
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
#Connect to your network
def connect_wifi(ssid, password):
station = network.WLAN(network.STA_IF)
station.active(True)
try:
station.connect(ssid, password)
while not station.isconnected():
oled.text("Connecting...",1,1)
oled.show()
oled.fill(0)
oled.show()
oled.text('Connected :)',1,1)
oled.show()
sleep(3)
except:
oled.fill(0)
oled.show()
oled.text('Connection Failed :(',1,1)
oled.show()
# Send the email
def send_otp(recipient_email):
new_otp = str(random.randint(100000,999999))
body = "Dear "+name+","+"\n"+"\n"+"Your OTP is "+new_otp+"."
smtp = umail.SMTP('smtp.gmail.com', 465, ssl=True) # Gmail's SSL port
smtp.login(sender_email, sender_app_password)
smtp.to(recipient_email)
smtp.write("From:" + sender_name + "<"+ sender_email+">\n")
smtp.write("Subject:" + email_subject + "\n")
smtp.write(body)
smtp.send()
smtp.quit()
return new_otp
# Sets the columns to low
def init():
for row in range(0,4):
for col in range(0,4):
row_pins[row].value(0)
#Keypad Scan
def scan(row, col):
""" scan the keypad """
# set the current column to high
row_pins[row].value(1)
key = None
# check for keypressed events
if col_pins[col].value() == KEY_DOWN:
key = KEY_DOWN
if col_pins[col].value() == KEY_UP:
key = KEY_UP
row_pins[row].value(0)
# return the key state
return key
# OTP Verification
def otp_verify(n=0):
global OTP
received_OTP = ""
oled.fill(0)
oled.show()
oled.text("OTP Sent",1,1)
oled.show()
sleep(3)
oled.fill(0)
oled.show()
while n!=3:
oled.text("Enter OTP: ",1,1)
oled.show()
oled.text(received_OTP,1,10)
oled.show()
for row in range(4):
for col in range(4):
key = scan(row,col)
if key == KEY_DOWN:
if keys[row][col]=='#':
print()
if received_OTP==OTP:
oled.fill(0)
oled.show()
oled.text("OTP Verified",1,1)
oled.show()
sleep(2)
oled.fill(0)
oled.show()
oled.text("Unlocked",1,1)
oled.show()
my_servo.move(0)
sleep(5)
oled.fill(0)
oled.show()
oled.text("Locked",1,1)
oled.show()
my_servo.move(90)
sleep(2)
oled.fill(0)
oled.show()
return
elif 2-n==0:
minu=5
while minu>0:
oled.fill(0)
oled.show()
oled.text(f"Locked {minu} Min",1,1)
oled.show()
sleep(60)
minu -= 1
return
else:
oled.fill(0)
oled.show()
oled.text("Invalid OTP",1,1)
oled.show()
sleep(2)
oled.fill(0)
oled.show()
oled.text(f"{2-n} attempts left",1,1)
oled.show()
sleep(2)
oled.fill(0)
oled.show()
oled.text("Resending OTP...",1,1)
oled.show()
OTP = send_otp(recipient_email)
otp_verify(n+1)
return
elif keys[row][col]=='D':
print()
received_OTP = received_OTP[:len(received_OTP)-1]
print(f"{received_OTP}",end='')
oled.fill(0)
oled.show()
sleep(0.25)
break
else:
print(keys[row][col],end="")
received_OTP += keys[row][col]
sleep(0.25)
break
OTP = ""
ssid = 'Wokwi-GUEST'
password = ''
sender_email = '[email protected]'
sender_name = 'Ho.Se'
sender_app_password = 'rwfq wzgs cjnl nwob'
recipient_email = "[email protected]"
email_subject ='Door Lock OTP'
name = "User"
pasw = "1234"
pasinp = ""
# Connect to your network
connect_wifi(ssid, password)
oled.fill(0)
oled.show()
# Program Start
while True:
oled.text("Enter Password: ",1,1)
oled.show()
oled.text(pasinp,1,10)
oled.show()
for row in range(4):
for col in range(4):
key = scan(row,col)
if key == KEY_DOWN:
if keys[row][col]=='#':
print()
if pasinp==pasw:
pasinp = ""
oled.fill(0)
oled.show()
oled.text("Sending OTP...",1,1)
oled.show()
OTP = send_otp(recipient_email)
otp_verify()
else:
pasinp = ""
oled.fill(0)
oled.show()
oled.text("Incorrect Pass",1,1)
oled.show()
sleep(10)
oled.fill(0)
oled.show()
elif keys[row][col]=='D':
pasinp = pasinp[:len(pasinp)-1]
print()
print(f"{pasinp}",end='')
oled.fill(0)
oled.show()
sleep(0.25)
break
else:
print(f"{keys[row][col]}",end="")
pasinp += keys[row][col]
sleep(0.25)
break