import dht
import time
import machine
from time import sleep_ms, ticks_ms
from machine import I2C, Pin
from i2c_lcd import I2cLcd
sensor =dht.DHT22(machine. Pin(4))
AddressOfLcd = 0x27
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
def read_temp_humi():
try:
sensor.measure()
temp = sensor.temperature()
humi = sensor.humidity()
print("temperature: {} C".format(temp))
print("humidity: {} C".format(humi))
lcd.move_to(0,0)
lcd.putstr("temp: {} C".format(temp))
lcd.move_to(0,1)
lcd.putstr("hum: {}% ".format(humi))
except OsError as e:
print("Fialed to read sensor")
if __name__ =="__main__":
while True:
read_temp_humi()
time.sleep(5)