micropython-lib icon indicating copy to clipboard operation
micropython-lib copied to clipboard

tempsensor.py example disconnects unexpectedly

Open Remco-Schoeman opened this issue 1 year ago • 1 comments

at the line below the example waits for the connected client to disconnect. What is not obvious is that awaiting this disconnected() call causes an disconnect after 60 seconds!

https://github.com/micropython/micropython-lib/blob/8058b2935bad15f930a45a5f5f640c8da8aaf1f2/micropython/bluetooth/aioble/examples/temp_sensor.py#L58

It took me a long time reading code before I found the reason why: The timeout_ms parameter for the disconnected() call specifies a default timeout of 60 seconds for the wait, after which a CancelledError is raised, and the peripheral_task() exits, disconnecting the connection.

https://github.com/micropython/micropython-lib/blob/8058b2935bad15f930a45a5f5f640c8da8aaf1f2/micropython/bluetooth/aioble/aioble/device.py#L218

regrettably neither the CancelledError or the stacktrace give any clear indication of what happened.

As this is example code it might be nice to change the example to this instead:

    print("Connection from", connection.device)
    while connection.is_connected():
        temp_characteristic.notify(connection)
        await asyncio.sleep_ms(5_000)

which will push change notifications to the connected client every 5 sec or so, and not disconnect mysteriously (for micropython noobs like me in any case) after 60 seconds.

I can make a PR for this if desired.

Remco-Schoeman avatar Feb 16 '24 23:02 Remco-Schoeman

I think this kind of issue was solved already by the merged PRs of 835 and 497.

ekspla avatar Sep 12 '24 07:09 ekspla

closing per the above comment. Please do feel free to re-open if you think additional clarification is needed.

Josverl avatar Nov 13 '25 00:11 Josverl