Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE Connection problem with BLED112 on Windows #24

Closed
mivola opened this issue Mar 12, 2019 · 3 comments
Closed

BLE Connection problem with BLED112 on Windows #24

mivola opened this issue Mar 12, 2019 · 3 comments

Comments

@mivola
Copy link

mivola commented Mar 12, 2019

Hi guys,

I'm a complete noon to Python and this whole BLE topic. I bought a BLED112 and think I got it installed correctly as I can connect to the MoveHub using "Bluegiga BLE GUI Tool".

Now I want to run the examples from this project using the PyCharm IDE. I cloned the pylgbst repo from my own fork and started playing around. Unfortunately I get an error when I try to run the demo.py (using right click => "Run/Debug 'demo'":

python.exe C:/git/pylgbst/examples/demo.py
INFO:root:Trying get_connection_bluepy
INFO:root:Trying get_connection_bluegiga
INFO:pygatt.backends.bgapi.bgapi:Initialized new BGAPI backend
INFO:pygatt.backends.bgapi.bgapi:Auto-detecting serial port for BLED112
INFO:pygatt.backends.bgapi.bgapi:Found BLED112 on serial port COM3
INFO:pygatt.backends.bgapi.bgapi:Resetting and reconnecting to device for a clean environment
INFO:pygatt.backends.bgapi.bgapi:Auto-detecting serial port for BLED112
INFO:pygatt.backends.bgapi.bgapi:Found BLED112 on serial port COM3
INFO:root:Trying get_connection_gatt
INFO:root:Trying get_connection_gattool
INFO:root:Trying get_connection_gattlib
Traceback (most recent call last):
  File "C:/git/pylgbst/examples/demo.py", line 188, in <module>
    connection = DebugServerConnection()
  File "C:\git\pylgbst\pylgbst\comms\__init__.py", line 146, in __init__
    self.sock.connect(('localhost', port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/git/pylgbst/examples/demo.py", line 191, in <module>
    connection = get_connection_auto()
  File "C:\git\pylgbst\pylgbst\__init__.py", line 58, in get_connection_auto
    raise Exception("Failed to autodetect connection, make sure you have installed prerequisites")
Exception: Failed to autodetect connection, make sure you have installed prerequisites

Process finished with exit code 1

According to the output from pygatt.backends.bgapi.bgapi I would assume the Bluetooth adapter is found, but it does not search for any devices. Is there anything I missed during setup/installation?

Thanks a lot!
Michael

@undera
Copy link
Owner

undera commented Mar 15, 2019

Hi,
Indeed, in case of Windows you only need to try the bluegiga way. So the error you see is irrelevant on Windows. You need to concentrate on BLED112. I myself don't have Windows nor BLED112, so I can only guess.
First thing to do, is instead of automatic connection, use hub=MoveHub(get_connection_bluegiga()) always, so you won't get irrelevant messages. Then, to get potentially more diagnostics, add import logging; logging.basicConfig(level=logging.DEBUG); before getting MoveHub connection.

Please share the results here.

@mivola
Copy link
Author

mivola commented Mar 15, 2019

Hi Andrey,

I changed the logging and the initialization of the MoveHub as suggested. This changed the error message:

DEBUG:comms-pygatt:Trying to connect client to MoveHub with MAC: None
INFO:pygatt.backends.bgapi.bgapi:Initialized new BGAPI backend
DEBUG:pygatt.backends.bgapi.bgapi:Opening connection to serial port (attempt 1)
INFO:pygatt.backends.bgapi.bgapi:Auto-detecting serial port for BLED112
DEBUG:pygatt.backends.bgapi.util:Found 1 serial USB devices
DEBUG:pygatt.backends.bgapi.util:Checking serial USB device: COM3 - USB Serial Device (COM3)
DEBUG:pygatt.backends.bgapi.util:USB device: USB Serial Device (COM3) VID=0x2458 PID=0x0001 on COM3
INFO:pygatt.backends.bgapi.bgapi:Found BLED112 on serial port COM3
INFO:pygatt.backends.bgapi.bgapi:Resetting and reconnecting to device for a clean environment
DEBUG:pygatt.backends.bgapi.bgapi:Opening connection to serial port (attempt 1)
INFO:pygatt.backends.bgapi.bgapi:Auto-detecting serial port for BLED112
DEBUG:pygatt.backends.bgapi.util:Found 1 serial USB devices
DEBUG:pygatt.backends.bgapi.util:Checking serial USB device: COM3 - USB Serial Device (COM3)
DEBUG:pygatt.backends.bgapi.util:USB device: USB Serial Device (COM3) VID=0x2458 PID=0x0001 on COM3
INFO:pygatt.backends.bgapi.bgapi:Found BLED112 on serial port COM3
DEBUG:pygatt.backends.bgapi.bgapi:Failed to open serial port
Traceback (most recent call last):
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygatt\backends\bgapi\bgapi.py", line 159, in _open_serial_port
    timeout=0.25)
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialwin32.py", line 31, in __init__
    super(Serial, self).__init__(*args, **kwargs)
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialutil.py", line 240, in __init__
    self.open()
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialwin32.py", line 78, in open
    self._reconfigure_port()
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialwin32.py", line 222, in _reconfigure_port
    'Original message: {!r}'.format(ctypes.WinError()))
serial.serialutil.SerialException: Cannot configure port, something went wrong. Original message: FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)
Traceback (most recent call last):
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygatt\backends\bgapi\bgapi.py", line 159, in _open_serial_port
    timeout=0.25)
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialwin32.py", line 31, in __init__
    super(Serial, self).__init__(*args, **kwargs)
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialutil.py", line 240, in __init__
    self.open()
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialwin32.py", line 78, in open
    self._reconfigure_port()
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialwin32.py", line 222, in _reconfigure_port
    'Original message: {!r}'.format(ctypes.WinError()))
