from machine import I2C, Pin
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from dht import DHT22
from time import sleep

I2C_ADDR = 0x27
TOTAL_ROWS = 2
TOTAL_COLUMNS = 16

i2c = I2C(scl=Pin(22), sda=Pin(21), freq=50000)
lcd = I2cLcd(i2c, I2C_ADDR, TOTAL_ROWS, TOTAL_COLUMNS)


dht_pin = Pin(33)
sensor = DHT22(dht_pin)

while True:
    try:
        sensor.measure()
        temperature = sensor.temperature()
        humidity = sensor.humidity()
        lcd.clear()
        lcd.putstr("Suhu: {:.2f}C".format(temperature))
        lcd.move_to(0, 1)
        lcd.putstr("Kelembapan: {:.2f}%".format(humidity))

        print("Suhu: {:.2f}°C".format(temperature))
        print("Kelembapan: {:.2f}%".format(humidity))

    except OSError as e:
        print("Error:", e)

    sleep(2)