ble2mqtt icon indicating copy to clipboard operation
ble2mqtt copied to clipboard

Bluetooth interface requires periodic reset

Open kalbfuss opened this issue 2 years ago • 3 comments

I noticed that bluetooth connections have a tendency to fail on one of my Raspberry Pi Zero W running ble2mqtt under RaspbianOS (Debian). Initially, ble2mqtt is able to connect to Xiaomi LYWSD03MMC temperature and humidity sensor devices. After a while, connections always fail and I start to see the following error messages in the log:

...
Jan 21 20:50:52 bridge2 ble2mqtt[327]: WARNING:ble2mqtt.ble2mqtt:Error while connecting to Xiaomi_LYWSD03MMC_a4c138736b80, Device with address A4:C1:38:73:6B:80 was >
Jan 21 20:51:53 bridge2 ble2mqtt[327]: ERROR:ble2mqtt.__main__:Caught exception: {'message': 'Task was destroyed but it is pending!', 'task': <Task pending name='Tas>
Jan 21 20:51:53 bridge2 ble2mqtt[327]: ERROR:asyncio:Task was destroyed but it is pending!
Jan 21 20:51:53 bridge2 ble2mqtt[327]: task: <Task pending name='Task-256' coro=<BleakClientBlueZDBus._disconnect_monitor() running at /usr/local/lib/python3.9/dist->
Jan 21 20:51:53 bridge2 ble2mqtt[327]: INFO:ble2mqtt.__main__:Ignore this exception.

After reset of the bluetooth interface, everything seems to work again until the next failure:

$ sudo hciconfig hci0 reset

Any idea, what might be causing this problem? Obviously, I am interested in a stable setup that does not require any manual interventions.

kalbfuss avatar Jan 21 '22 20:01 kalbfuss

Hey, thank you for the report. Unfortunately, this issue cannot be easily fixed. Here is an issue in bleak https://github.com/hbldh/bleak/issues/500 In short, parsing DBUS messages in python is not optimized. And for some reason, the number of messages grows over time, thus consuming more CPU time. For IoT devices it can lead to freezes and outages.

devbis avatar Jan 22 '22 09:01 devbis

Thanks for the prompt answer. I am not sure whether it is the same issue. In fact, ble2mqtt does not use a lot of CPU time on the Pi Zero. I periodically see it flashing up in top with about 20% CPU usage, but not more and neither permanently. Besides, the LYWSD03MMC devices do not change their bluetooth address. So this problem might be something else. Would you be interested in a debug level log?

kalbfuss avatar Jan 22 '22 16:01 kalbfuss

I should probably add that connection to the sensors is not very reliable as I am operating at the range limit. Ble2mqtt sometimes fails to find and connect to the sensors. Looking at the log I get the impression that the connection task is destroyed, while it is still trying to connect to a device, possibly because the next poll is already due. Might that be the cause of the observed behaviour? In this case it should help to reduce the timeout interval or increase the poll interval.

kalbfuss avatar Jan 22 '22 16:01 kalbfuss