from machine import I2C, Pin
from time import sleep
from pico_i2c_lcd import I2cLcd
import dht
dSensor = dht.DHT22(Pin(2))
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
def readDHT():
try:
dSensor.measure()
temp = dSensor.temperature()
temp_f = (temp * (9/5)) + 32.0
hum = dSensor.humidity()
print('Temperature= {} C, {} F'.format(temp, temp_f))
print('Humidity= {} '.format(hum))
return temp, hum
except OSError as e:
print('Failed to read data from DHT sensor')
while True:
temp, hum = readDHT()
lcd.clear()
lcd.putstr("Temp:"+str(int(temp))+"\n")
lcd.putstr("Humidity:"+str(int(hum))+"\n")
sleep(2)