virantha / bricknil

Control LEGO Bluetooth Sensors and Motors with Python
https://virantha.github.io/bricknil
Apache License 2.0
143 stars 39 forks source link

Raspberry Pi: Software caused connection abort #33

Open tylerrmit opened 1 year ago

tylerrmit commented 1 year ago

I have connected a Large motor to port A on a Technic Hub, to power a Lego Loop Coaster. I've been able to control it via Bricknil on my Macbook Pro, but I can't get it to work on a Raspberry Pi. Almost every time I run my program on the Raspberry Pi, I get the following error:

`` INFO:BLE Event Q.0:found device Technic Hub DEBUG:bleak.backends.bluezdbus.client:Connecting to BLE device @ 90:84:2B:70:74:8C with hci0 DEBUG:bleak.backends.bluezdbus.discovery:Technic Hub, 90:84:2B:70:74:8C (-74 dBm), Object Path: /org/bluez/hci0/dev_90_84_2B_70_74_8C Traceback (most recent call last): File "/home/tyler/lego1/lib/python3.9/site-packages/bleak/backends/bluezdbus/client.py", line 69, in connect await self._bus.callRemote( txdbus.error.RemoteError: org.bluez.Error.Failed: Software caused connection abort

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/tyler/lego1/./bricknil_test.py", line 21, in start(system) File "/home/tyler/lego1/lib/python3.9/site-packages/bricknil/bricknil.py", line 214, in start ble.run() File "/home/tyler/lego1/lib/python3.9/site-packages/bricknil/bleak_interface.py", line 52, in run self.loop.run_until_complete(self.asyncio_loop()) File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete return future.result() File "/home/tyler/lego1/lib/python3.9/site-packages/bricknil/bleak_interface.py", line 71, in asyncio_loop await device.connect() File "/home/tyler/lego1/lib/python3.9/site-packages/bleak/backends/bluezdbus/client.py", line 78, in connect raise BleakError(str(e)) bleak.exc.BleakError: org.bluez.Error.Failed: Software caused connection abort ``

It works every time on the Macbook Pro. It worked once out of maybe 50 attempts on the Raspberry Pi.

Does anyone have a combination of Raspberry Pi OS / Python / Bricknil / Bleak versions that is working well for them?

Steps to reproduce:

Successfully installed attrs-23.1.0 automat-22.10.0 bluebrick-Adafruit-BluefruitLE-0.9.13 bricknil-0.9.3 bricknil-bleak-0.3.1 constantly-15.1.0 curio-1.6 future-0.18.3 hyperlink-21.0.0 idna-3.4 incremental-22.10.0 pyyaml-6.0.1 six-1.16.0 twisted-23.8.0 txdbus-1.1.2 typing-extensions-4.7.1 zope-interface-6.0

Then run the following Python program, based on the technic 4x4 example:

``

! /usr/bin/env python3

import logging from curio import sleep from bricknil import attach, start from bricknil.hub import CPlusHub from bricknil.sensor.motor import CPlusLargeMotor

@attach(CPlusLargeMotor, name='loop_motor', port=0) class Coaster(CPlusHub): async def run(self): self.message_info("Running") await self.loop_motor.set_speed(50) await sleep(20)

async def system(): hub = Coaster('loop_hub', True)

if name == 'main': logging.basicConfig(level=logging.DEBUG) start(system) ``

If I press the button on the hub immediately before running the Python program, or soon after, I can see that it discovers the hub OK, but fails when it tries to connect.

tylerrmit commented 1 year ago

I am getting much better stability by setting query_port_info=False when creaating the CPlusHub object. I haven't had a "Software caused connection abort" failure since switching it from True to false.

I do still get a lot of error messages like this one:

Unhandled Error Traceback (most recent call last): File "/home/tyler/lego1/lib/python3.9/site-packages/txdbus/protocol.py", line 140, in dataReceived self.rawDBusMessageReceived(raw_msg) File "/home/tyler/lego1/lib/python3.9/site-packages/txdbus/protocol.py", line 266, in rawDBusMessageReceived self.signalReceived(m) File "/home/tyler/lego1/lib/python3.9/site-packages/txdbus/client.py", line 642, in signalReceived self.router.routeMessage(msig) File "/home/tyler/lego1/lib/python3.9/site-packages/txdbus/router.py", line 128, in routeMessage r.match(m) --- <exception caught here> --- File "/home/tyler/lego1/lib/python3.9/site-packages/txdbus/router.py", line 67, in match self.callback(m) File "/home/tyler/lego1/lib/python3.9/site-packages/bleak/backends/bluezdbus/discovery.py", line 65, in parse_msg device_interface = message.body[1].get("org.bluez.Device1", {}) builtins.AttributeError: 'list' object has no attribute 'get'

Earlier in the day, I tried lots of different Raspberry Pi images from 2020 to 2022, and tried a Raspberry Pi Zero W. None of that seemed to help much.