import machine
import time
import dht
import pico_i2c_lcd
sensor = dht.DHT22(machine.Pin(22))
i2c = machine.I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = pico_i2c_lcd.I2cLcd(i2c, I2C_ADDR, 2, 16)
old_temp = 100000
old_hum = 100000
lcd.putstr("TEMP: waiting...\n")
lcd.putstr(" HUM: waiting...")
def writeTemp(temp):
# temp_f = temp * (9/5) + 32.0
lcd.move_to(5, 0)
lcd.putstr(" ")
lcd.move_to(5, 0)
lcd.putstr(" "+str(temp) + "C")
# lcd.putstr(" "+str(temp) + "C "+str(temp_f)+"F")
def writeHum(hum):
lcd.move_to(5, 1)
lcd.putstr(" ")
lcd.move_to(5, 1)
lcd.putstr(" "+str(hum) + "%")
while True:
print(I2C_ADDR)
try:
time.sleep(2)
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
if old_temp != temp:
writeTemp(temp)
if old_hum != hum:
writeHum(hum)
old_hum = hum
old_temp = temp
except OSError as e:
print('Failed to read sensor.')