undera / pylgbst

Python library for LEGO® PoweredUp devices
MIT License
558 stars 121 forks source link

Connection breaks while still instantiating Movehub() #64

Closed tiwo closed 4 years ago

tiwo commented 4 years ago

(Thank you for this nice library!)

I'm experiencing connection issues. I believe they could stem from something in the interplay of pylgbst and my Lego Boost Hub. I would be grateful for any pointers on how to proceed.

I've had errors using Debian on some Laptop and thought I'd try again with a Raspberry, which I suspect is used by other folks with pylgbst.

I'm trying to run this code (from a bash script that automates for a reproducible setup):

conn = get_connection_auto(hub_mac=hub_mac)
hub = MoveHub(connection=conn)

The actual error comes from the respective backend (bluepy (pip freeze): BTLEDisconnectError: Device disconnected, pygatt (pip freeze): No response received / pygatt.exceptions.NotificationTimeout). However it seems that the Move hub disconnects during initialization of the MoveHub instance. During the connection attempt, its light was blue for a second, and according to the logs, the initialization proceeds up to a certain point, and then fails as if the Lego Boost device was disappearing.

I have had a look at #33 , where you had to track a new firmware version. My errors might have a similar cause (but my firmware seems up to date).

undera commented 4 years ago

Hi, Since MoveHub initialization is quite simple, it is either low battery, or BLE adapter hardware issue. But I can only guess, since I don't have my hands on problematic setup. What can help in troubleshooting is to enable Python logging with DEBUG level and see for possible pointers which exact operation correlates to this issue.

coezbek commented 4 years ago

I think I am facing the same issue as @tiwo. With debugging enabled and using all library setups which work (bluez, gatttool, gattlib) sporadically, the furthest I can get is to half a second of blue connected light on the move hub and immediate termination when the first command is sent:

pi@raspi:~ $ sudo python boostic3.py
INFO:root:Trying get_connection_gattool
DEBUG:comms-pygatt:Trying to connect client to MoveHub with MAC: 00:16:53:b4:dc:8f
DEBUG:pygatt.backends.gatttool.gatttool:gatttool_cmd=gatttool -i hci0 -I
INFO:comms-pygatt:Discovering devices...
INFO:pygatt.backends.gatttool.gatttool:Running...
INFO:pygatt.backends.gatttool.gatttool:Starting BLE scan
....
INFO:pygatt.backends.gatttool.gatttool:Found 14 BLE devices
DEBUG:comms-pygatt:Devices: [...{'name': u'Chris Hub', 'address': u'00:16:53:B4:DC:8F'}]
...
DEBUG:comms:Checking device name: Chris Hub, MAC: 00:16:53:B4:DC:8F
INFO:comms:Found Chris Hub at 00:16:53:B4:DC:8F
INFO:pygatt.backends.gatttool.gatttool:Connecting to 00:16:53:B4:DC:8F with timeout=5.0
DEBUG:pygatt.device:Looking up handle for characteristic 00001624-1212-efde-1623-785feabcd123
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a05-0000-1000-8000-00805f9b34fb, value handle: 0x3
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a00-0000-1000-8000-00805f9b34fb, value handle: 0x7
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a01-0000-1000-8000-00805f9b34fb, value handle: 0x9
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a04-0000-1000-8000-00805f9b34fb, value handle: 0xb
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00001624-1212-efde-1623-785feabcd123, value handle: 0xe
DEBUG:pygatt.device:Found <Characteristic uuid=00001624-1212-efde-1623-785feabcd123 handle=14>
DEBUG:pygatt.backends.gatttool.gatttool:Sending cmd=char-write-req 0x0f 0100

This call then just hangs there and does nothing.

Any idea?

tiwo commented 4 years ago

The message corresponding to 0x0f 0100 doesn't seem as suspicious as i first thought: It is originating from this line in the comms submodule, and is part of the BLE stack.

I've excluded EMI and low battery levels as a cause, and have tried three different bluetooth adapters. I too have the <1/2 second of blue light before the disconnection. With pygatt, I see a timeout error 30 seconds after the disconnect.

coezbek commented 4 years ago

I tried with another library bricknil and I get the same issue. I have opened there a issue as well.

https://github.com/virantha/bricknil/issues/16

undera commented 4 years ago

If you get the same behavior with bricknil, it means that problem is not with pylgbst or bricknil, it's somewhere in your hardware or underlying BLE stack.