import dht
from machine import Pin, SoftI2C
from time import sleep
import ssd1306
sensor = dht.DHT22(Pin(14))
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
while True:
try:
sleep(2) # Wait between readings
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
oled.fill(0) # Clear the display before updating
oled.text("Temp: " + str(temp) + " C", 0, 0)
oled.text("Hum: " + str(hum) + " %", 0, 10)
oled.show()
except OSError as e:
print("Failed to read sensor:", e)