我正在尝试用尽电池电量,以查看为ESP32供电的时间。为此,我编写了一个简单的程序,每分钟一次将“保持活动”写入网站。但是,该程序每次将字符串成功写入Web文件时都会崩溃。我不知道为什么它可以工作九次,然后失败。这是代码:
from time import sleep_ms, ticks_ms
import network
import socket
import urequests
import machine
import json
SSID="my_ssid"
PASSWORD="my_password"
port=100
wlan=None
s=None
def connectWifi(ssid,passwd): #function to connect to the Web
global wlan #declare a WLAN object
wlan=network.WLAN(network.STA_IF) #create a wlan object
wlan.active(True) #Activate the network interface
wlan.disconnect() #Disconnect the last connected WiFi
wlan.connect(ssid,passwd) #connect wifi
while(wlan.ifconfig()[0]=='0.0.0.0'): #wait for connection
sleep_ms(1)
sleep_ms(1000) #hold on for 1 second
sendmessage("Connected to WLAN")
sleep_ms(1000) #hold on for 1 second
return True
def sendmessage(myMessage):
url = "http://www.sabulo.com/stillalive.php"
headers = {'content-type': 'application/json'}
data = {'message': myMessage}
jsonObj = json.dumps(data)
resp = urequests.post(url, data=jsonObj, headers=headers)
return True
def main():
connectWifi(SSID,PASSWORD)
hitcount = 0
while True:
sendmessage("Still alive " + str(hitcount))
print("Still alive " + str(hitcount))
hitcount = hitcount + 1
sleep_ms(1000)
main()
然后我得到了:
>>>
>>> I (5906) phy: phy_version: 4007, 9c6b43b, Jan 11 2019, 16:45:07, 0, 0
Still alive 0
Still alive 1
Still alive 2
Still alive 3
Still alive 4
Still alive 5
Still alive 6
Still alive 7
Still alive 8
╝Traceback (most recent call last):
File "<stdin>", line 45, in <module>
File "<stdin>", line 40, in main
File "<stdin>", line 33, in sendmessage
File "urequests.py", line 111, in post
File "urequests.py", line 56, in request
OSError: 23
╝>
MicroPython v1.10-98-g4daee3170 on 2019-02-14; ESP32 module with ESP32
Type "help()" for more information.
我在各种应用程序中使用了相同的网络上传代码段,但从来没有出现此错误。
任何帮助表示感谢:)