from machine import Pin, I2C
import ssd1306
import dht
import time
# Setup I2C untuk OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Setup Sensor DHT22 (Pindahkan ke Pin 15 agar lebih stabil)
sensor = dht.DHT22(Pin(15))
while True:
try:
time.sleep(2) # DHT22 butuh waktu jeda minimal 2 detik
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
# Bersihkan layar sebelum update data baru
oled.fill(0)
# Tampilkan data (Wajib diconvert ke string dengan str())
oled.text("Suhu: " + str(temp) + " C", 0, 10)
oled.text("Hum: " + str(hum) + " %", 0, 30)
oled.show()
except OSError as e:
print("Gagal membaca sensor.")