【发布时间】:2022-11-05 20:33:51
【问题描述】:
因此,我试图创建一个网络服务器并使用 pico 并行处理数据,我的目标是使用本地网络 ip 从我的浏览器访问 pico,以查看 pico 在哪个步骤工作以及当前循环中有哪些数据,但是我有两个问题,我不知道如何使它工作:
-
当使用_thread并行运行两个进程时,webserver函数挂起直到dataprocess函数完成,所以我无法实时看到发生了什么,webserver只有在另一个进程完成时才会响应并且它再次挂起,我需要按f5 在我的浏览器上的确切时间,数据处理功能完成并重新启动只是为了查看部分过程,因为如果我刷新浏览器以查看进度,它会挂起
-
运行webserver时,dataprocess函数的urequets.get函数不起作用,抛出[Errno 103] ECONNABORTED
这是我的代码中不起作用的部分:
import utime, machine, urequests, json, network, socket, _thread led = machine.Pin("LED", machine.Pin.OUT) def connect(): global wlan wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect("SSID", "PASSS") while wlan.isconnected() == False: print("Connecting...") led.off() utime.sleep_ms(100) led.on() utime.sleep_ms(100) led.off() utime.sleep_ms(100) led.on() utime.sleep_ms(100) led.on() ip = wlan.ifconfig()[0] print(f'Connected on {ip}') return ip def open_socket(ip): address = (ip, 80) connection = socket.socket() connection.bind(address) connection.listen(1) return connection def webpage(steps): html = f""" <!DOCTYPE html> <html> <head> <title>Pico 2</title> </head> <body> <p>{steps}</p> </body> </html> """ return str(html) def pushgetdata(): while wlan.isconnected() == True: try: global steps led.off() utime.sleep_ms(300) led.on() steps = "Step 1: Reading values from sensor one...<br>" #function to read data from one sensor here #... #... #... led.off() utime.sleep_ms(100) led.on() steps = steps + "Step 2: Reading values from sensor two...<br>" #function to read data from other sensor here #... #... #... led.off() utime.sleep_ms(100) led.on() steps = steps + "Step 3: Pushing and getting results...<br>" jsondata = urequests.get("https://xxx.xxx.xxx/api/?device=pico2&sensor1=valulesfromsensor1&sensor2=valuesfromsensor2") proceseddata = jsondata.json() steps = steps + proceseddata + "<br>" steps = steps + "Step 4: Doing things with results...<br>" #function to do conditions and things with results... #... #... #... jsondata.close() steps = steps + "Step 5: Finished, sleeping for the next round...<br>" utime.sleep_ms(100) led.off() utime.sleep_ms(100) led.on() utime.sleep(900) except OSError as e: steps = steps + e def serve(connection): while True: try: client, addr = connection.accept() print('client connected from', addr) request = client.recv(1024) request = str(request) html = webpage(steps) client.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n') client.send(html) client.close() except OSError as e: client.close() def webserver(): ip = connect() connection = open_socket(ip) _thread.start_new_thread(serve,(connection,)) try: webserver() pushgetdata() except KeyboardInterrupt: machine.reset()
【问题讨论】:
标签: micropython raspberry-pi-pico