from machine import Pin
import network
import dht
import time
import urequests
#link wlan
sta=network.WLAN(network.STA_IF)
sta.active(True)
sta.connect('seanwee','17485523')
print('Linking...')
while not sta.isconnected() :
pass
print('Link OK')
#init
p0=Pin(21, Pin.IN)
d=dht.DHT11(p0)
make_url = 'stop_https://hook.eu2.make.com/e1ct2nbp2c26wf9t2aziyze4nacwyntp'
j=0
#main
while True:
f=open('DHT_Temp.txt','w')
Temp=0
Hum=0
try:
for i in range(5):
d.measure()
t=d.temperature()
h=d.humidity()
Temp+=t
Hum+=h
time.sleep(0.5)
except Exception as e: print(e)
Temp = Temp/5
Hum = Hum/5
print('Temperature=', Temp, 'C', ',Humidity=', Hum, '%')
if(Temp>=25):
try:
print(make_url +"?DHT_Temp="+str(Temp))
res=urequests.post(make_url+'?DHT_Temp='+str(Temp))
res.close()
print("溫度過高,目前為"+str(Temp)+"度")
except Exception as e: print(e)
j=j+1
print('第'+str(j)+'筆'+'publish finish')
f.write(str(Temp)+' '+str(Hum)+'\n')
time.sleep(5)
f.close()