from machine import Pin, I2C
import ssd1306
import random
import umail
import network
import time
from servo import Servo
# ESP32 Pin assignment
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
def connect_wifi(ssid, password):
#Connect to your network
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while not station.isconnected():
oled.text(".",10,10)
oled.show()
time.sleep(0.5)
oled.text('Connection successful',10,10)
oled.show()
OTP,received_otp = "",""
ssid = 'Wokwi-GUEST'
password = ''
sender_email = '[email protected]'
sender_name = 'ESP32'
sender_app_password = 'cr34t30tp'
oled.text("Enter Email ID:",10,10)
oled.show()
recipient_email = [email protected]
oled.fill(0)
oled.show()
email_subject ='Door Lock OTP'
name = "User"
# Connect to your network
connect_wifi(ssid, password)
# 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
def otp_verify():
global OTP
oled.fill(0)
oled.show()
oled.text("OTP has been sent to "+recipient_email,10,10)
oled.show()
time.sleep(3)
oled.fill(0)
oled.show()
oled.text("Enter OTP: ",10,10)
oled.show()
received_OTP = input("Enter OTP: ")
oled.fill(0)
oled.show()
if received_OTP==OTP:
oled.fill(0)
oled.show()
oled.text("OTP Verified",10,10)
oled.show()
motor=Servo(pin=22)
motor.move(90)
else:
oled.fill(0)
oled.show()
oled.text("Invalid OTP",10,10)
oled.show()
time.sleep(2)
oled.fill(0)
oled.show()
oled.text("Resending OTP...",10,10)
oled.show()
OTP = send_otp(recipient_email)
otp_verify()
otp_verify()
Loading
ssd1306
ssd1306