# ex_3_5_05_oled_dht22.py (จำลอง: https://wokwi.com/projects/469817988933583873)
from machine import Pin, I2C
import ssd1306
import dht
import time
i2c = I2C(0, sda=Pin(21), scl=Pin(22), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3c)
sensor = dht.DHT22(Pin(25))
while True:
try:
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
oled.fill(0) # วาดใหม่ทั้งเฟรมทุกรอบ
oled.text("Farm Monitor", 0, 0)
oled.text("T: {:.1f} C".format(t), 0, 24)
oled.text("H: {:.1f} %".format(h), 0, 40)
oled.show()
except OSError:
oled.fill(0)
oled.text("Sensor Error!", 0, 24)
oled.show()
time.sleep(2)