from machine import Pin
import network
import time
import urequests
import json
#R紅,PIN7
#Y黃,PIN8
#B藍,PIN11
LED_R = Pin(7, Pin.OUT)
LED_Y = Pin(8, Pin.OUT)
LED_B = Pin(11, Pin.OUT)
#連線及設定API
sta=network.WLAN(network.STA_IF)
sta.active(True)
sta.connect('Wokwi-GUEST','')
while not sta.isconnected() :
time.sleep(0.1)
host='http://api.thingspeak.com'
api_key='LS5LR18Q9MI7CLC9'
#主程式
while True:
#接收thinkspeak資料
url='%s/channels/2509413/feeds.json?api_key=%s&results=1' %(host, api_key)
r=urequests.get(url)
d=json.loads(r.text)
#資料定位
WATER_Value = d['feeds'][0]['field1']
TEMP_Value =d['feeds'][0]['field2']
HUM_Value =d['feeds'][0]['field3']
#顯示接收到的資訊
print("水位:",str(WATER_Value),"%, 溫度:",str(TEMP_Value),"C, 濕度:",str(HUM_Value))
#當溫度低於0度時,亮提示燈,反之不亮
if(float(TEMP_Value) <= 0):
LED_Y.value(1)
else:
LED_Y.value(0)
#當濕度超過65時,亮提示燈,反之不亮
if(float(HUM_Value) > 65):
LED_B.value(1)
else:
LED_B.value(0)
#當水位低於30%時,亮提示燈,反之不亮
if(float(WATER_Value) < 30):
LED_R.value(1)
else:
LED_R.value(0)
#每隔5秒做一次資料讀取
time.sleep(5)Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1