from machine import Pin
import time
import dht
p0 = Pin(2, Pin.IN)          # 溫溼度感測器輸入腳接到 GPIO2
LED1 = Pin(25, Pin.OUT)      # LED1 輸出腳接到 GPIO25
LED2 = Pin(26, Pin.OUT)      # LED2 輸出腳接到 GPIO26
d = dht.DHT22(p0)            # 建立 DHT22 物件
while True:
    d.measure()              # 重新量測溫溼度
    t = d.temperature()      # 讀取溫度
    h = d.humidity()         # 讀取濕度
    print('Temperature =', t, 'C', 'Humidity =', h, '%')  # 列印溫度與濕度
    
    if t > 40:               # 若溫度 > 40℃
        LED1.value(0)        # LED1 亮
    else:
        LED1.value(1)        # LED1 滅
    if h > 60:               # 若濕度 > 60%
        LED2.value(0)        # LED2 亮
    else:
        LED2.value(1)        # LED2 滅
    time.sleep(0.5)          # 延遲 0.5 秒