import network
import time
import machine
import dht
import urequests
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
# 初始化 DHT22
# 將 '14' 替換為您連接 DHT22 的 GPIO 引腳號
sensor = dht.DHT22(machine.Pin(14))
# 讀取溫濕度
try:
sensor.measure()
temp = sensor.temperature() # 讀取溫度(攝氏度)
hum = sensor.humidity() # 讀取濕度(百分比)
print('Temperature: {}°C Humidity: {}%'.format(temp, hum))
except OSError as e:
print('Failed to read sensor.')
# IFTTT Webhook URL
ifttt_url = 'https://maker.ifttt.com/trigger/test/with/key/bEDsvcommJTQJ1GMQubpR4S2ok59VSEW_7yqc5-6JkR'
# 數據
data = {'value1': temp, 'value2': hum}
# 發送請求
response = urequests.post(ifttt_url, json=data)
response.close()