import time
import board
import adafruit_dht
from machine import Pin, I2C
import ssd1306
# 設定 OLED 螢幕
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# 設定 DHT22 感測器
dht_pin = board.D4 # 根據實際硬體連接調整引腳
dht = adafruit_dht.DHT22(dht_pin)
while True:
try:
# 讀取溫濕度數據
temperature = dht.temperature
humidity = dht.humidity
# 顯示溫濕度數據在 OLED 螢幕上
oled.fill(0)
oled.text("Temperature: {:.1f} C".format(temperature), 0, 0)
oled.text("Humidity: {:.1f} %".format(humidity), 0, 16)
# 檢查是否需要發出警告
if temperature > 30 or humidity > 90:
oled.text("WARNING", 0, 32)
oled.show()
except Exception as e:
print("Error:", e)
time.sleep(2)