[問題] 程式碼問題請教已刪文
各位版上30cm大大日安。
小弟有三個問題請教,先付上程式碼。
這是從Micropython書上的範例,硬體是ESP8266。
from machine import Pin, Timer
import urequests as req
import dht
import time
import gc
#下列定義腳位關係,方便拉線除錯使用。
D0=16
D1=5
D2=4
D3=0
D4=2
D5=14
D6=12
D7=13
D8=15
d=dht.DHT11(Pin(D4))
running = True
url='http://api.thingspeak.com/update'
key='SETM42*********'
apiURL="{}?key={}".format(url, key)
def sendDHT11(t):
global apiURL, running
try:
d.measure()
except OSError as e:
print(e)
return
apiURL += "&field1={}&field2={}".format(d.temperature(),d.humidity())
r=req.get(apiURL)
print('Free RAM before GC:',gc.mem_free())
gc.collect()
print('Free RAM after GC:', gc.mem_free())
if r.status_code != 200:
t.deinit()
print('Bad request error.')
running=False
else:
print('Data saved, id:', r.text)
tim=Timer(1)
tim.init(period=30000, mode=Timer.PERIODIC, callback=sendDHT11)
try:
while running:
pass
except:
tim.deinit()
print('stopped')
第一個問題:
我有嘗試把
print('Free RAM before GC:',gc.mem_free())
gc.collect()
print('Free RAM after GC:', gc.mem_free())
放在defsendDHT11(t):的else底下。
也嘗試放在
tim.init(period=30000, mode=Timer.PERIODIC, callback=sendDHT11)
底下
也嘗試放在現在這個位置,但是都遇到
memoryerror: memory allocation failed, allocating 10134 bytes
這個錯誤
請問應該要如何解決?
第二個問題:
一般我們要動作的內容會放在While loop裡面。
可是這個案例卻是放在While loop 外面,但是動作卻是正常的,請問是為什麼呢?
第三個問題:
一般如果我們的函式有帶參數,就必須要加入參數做呼叫。
但是這個案例呼叫時沒有傳參數卻也沒有錯誤出現。請問是為什麼呢?
以上三個問題,煩請撥冗解惑,感恩。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.230.72.85 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1613806631.A.73D.html
→
02/20 17:12,
4年前
, 1F
02/20 17:12, 1F
→
02/20 19:10,
4年前
, 2F
02/20 19:10, 2F
→
02/20 19:10,
4年前
, 3F
02/20 19:10, 3F
※ 編輯: yimean (118.232.28.188 臺灣), 02/20/2021 21:00:43
→
02/20 22:38,
4年前
, 4F
02/20 22:38, 4F
→
02/20 22:40,
4年前
, 5F
02/20 22:40, 5F
→
02/20 22:41,
4年前
, 6F
02/20 22:41, 6F
推
02/21 00:40,
4年前
, 7F
02/21 00:40, 7F
推
02/21 00:44,
4年前
, 8F
02/21 00:44, 8F
Python 近期熱門文章
PTT數位生活區 即時熱門文章