#importing libraries
import dht
from machine import Pin , UART , ADC, time_pulse_us, PWM, Timer
import time
import network
import BlynkLib
#wifi connection and blynk token
BLYNK_TEMPLATE_ID = "TMPL2NokFB3pV"
BLYNK_TEMPLATE_NAME = "Semester 1 Project"
BLYNK_AUTH_TOKEN = "HbMWKl1LSSoHtIDc8Oa6pgVtcNlh7NEX"
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
# =========================
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 code
# =========================
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN , insecure=True)
@blynk.on("connected")
def blynk_connected():
print("Blynk connected")
dht_sensor = dht.DHT22(Pin(32)) # Data pin connected to GPIO32
# uart, added the timeout to send all the values at the same time
uart = UART(1,baudrate=9600,tx = Pin(17) , rx = Pin(16), timeout= 1000,timeout_char=100)
#gas pins
gas_dig = Pin(25, Pin.IN)
gas_sensor = ADC(Pin(34))
gas_sensor.atten(ADC.ATTN_11DB)
#ultrasonic pins
echoPin = Pin(14, Pin.IN)
trigPin = Pin(12, Pin.OUT)
#LED pin
led = Pin(4, Pin.OUT)
#thresholds
dis_threshold = 30
gas_threshold = 2500
temp_threshold= 30
hum_threshold= 40
#two servos (one for lock and the other for door)
servo_lock = PWM(Pin(2), freq=50)
servo_door = PWM(Pin(18), freq=50)
#Servos functions
def lock_servo_angle(angle):
duty = int((angle / 180) * 102 + 26)
servo_lock.duty(duty)
def door_servo_angle(angle):
duty = int((angle / 180) * 102 + 26)
servo_door.duty(duty)
#setting both servos on zero angle
lock_servo_angle(0)
door_servo_angle(0)
def dis_measure():
#measuring the distance
trigPin.value(0)
time.sleep_us(2)
trigPin.value(1)
time.sleep_us(10)
trigPin.value(0)
#calculates the time that the echo pin was high
duration = time_pulse_us(echoPin, 1)
dis = (duration * 0.034) / 2
return dis
#stores the last time the sensors read the data
last_read = time.ticks_ms()
# MAin Loop
while True :
blynk.run()
# wait till 250 ms passed before reading
if time.ticks_diff(time.ticks_ms() ,last_read) >= 250:
now = time.ticks_ms()
last_read = now
#setting variabls and reads data
dis_value = dis_measure() #reading distance
gas = gas_sensor.read() #reading gas value
near_flag = "no object detected"
gas_leak = "no gas leak"
door_text = "door closed"
door_open = False
# DHT
dht_sensor.measure()
temp =dht_sensor.temperature()
hum = dht_sensor.humidity()
# Check distance
if dis_value < dis_threshold or gas > gas_threshold:
door_open = True
else:
door_open = False
# Check gas
if gas > gas_threshold:
gas_leak = "gas leak detected"
led.on()
gas_leak = 1
else:
led.off()
gas_leak = 0
#check door state
if door_open:
lock_servo_angle(80)
door_servo_angle(80)
door_state = 1
door_text = "open"
else:
door_state = 0
lock_servo_angle(0)
door_servo_angle(0)
door_text = "closed"
# a checker if the servos conditions are met
print("Distance:", dis_value)
print("Gas:", gas)
print("door_open:", door_open)
try:
print("Temp : ", temp," | hum : " ,hum," | dis : ", dis_value, "Gas:" , gas)
if dis_value < dis_threshold:
near_flag = 1
else:
near_flag= 0
# CSV message
msg = "{:.1f},{:.1f},{},{},{}\n".format(
temp,
hum,
near_flag,
door_state,
gas_leak
)
uart.write(msg)
print("Sent : ", msg.strip())
# Send data to Blynk
blynk.virtual_write(0, door_text)
blynk.virtual_write(1, gas)
blynk.virtual_write(2, gas_leak)
blynk.virtual_write(3, temp)
blynk.virtual_write(4, hum)
except Exception as e :
print("sensors read error : ",e)