from machine import Pin, I2C
from i2c_lcd import I2cLcd
import utime
import dht
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = 0x27
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
pir = Pin(2, Pin.IN)
led = Pin(3, Pin.OUT)
led.value(0)
led_off_time = 0
while True:
dht_sensor = dht.DHT22(Pin(4))
dht_sensor.measure()
temp =dht_sensor.temperature()
motion = pir.value()
current_time = utime.ticks_ms()
if motion == 1 and temp <30:
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Motion Detected")
lcd.move_to(0, 1)
lcd.putstr("Temp <30")
elif motion == 1 and temp>=30:
led.value(1)
led_off_time = utime.ticks_add(current_time, 30000)
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Motion Detected")
lcd.move_to(0, 1)
lcd.putstr("Temp >=30")
if led.value() == 1 and utime.ticks_diff(led_off_time, current_time) <= 0:
led.value(0)
utime.sleep(0.2)