from machine import Pin, SoftI2C
from i2c_lcd import I2cLcd
import time
import dht
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=100000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
time.sleep(1)
lcd.clear()
d = dht.DHT11(Pin(23))
t = 0
h = 0
led1 = Pin(2, Pin.OUT)
led2 = Pin(4, Pin.OUT)
led3 = Pin(16, Pin.OUT)
led1.off()
led2.off()
led3.off()
text = 'Starting...'
lcd.putstr(text)
switch = Pin(18, Pin.IN, Pin.PULL_UP)
while True:
if not switch.value():
try:
d.measure()
time.sleep(2)
t = d.temperature()
h = d.humidity()
temp = 'Temp: {:.0f} C'.format(t)
humid = 'Hum: {:.0f} %'.format(h)
print('DHT11:', t, h)
lcd.clear()
lcd.putstr(temp)
lcd.move_to(0, 1)
lcd.putstr(humid)
if t > 30:
led1.on()
else:
led1.off()
if 28 <= t <= 30:
led2.on()
else:
led2.off()
if t < 28:
led3.on()
else:
led3.off()
time.sleep(5)
except Exception as e:
pass
else:
led1.off()
led2.off()
led3.off()
lcd.clear()