from machine import Pin, PWM, ADC, UART, time_pulse_us
import dht
import network
import time
import BlynkLib
# ==========UART Setup==========
uart = UART(1, baudrate = 9600, tx = Pin(17), rx = Pin(16))
# ==========DHT22 GPIO SETUP==========
dht_sensor = dht.DHT22(Pin(14))
# ==========GAS SENSOR GPIO SETUP==========
gas_digital = Pin(25, Pin.IN)
# ==========ULTRASONIC GPIO SETUP==========
TRIG_PIN = 5
ECHO_PIN = 18
trig = Pin(TRIG_PIN, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
# ==========SERVO MOTOR GPIO SETUP==========
SERVO_1_PIN = 26
SERVO_2_PIN = 27
servo_door = PWM(Pin(SERVO_1_PIN), freq=50)
servo_lock = PWM(Pin(SERVO_2_PIN), freq=50)
# ==========LED GPIO SETUP==========
led = Pin(2, Pin.OUT)
# ==========Distance THRESHOLD==========
distance_threshold = 30
# ==========SERVO MOTORS ANGLE SETTING==========
def set_servo_door_angle(angle):
min_duty = 26
max_duty = 123
duty = int(min_duty + (angle / 180) * (max_duty - min_duty))
servo_door.duty(duty)
def set_servo_lock_angle(angle):
min_duty = 26
max_duty = 123
duty = int(min_duty + (angle / 180) * (max_duty - min_duty))
servo_lock.duty(duty)
# ==========NEAR FLAG DECELERATION==========
near_flag = False
# ==========MEASURE DISTANCE==========
def measure_distance():
trig.off()
time.sleep_us(2)
trig.on()
time.sleep_us(10)
trig.off()
duration = time_pulse_us(echo, 1)
distance = (duration * 0.034) / 2
return distance
# =========================
#define BLYNK_TEMPLATE_ID "TMPL25W00yD0S"
#define BLYNK_TEMPLATE_NAME "Semester 1 Project"
#define BLYNK_AUTH_TOKEN "0hBvbccRH31p0i7V7dpuEMck_X8szQYm"
BLYNK_AUTH_TOKEN = "0hBvbccRH31p0i7V7dpuEMck_X8szQYm"
BLYNK_TEMPLATE_ID = "TMPL25W00yD0S"
BLYNK_TEMPLATE_NAME = "Semester 1 Project"
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
# ==========WIFI==========
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
print("Connecting to WiFi....")
while not wifi.isconnected():
time.sleep(0.1)
print("WiFi Connected")
# ==========BLYNK==========
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN, insecure = True)
@blynk.on("connected")
def blynk_connected():
print ("Blynk Connected")
blynk.sync_virtual(0)
# ==========LAST SENSOR TIME==========
last_sensor_time = 0
# ==========MAIN LOOP==========
while True:
digital_gas_value = gas_digital.value()
time.sleep(0.5)
distance = measure_distance()
print(f"Distance: {distance} cm")
time.sleep(1)
if distance <= distance_threshold or digital_gas_value == 0: #DOOR OPENING CONDITIONS
set_servo_lock_angle(180)
time.sleep(0.5)
set_servo_door_angle(180)
door_status = "OPEN"
else:
set_servo_door_angle(0)
time.sleep(1)
set_servo_lock_angle(0)
door_status = "CLOSED"
if digital_gas_value == 0: #GAS LEAK ALERT
led.value(1)
print ("HIGH GAS LEVEL!")
else:
led.value(0)
if distance <= distance_threshold: #NEARBY OBJECT ALERT
near_flag = True
else:
near_flag = False
blynk.run() #Blynk RUNNING
now = time.ticks_ms()
if time.ticks_diff(now, last_sensor_time) > 250:
last_sensor_time = now
try:
blynk.virtual_write(0, door_status) #V0 FOR DOOR STATUS
print(f"Door Status: {door_status}")
except Exception as e:
print("Door Error:", e)
try:
if digital_gas_value == 1:
blynk.virtual_write(1, 0) #V1 FOR GAS VALUE
time.sleep(3)
elif digital_gas_value == 0:
blynk.virtual_write(1, 225)
time.sleep(3)
print(f"Gas value: {digital_gas_value}")
except Exception as e:
print("Gas sensor Error:", e)
try:
if led.value() == 1:
blynk.virtual_write(2, 225) #V2 FOR GAS ALERT
time.sleep(3)
elif led.value() == 0:
blynk.virtual_write(2, 0)
time.sleep(3)
except Exception as e:
print("LED ERROR:", e)
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
print(f"Temperature: {temp} C")
print(f"Humidity: {hum} %")
door = door_status
blynk.virtual_write(3, temp) # V3 TEMPERATURE
blynk.virtual_write(4, hum) # V4 HUMIDITY
except Exception as e:
print("DHT22 Error:", e)
msg = "{:.1f},{:.1f},{},{},{}\n".format(temp, hum, near_flag, door, digital_gas_value)
uart.write(msg)
print("Sent!", msg)
DOOR LOCK
DOOR