from machine import Pin, I2C
from time import sleep
import dht
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
I2C_ADDR = 0x27
LCD_COLUMNS = 16
LCD_ROWS = 2
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, LCD_ROWS, LCD_COLUMNS)
dht_sensor = dht.DHT22(Pin(15))
lcd.clear()
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
humidity = dht_sensor.humidity()
print("Temp: {:.1f}°C".format(temp))
print("Humidity: {:.1f}%".format(humidity))
print("---")
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr(" Temp: {:.1f}".format(temp) + "\xDF" + "C")
lcd.move_to(0, 1)
lcd.putstr(" Humidity: {:.1f}%".format(humidity))
lcd.move_to(0, 2)
except OSError as e:
print("Failed to read sensor.")
sleep(1)