【问题标题】:How to make this work in parallel with _thread on a pico w and micropython?如何在 pico w 和 micropython 上与 _thread 并行工作?
【发布时间】:2022-11-05 20:33:51
【问题描述】:

因此,我试图创建一个网络服务器并使用 pico 并行处理数据,我的目标是使用本地网络 ip 从我的浏览器访问 pico,以查看 pico 在哪个步骤工作以及当前循环中有哪些数据,但是我有两个问题,我不知道如何使它工作:

  1. 当使用_thread并行运行两个进程时,webserver函数挂起直到dataprocess函数完成,所以我无法实时看到发生了什么,webserver只有在另一个进程完成时才会响应并且它再次挂起,我需要按f5 在我的浏览器上的确切时间,数据处理功能完成并重新启动只是为了查看部分过程,因为如果我刷新浏览器以查看进度,它会挂起

  2. 运行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


    【解决方案1】:

    我不确定您的代码到底发生了什么,但是使用相同的线程思想,如果有帮助,请在此处找到我今天遇到的类似问题的解决方案。

        from machine import Pin
        from time import sleep
        
        import wlan_data, network
        import socket, _thread
        
        
        wlan = network.WLAN(network.STA_IF)
        wlan.active(True)
        wlan.connect(wlan_data.SSID, wlan_data.PASSWORD)
        
        print("WLAN is connected: " + str(wlan.isconnected()))
        
        page = open("index.html", "r")
        html = page.read()
        page.close()
        
        sta_if = network.WLAN(network.STA_IF)
        print(sta_if.ifconfig()[0])
        
        def setUpSocket():
            addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
            s = socket.socket()
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            s.bind(addr)
            s.listen(1)
            return s
        
        def serve(s):
            while True:
                try:
                    cl, addr = s.accept()
                    cl_file = cl.makefile('rwb', 0)
                    while True:
                        line = cl_file.readline()
                        if not line or line == b'
    ':
                            break
                    response = html
                    cl.send('HTTP/1.0 200 OK
    Content-type: text/html
    
    ')
                    cl.send(response)
                    cl.close()
                except OSError as e:
                    cl.close()
            
        def webserver():
            s = setUpSocket()
            _thread.start_new_thread(serve,(s,))
        
        webserver()
        
        led = machine.Pin("LED", machine.Pin.OUT)
        led.toggle()
            
        
        while 1:
            led.toggle()
            sleep(10)
    
    

    【讨论】:

      猜你喜欢
      • 2022-09-25
      • 2022-08-15
      • 2023-01-03
      • 2023-01-03
      • 2021-07-01
      • 2022-12-15
      • 2023-01-31
      • 2022-09-29
      • 2022-12-03
      相关资源
      最近更新 更多