2017年8月21日 星期一

MicroPython DHT11測試 4 : Try ....except 與WDT (watch dog Timer)



#測試 4 : Try ....except 與WDT (watch dog Timer)
#=============================================================================
# leds 0 = d3   ,   1 = d10   ,  2 = d4 ,  3 = d9 ,  4 =  d2  ,  5 = d1
#       9 = sdd2 , 10 = sdd3
#     12 = d6    ,  13 = d7   ,  14 = d5 , 15 = d8 , 16 = d0
#=============================================================================

from machine import Pin
from machine import WDT
import dht
import time
import urequests

p0=Pin(4, Pin.IN)
p2=Pin(2, Pin.OUT)
d=dht.DHT11(p0)
host='http://api.thingspeak.com'
api_key='U52Q30TDQDA0G6IW'
wdt=WDT()                            #無參數就可以順利建立 WDT 物件

def LED_blink(s):
    wdt
    for i in range(1,10*s):
        p2.value(1)
        time.sleep_ms(50)
        p2.value(0)
        time.sleep_ms(50)

while True:
    try:
        d.measure()                
        t=d.temperature()      
        f=round(t * 9/5 + 32)
        h=d.humidity()
        url='%s/update?api_key=%s&field1=%s&field2=%s&field3=%s' %(host, api_key, t, f, h)
        print('Temperature=', t, 'C', '/', f, 'F', 'Humidity=', h, '%')
        r=urequests.get(url)
        print('response=', r.text)
    except:
        print('urequests.get() exception occurred!')
    LED_blink(16)

沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...