from machine import Pin, SoftI2C
from i2c_lcd import I2cLcd
import dht, time
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
lcd.clear()
lcd.backlight_on()
d = dht.DHT22(Pin(4))
while 1:
try:
d.measure()
temp = d.temperature()
humi = d.humidity()
lcd.clear()
lcd.move_to(0, 0)
line1 = "Temp: {:.1f}C".format(temp)
lcd.putstr(line1)
lcd.move_to(0, 1)
line2 = "Humi: {:.1f}%".format(humi)
lcd.putstr(line2)
time.sleep_ms(100)
except OSError as e: #if sensor is disconnected
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Failed to read sensor!")
time.sleep_ms(200)