serial.serialutil.SerialException: Cannot configure port, something went wrong. Original message: FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/git/pylgbst/examples/demo.py", line 194, in <module>
    connection = get_connection_bluegiga()
  File "C:\git\pylgbst\pylgbst\__init__.py", line 12, in get_connection_bluegiga
    return BlueGigaConnection().connect(hub_mac)
  File "C:\git\pylgbst\pylgbst\comms\cpygatt.py", line 27, in connect
    adapter.start()
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygatt\backends\bgapi\bgapi.py", line 202, in start
    self._open_serial_port()
  File "C:\Users\mvoigt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygatt\backends\bgapi\bgapi.py", line 170, in _open_serial_port
    "No BGAPI compatible device detected")
pygatt.exceptions.NotConnectedError: No BGAPI compatible device detected

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/git/pylgbst/examples/demo.py", line 199, in <module>
    connection.disconnect()
NameError: name 'connection' is not defined

Process finished with exit code 1

Then I started debugging with some breakpoints in bgapi.py, serialwin32.py, cpygatt.py etc - and without changing the code or any variables, the connection was working fine afterwards...

pydev debugger: process 10260 is connecting

Connected to pydev debugger (build 183.5912.18)
INFO:pygatt.backends.bgapi.bgapi:Initialized new BGAPI backend
INFO:pygatt.backends.bgapi.bgapi:Auto-detecting serial port for BLED112
INFO:pygatt.backends.bgapi.bgapi:Found BLED112 on serial port COM3
INFO:pygatt.backends.bgapi.bgapi:Resetting and reconnecting to device for a clean environment
INFO:pygatt.backends.bgapi.bgapi:Auto-detecting serial port for BLED112
INFO:pygatt.backends.bgapi.bgapi:Found BLED112 on serial port COM3
INFO:pygatt.backends.bgapi.bgapi:Running receiver
INFO:pygatt.backends.bgapi.bgapi:Disabling advertising
INFO:comms-pygatt:Discovering devices...
INFO:pygatt.backends.bgapi.bgapi:Starting an active scan
INFO:pygatt.backends.bgapi.bgapi:Pausing for 1s to allow scan to complete
INFO:pygatt.backends.bgapi.bgapi:Stopping scan
INFO:pygatt.backends.bgapi.bgapi:Discovered 1 devices: [{'address': '00:16:53:AA:7B:F2', 'name': 'LEGO Move Hub', 'rssi': -55, 'packet_data': {'connectable_advertisement_packet': {'manufacturer_specific_data': bytearray(b'\x97\x03\x00@\x06\xfeA\x00'), 'flags': bytearray(b'\x06'), 'complete_list_128-bit_service_class_uuids': ['0x00:00:16:23:12:12:EF:DE:16:23:78:5F:EA:BC:D1:23']}, 'scan_response_packet': {'slave_connection_interval_range': bytearray(b'\x10\x00 \x00'), 'tx_power_level': bytearray(b'\x00'), 'complete_local_name': 'LEGO Move Hub'}}}]
INFO:root:Found LEGO Move Hub at 00:16:53:AA:7B:F2
INFO:pygatt.backends.bgapi.bgapi:Connecting to device at address 00:16:53:AA:7B:F2 (timeout 5s)
INFO:pygatt.backends.bgapi.bgapi:Connection status: handle=0x0, flags=(242, 123, 170, 83, 22, 0), address=0xb'f27baa531600', connection interval=212.500000ms, timeout=830, latency=22 intervals, bonding=0x0
INFO:pygatt.backends.bgapi.bgapi:Connected to 00:16:53:AA:7B:F2
INFO:pygatt.backends.bgapi.bgapi:Fetching characteristics for connection 0
INFO:pygatt.backends.bgapi.bgapi:Found approved characteristic 00002a05-0000-1000-8000-00805f9b34fb
INFO:pygatt.backends.bgapi.bgapi:Found approved characteristic 00002a00-0000-1000-8000-00805f9b34fb
INFO:pygatt.backends.bgapi.bgapi:Found approved characteristic 00002a01-0000-1000-8000-00805f9b34fb
INFO:pygatt.backends.bgapi.bgapi:Found approved characteristic 00002a04-0000-1000-8000-00805f9b34fb
INFO:pygatt.backends.bgapi.bgapi:Found custom characteristic 00001624-1212-efde-1623-785feabcd123
INFO:pygatt.backends.bgapi.bgapi:Characteristic 0x00002a05-0000-1000-8000-00805f9b34fb is handle 0x3
INFO:pygatt.backends.bgapi.bgapi:Characteristic descriptor 0x00002902-0000-1000-8000-00805f9b34fb is handle 0x4
INFO:pygatt.backends.bgapi.bgapi:Characteristic 0x00002a00-0000-1000-8000-00805f9b34fb is handle 0x7
INFO:pygatt.backends.bgapi.bgapi:Characteristic 0x00002a01-0000-1000-8000-00805f9b34fb is handle 0x9
INFO:pygatt.backends.bgapi.bgapi:Characteristic 0x00002a04-0000-1000-8000-00805f9b34fb is handle 0xb
INFO:pygatt.backends.bgapi.bgapi:Characteristic 0x00001624-1212-efde-1623-785feabcd123 is handle 0xe
INFO:pygatt.backends.bgapi.bgapi:Characteristic descriptor 0x00002902-0000-1000-8000-00805f9b34fb is handle 0xf
INFO:pygatt.device:Subscribed to uuid=00001624-1212-efde-1623-785feabcd123
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0f0004020125000000001000000010'
INFO:movehub:Attached DISTANCE_COLOR_SENSOR on port D
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0f0004370127000000001000000010'
INFO:movehub:Attached MOTOR on port A
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0f0004380127000000001000000010'
INFO:movehub:Attached MOTOR on port B
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'090004390227003738'
INFO:movehub:Attached MOTOR on port AB
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0f0004320117000000001000000010'
INFO:movehub:Attached LED on port LED
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0f00043a0128000000001000000002'
INFO:movehub:Attached TILT_SENSOR on port TILT_SENSOR
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0f00043b0115000200000002000000'
INFO:movehub:Attached AMPERAGE on port AMPERAGE
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0f00043c0114000200000002000000'
INFO:movehub:Attached VOLTAGE on port VOLTAGE
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0f0004010126000000001000000010'
INFO:movehub:Attached IMOTOR on port C
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'12000101064c45474f204d6f766520487562'
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'14000108064c45474f2053797374656d20412f53'
INFO:movehub:bytearray(b'LEGO Move Hub') by bytearray(b'LEGO System A/S')
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0a00473c000000000001'
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0600453c6d0b'
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0600453c6c0b'
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0600453c6c0b'
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0600453c6c0b'
INFO:pygatt.device:Received notification on handle=0xe, value=0xb'0a00473c000000000000'
INFO:movehub:Voltage: 71%

So I have the feeling this is an issue in the related libraries for the BLE/socket/COM communication...

Regards
Michael

@mivola mivola changed the title run demo.py with BLED112 on Windows (PyCharm) BLE Connection problem with BLED112 on Windows Mar 15, 2019
@mivola
Copy link
Author

mivola commented Mar 15, 2019

OK, after some more investigation, this issue is caused by peplin/pygatt#208 and the fix is here: peplin/pygatt#181

@mivola mivola closed this as completed Mar 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants