0

I am writing in MicroPython. Target is a servo-motor controlled by calls to device's IP-address. The Raspberry Pi Pico runs a server listening to incoming requests:

http://192.168.178.22/servo/on      should start the servo motor
http://192.168.178.22/servo/off     should stop the servo motor

If calls are made not-async it works, but I need to stop the servo at any time, so async is needed (should run indefinitely until "off" is called). That's why I came up with uasyncio. Everything works but only after second request the server no longer responds.

main running in a loop only showing the memory (of use for later implementation):

async def main():
    print("Setting up event loop...")
    showFilesOnPico()
    connectToWifi(secrets.SSID, secrets.PASSWORD, ledInternal)
    servoStartUp()

    # The event loop will keep running while the server is running
    while True:
        print(".")
        # check for low memory
        print("Free Memory:", gc.mem_free(), "bytes" )
        ##
        await uasyncio.sleep(1)  # Yield control to other tasks

Starting of server logic:

async def startServer():
    addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
    server = await uasyncio.start_server(handle_request, *addr)

handle_request (for testing I disabled servo on/off as the problem also appears on "testingdepartment"-requests):

async def handle_request(reader, writer):
    print('handle_request called')
    try:
        request = await reader.read(1024)
        request = str(request)

        print("Request:", request)

        #led_on = request.find('/servo/on')
        #led_off = request.find('/servo/off')
        testing = request.find('/testingdepartment')

        #if led_on != -1:
        #    ledInternal.value(1)
        #    uasyncio.create_task(demoServo())

        #if led_off != -1:#
        #    ledInternal.value(0)
        #    uasyncio.create_task(deactivateServo())

        if testing != -1:
            print("testing clicked")
            await uasyncio.sleep(1)

        print("before writer.drain")
        response = b'HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n'
        writer.write(response)
        await writer.drain()
        print("after writer.drain")
    except Exception as e:
        print('Error in handle_request:', e)
    finally:
        reader.close()
        writer.close()
    print("end of handle request reached :-D :-D :-D")
    await uasyncio.sleep(1)

Start of async loop:

loop = uasyncio.get_event_loop()
loop.create_task(main())
loop.create_task(startServer())
loop.run_forever()

It works only for a few requests. I could not isolate the problem. The requests are made calling the URL mentioned earlier. The servo-motor is set by this function which works (for testing the motor only repeats the movement 6 times).

async def demoServo():
    print('DemoServo is called')

    global isServoRoutineRunning
    global shouldAbortServoRoutine
    if not isServoRoutineRunning:
        # Reset shouldAbortServoRoutine to False
        shouldAbortServoRoutine = False
        isServoRoutineRunning = True
        for x in range(6):
            if shouldAbortServoRoutine:
                break
            print('pos 90')
            pwm.duty_ns(grad090)
            await uasyncio.sleep(2)

            print('pos 0')
            pwm.duty_ns(grad000)
            await uasyncio.sleep(2)

        pwm.deinit()
        print('end')
        isServoRoutineRunning = False
user4157124
  • 2,809
  • 13
  • 27
  • 42
Hillbicks
  • 1
  • 1

0 Answers0