from servo import Servo
from hcsr04 import HCSR04
from time import sleep
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
import network
from blynkLib import Blynk
BLYNK_AUTH_TOKEN = "lNH9nZam0oRCmrOYV8Qcua3AFTRQFjEJ"
wifi= network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect("Wokwi-GUEST","")
while not wifi.isconnected():
pass
print("Wifi Connected Successfully")
blynk= Blynk(BLYNK_AUTH_TOKEN)
#Ultra Sonic Sensor setup
sensor = HCSR04(trigger_pin = 5, echo_pin = 18, echo_timeout_us = 100000)
#Servo setup
motor = Servo(pin=19)
motor2 = Servo(pin=4)
red= Pin(27,Pin.OUT)
yellow= Pin(14,Pin.OUT)
green= Pin(12,Pin.OUT)
#I2C LCD Setup
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
#Global Variables
freeSlots = 4
entryFlag = 0
exitFlag = 0
#PIR pin declaration
entry_PIR = Pin(13, Pin.IN)
exit_PIR = Pin(26, Pin.IN)
#Functions Definition
def displayWelcome():
lcd.move_to(0,0)
lcd.putstr("Welcome, DriveIn")
sleep(2)
def displaySlots():
lcd.move_to(2,0)
lcd.putstr("Free Slots: " + str(freeSlots))
def Depart():
lcd.clear()
lcd.move_to(0,0)
lcd.putstr("Car Departing")
sleep(5)
lcd.clear()
def SlotsFull():
lcd.clear()
lcd.move_to(0,0)
lcd.putstr("No free slots")
lcd.move_to(0,1)
lcd.putstr("available")
sleep(3)
lcd.clear()
blynk.virtual_write(2, freeSlots)
while True:
blynk.run()
distance = sensor.distance_cm()
if(freeSlots > 0):
if(distance < 9):
#Open Servo
motor.move(0)
green.value(1)
red.value(0)
#Welcome
displayWelcome()
lcd.clear()
elif(distance > 9):
motor.move(90)
green.value(0)
if(entry_PIR.value() == 1 and entryFlag == 0):
#Decrease free slots
freeSlots = freeSlots - 1
if(freeSlots < 0):
freeSlots = 0
blynk.virtual_write(2, freeSlots)
entryFlag = 1
elif(entry_PIR.value() == 0):
entryFlag = 0
# elif (freeSlots < 0 and distance > 50):
# lcd.clear()
# lcd.move_to(0,0)
else :
motor.move(90)
green.value(0)
red.value(1)
SlotsFull()
if(freeSlots < 4):
if(exit_PIR.value() == 1 and exitFlag == 0):
#Increase free slots
freeSlots = freeSlots + 1
motor2.move(0)
yellow.value(1)
green.value(0)
red.value(0)
Depart()
motor2.move(90)
yellow.value(0)
if(freeSlots > 4):
freeSlots = 4
exitFlag = 1
blynk.virtual_write(2, freeSlots)
elif(exit_PIR.value() == 0):
exitFlag = 0
print('Distance: ', distance)
displaySlots()
sleep(2)
blynk.run()