miguelgrinberg/microdot

WebSocket becomes unresponsive for slow sensors

Closed this issue · 0 comments

IMPORTANT: If you have a question, or you are not sure if you have found a bug in this package, then you are in the wrong place. Hit back in your web browser, and then open a GitHub Discussion instead. Likewise, if you are unable to provide the information requested below, open a discussion to troubleshoot your issue.

Describe the bug
I used WebSocket to turn on some DC motors. I just noticed that when it takes time for starting or stopping dc motors then the connection gets dropped and MicroDot becomes unresponsive with no error.

To Reproduce

@app.route('/echo')
@with_websocket
def echo(request, ws):
    while True:
        data = ws.receive()
        // turnOnMotor() # or
        // turnOffMotor()
        ws.send(data)

Expected behavior
The motor is turned off or On upon receiving the command. Commands can come in milliseconds differential.

Additional context
What is the best way to handle this scenario in MicroDot when the function that you are about to call will take some time and how to debug this problem?

Note:
If I remove the command that turns on or off the DC motor then there is no issue and WebSocket exchanges are okay.
If I swap the DC motors with LED then there is no problem as it is a quick thing to do for the microcontroller.

Based on my debugging, the issue is probably that it takes time for motors to turn on or turn off (using PWM) and that delay is impacting the WebSocket message exchange for MicroDot.

I am looking for a possible way to handle this.

Note Again: I have tried all microdot_websocket files. (e.g microdot_websocket.py, microdot_websocket_alt.py microdot_asyncio_websocket.py)