cyrils / renogy-bt

Python library to read Renogy compatible BT-1 or BT-2 bluetooth modules using Raspberry Pi.
GNU General Public License v3.0
75 stars 27 forks source link

Error when using raspberry pi 4 #46

Open dawson-friesenhahn opened 7 months ago

dawson-friesenhahn commented 7 months ago

I've been using your repo on a very old raspberry pi model B v 2.0 running Linux raspberry pi 6.1.21+ with great success. I had to use a USB dongle for Bluetooth, but it worked without a hitch.

I recently upgraded to a raspberry pi model 4B running Linux raspberrypi 6.1.0-rpi6-rpi-v8 #1 SMP PREEMPT Debian 1:6.1.58-1+rpt2, and I cant for the life of me get connected. The error message comes from gatt I believe, and isn't very helpful:

(.venv) pi@raspberrypi:~/renogy-bt $ python example.py INFO:root:Init RoverClient: BT-TH-774B0D88 => f4:60:77:4b:0d:88 INFO:root:Adapter status - Powered: True INFO:root:Starting discovery... INFO:root:Devices found: 16 INFO:root:Found matching device BT-TH-774B0D88 => [f4:60:77:4b:0d:88] ERROR:root:Connection failed: le-connection-abort-by-local

I've tried using both the pi 4's native Bluetooth hardware and the old functioning Bluetooth dongle with similar results. bluetooth, bluez, and gatt are all the most recent versions.

Also tried:

  1. Upgraded power supply to 20 watts
  2. Pasted identical Bluetooth config files from functioning raspberry pi
  3. Apt updated everything
  4. Reflashed
  5. Rebooted the Bluetooth controller of the renergy Rover charge controller

Any advice in terms of avenues I should pursue for further debugging?

cyrils commented 7 months ago

I haven't really tested it on Pi4 hardware. What does your etc/bluetooth/main.conf say? Have you tried using an older version of bluez (like 5.5)?

emresaglam commented 5 months ago

I created a docker container for renogy-bt for linux/arm/v8 and I'm using it with my Rpi4. If interested I can throw a quick write up. It might help you @dawson-friesenhahn

dawson-friesenhahn commented 5 months ago

That would be fantastic. I've put this project on the back burner for now, but that write-up could prove very useful once I'm able to pick it up again. Thanks!

dawson-friesenhahn commented 5 months ago

Also, my /etc/bluetooth/main.conf:

[General]
# Defaults to 'BlueZ X.YZ', if Name is not set here and plugin 'hostname' is not loaded.
# The plugin 'hostname' is loaded by default and overides the Name set here so
# consider modifying /etc/machine-info with variable PRETTY_HOSTNAME=<NewName> instead.
#Name = BlueZ
# Default device class. Only the major and minor device class bits are
# considered. Defaults to '0x000000'.
#Class = 0x000100
# How long to stay in discoverable mode before going back to non-discoverable
# The value is in seconds. Default is 180, i.e. 3 minutes.
# 0 = disable timer, i.e. stay discoverable forever
#DiscoverableTimeout = 0
# Always allow pairing even if there are no agent registered
# Possible values: true, false
# Default: false
#AlwaysPairable = false
# How long to stay in pairable mode before going back to non-discoverable
# The value is in seconds. Default is 0.
# 0 = disable timer, i.e. stay pairable forever
#PairableTimeout = 0
# Use vendor id source (assigner), vendor, product and version information for
# DID profile support. The values are separated by ":" and assigner, VID, PID
# and version.
# Possible vendor id source values: bluetooth, usb (defaults to usb)
#DeviceID = bluetooth:1234:5678:abcd
# Do reverse service discovery for previously unknown devices that connect to
# us. For BR/EDR this option is really only needed for qualification since the
# BITE tester doesn't like us doing reverse SDP for some test cases, for LE
# this disables the GATT client functionally so it can be used in system which
# can only operate as peripheral.
# Defaults to 'true'.
#ReverseServiceDiscovery = true
# Enable name resolving after inquiry. Set it to 'false' if you don't need
# remote devices name and want shorter discovery cycle. Defaults to 'true'.
#NameResolving = true
# Enable runtime persistency of debug link keys. Default is false which
# makes debug link keys valid only for the duration of the connection
# that they were created for.
#DebugKeys = false
# Restricts all controllers to the specified transport. Default value
# is "dual", i.e. both BR/EDR and LE enabled (when supported by the HW).
# Possible values: "dual", "bredr", "le"
#ControllerMode = dual
# Enables Multi Profile Specification support. This allows to specify if
# system supports only Multiple Profiles Single Device (MPSD) configuration
# or both Multiple Profiles Single Device (MPSD) and Multiple Profiles Multiple
# Devices (MPMD) configurations.
# Possible values: "off", "single", "multiple"
#MultiProfile = off
# Permanently enables the Fast Connectable setting for adapters that
# support it. When enabled other devices can connect faster to us,
# however the tradeoff is increased power consumptions. This feature
# will fully work only on kernel version 4.1 and newer. Defaults to
# 'false'.
#FastConnectable = false
# Default privacy setting.
# Enables use of private address.
# Possible values: "off", "device", "network"
# "network" option not supported currently
# Defaults to "off"
# Privacy = off
# Specify the policy to the JUST-WORKS repairing initiated by peer
# Possible values: "never", "confirm", "always"
# Defaults to "never"
#JustWorksRepairing = never
# How long to keep temporary devices around
# The value is in seconds. Default is 30.
# 0 = disable timer, i.e. never keep temporary devices
#TemporaryTimeout = 30
# Enables the device to issue an SDP request to update known services when
# profile is connected. Defaults to true.
#RefreshDiscovery = true
[Controller]
# The following values are used to load default adapter parameters.  BlueZ loads# the values into the kernel before the adapter is powered if the kernel
# supports the MGMT_LOAD_DEFAULT_PARAMETERS command. If a value isn't provided,
# the kernel will be initialized to it's default value.  The actual value will
# vary based on the kernel version and thus aren't provided here.
# The Bluetooth Core Specification should be consulted for the meaning and valid# domain of each of these values.
# BR/EDR Page scan activity configuration
#BRPageScanType=
#BRPageScanInterval=
#BRPageScanWindow=
# BR/EDR Inquiry scan activity configuration
#BRInquiryScanType=
#BRInquiryScanInterval=
#BRInquiryScanWindow=
# BR/EDR Link supervision timeout
#BRLinkSupervisionTimeout=
# BR/EDR Page Timeout
#BRPageTimeout=
# BR/EDR Sniff Intervals
#BRMinSniffInterval=
#BRMaxSniffInterval=
# LE advertisement interval (used for legacy advertisement interface only)
#LEMinAdvertisementInterval=
#LEMaxAdvertisementInterval=
#LEMultiAdvertisementRotationInterval=
# LE scanning parameters used for passive scanning supporting auto connect
# scenarios
#LEScanIntervalAutoConnect=
#LEScanWindowAutoConnect=
# LE scanning parameters used for passive scanning supporting wake from suspend
# scenarios
#LEScanIntervalSuspend=
#LEScanWindowSuspend=
# LE scanning parameters used for active scanning supporting discovery
# proceedure
#LEScanIntervalDiscovery=
#LEScanWindowDiscovery=
# LE scanning parameters used for passive scanning supporting the advertisement
# monitor Apis
#LEScanIntervalAdvMonitor=
#LEScanWindowAdvMonitor=
# LE scanning parameters used for connection establishment.
#LEScanIntervalConnect=
#LEScanWindowConnect=
# LE default connection parameters.  These values are superceeded by any
# specific values provided via the Load Connection Parameters interface
#LEMinConnectionInterval=
#LEMaxConnectionInterval=
#LEConnectionLatency=
#LEConnectionSupervisionTimeout=
#LEAutoconnecttimeout=
[GATT]
# GATT attribute cache.
# Possible values:
# always: Always cache attributes even for devices not paired, this is
# recommended as it is best for interoperability, with more consistent
# reconnection times and enables proper tracking of notifications for all
# devices.
# yes: Only cache attributes of paired devices.
# no: Never cache attributes
# Default: always
#Cache = always
# Minimum required Encryption Key Size for accessing secured characteristics.
# Possible values: 0 and 7-16. 0 means don't care.
# Defaults to 0
#KeySize = 0
# Exchange MTU size.
# Possible values: 23-517
# Defaults to 517
#ExchangeMTU = 517
# Number of ATT channels
# Possible values: 1-5 (1 disables EATT)
# Default to 3
#Channels = 3
[Policy]
#
# The ReconnectUUIDs defines the set of remote services that should try
# to be reconnected to in case of a link loss (link supervision
# timeout). The policy plugin should contain a sane set of values by
# default, but this list can be overridden here. By setting the list to
# empty the reconnection feature gets disabled.
#ReconnectUUIDs=00001112-0000-1000-8000-00805f9b34fb,0000111f-0000-1000-8000-00805f9b34fb,0000110a-0000-1000-8000-00805f9b34fb
# ReconnectAttempts define the number of attempts to reconnect after a link
# lost. Setting the value to 0 disables reconnecting feature.
#ReconnectAttempts=7
# ReconnectIntervals define the set of intervals in seconds to use in between
# attempts.
# If the number of attempts defined in ReconnectAttempts is bigger than the
# set of intervals the last interval is repeated until the last attempt.
#ReconnectIntervals=1,2,4,8,16,32,64
# AutoEnable defines option to enable all controllers when they are found.
# This includes adapters present on start as well as adapters that are plugged
# in later on. Defaults to 'false'.
AutoEnable=true
matthewcobb commented 5 months ago

On a Pi5, experiencing the same error. ERROR:root:Connection failed: le-connection-abort-by-local - The only thing I would add is that occasionally it does connect; if you retry the script 30-40 times or such... Once connected it is quite stable however.

matthewcobb commented 5 months ago

I'm not sure if this is useful, but running btmon whilst trying to connect brings up some logs. I have manged to capture both the failed and occasional successful connection. Is there potentially some sort of timeout issue, and only occasionally it manages to connect in the allotted time? Forgive me, this bluetooth debugging is new territory!

Running bluetoothd v5.66 on a Pi5

Failed connections (90% of the time)

        Handle: 64 Address: FC:A8:9B:26:D2:DC (Texas Instruments)
> HCI Event: Command Status (0x0f) plen 4                                                                                                                 #75 [hci0] 36.876365
      LE Read Remote Used Features (0x08|0x0016) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7                                                                                                #76 [hci0] 36.876380
        Type: Passive (0x00)
        Interval: 60.000 msec (0x0060)
        Window: 30.000 msec (0x0030)
        Own address type: Public (0x00)
        Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                               #77 [hci0] 36.876651
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                                                                                    #78 [hci0] 36.876685
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                               #79 [hci0] 36.877437
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 12                                                                                                                 #80 [hci0] 37.162897
      LE Read Remote Used Features (0x04)
        Status: Connection Failed to be Established (0x3e)
        Handle: 64 Address: FC:A8:9B:26:D2:DC (Texas Instruments)
        Features: 0x3f 0x00 0x00 0x08 0x00 0x00 0x00 0x00
          LE Encryption
          Connection Parameter Request Procedure
          Extended Reject Indication
          Peripheral-initiated Features Exchange
          LE Ping
          LE Data Packet Length Extension
          Remote Public Key Validation
> HCI Event: Disconnect Complete (0x05) plen 4                                                                                                            #81 [hci0] 37.163288
        Status: Success (0x00)
        Handle: 64 Address: FC:A8:9B:26:D2:DC (Texas Instruments)
        Reason: Connection Failed to be Established (0x3e)
@ MGMT Event: Device Disconnected (0x000c) plen 8                                                                                                    {0x0001} [hci0] 37.180099
        LE Address: FC:A8:9B:26:D2:DC (Texas Instruments)
        Reason: Unspecified (0x00)

Working connection


        Handle: 64 Address: FC:A8:9B:26:D2:DC (Texas Instruments)
> HCI Event: Command Status (0x0f) plen 4                                                                                                               #420 [hci0] 148.528671
      LE Read Remote Used Features (0x08|0x0016) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7                                                                                              #421 [hci0] 148.528728
        Type: Passive (0x00)
        Interval: 60.000 msec (0x0060)
        Window: 30.000 msec (0x0030)
        Own address type: Public (0x00)
        Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                             #422 [hci0] 148.529034
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                                                                                  #423 [hci0] 148.529122
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                             #424 [hci0] 148.529474
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 12                                                                                                               #425 [hci0] 148.573386
      LE Read Remote Used Features (0x04)
        Status: Success (0x00)
        Handle: 64 Address: FC:A8:9B:26:D2:DC (Texas Instruments)
        Features: 0x3d 0x00 0x00 0x00 0x00 0x00 0x00 0x00
          LE Encryption
          Extended Reject Indication
          Peripheral-initiated Features Exchange
          LE Ping
          LE Data Packet Length Extension
< ACL Data TX: Handle 64 flags 0x00 dlen 7                                                                                                              #426 [hci0] 148.573711
      ATT: Exchange MTU Request (0x02) len 2
        Client RX MTU: 517
> HCI Event: LE Meta Event (0x3e) plen 11                                                                                                               #427 [hci0] 148.574104
      LE Data Length Change (0x07)
        Handle: 64 Address: FC:A8:9B:26:D2:DC (Texas Instruments)
        Max TX octets: 251
        Max TX time: 2120
        Max RX octets: 27
        Max RX time: 328
> ACL Data RX: Handle 64 flags 0x02 dlen 7                                                                                                              #428 [hci0] 148.768421
      ATT: Exchange MTU Response (0x03) len 2
        Server RX MTU: 251
< ACL Data TX: Handle 64 flags 0x00 dlen 11                                                                                                             #429 [hci0] 148.768663
      ATT: Read By Type Request (0x08) len 6
        Handle range: 0x0001-0xffff
        Attribute type: Server Supported Features (0x2b3a)
> HCI Event: Number of Completed Packets (0x13) plen 5                                                                                                  #430 [hci0] 148.817186
        Num handles: 1
        Handle: 64 Address: FC:A8:9B:26:D2:DC (Texas Instruments)
        Count: 2
> ACL Data RX: Handle 64 flags 0x02 dlen 9                                                                                                              #431 [hci0] 148.865643
      ATT: Error Response (0x01) len 4
        Read By Type Request (0x08)
        Handle: 0x0001
        Error: Attribute Not Found (0x0a)
< ACL Data TX: Handle 64 flags 0x00 dlen 11                                                                                                             #432 [hci0] 148.865799
      ATT: Read By Group Type Request (0x10) len 6
        Handle range: 0x0001-0xffff
        Attribute group type: Primary Service (0x2800)
> ACL Data RX: Handle 64 flags 0x02 dlen 27                                                                                                             #433 [hci0] 148.963291
> ACL Data RX: Handle 64 flags 0x01 dlen 3                                                                                                              #434 [hci0] 148.963764
      ATT: Read By Group Type Response (0x11) len 25
        Attribute data length: 6
        Attribute group list: 4 entries
        Handle range: 0x0001-0x0007
        UUID: Generic Access Profile (0x1800)
        Handle range: 0x0008-0x001a
        UUID: Device Information (0x180a)
        Handle range: 0x001b-0x002b
        UUID: Unknown (0xffd0)
        Handle range: 0x002c-0x0030
        UUID: Unknown (0xfff0)
< ACL Data TX: Handle 64 flags 0x00 dlen 11                                                                                                             #435 [hci0] 148.963891
      ATT: Read By Group Type Request (0x10) len 6
        Handle range: 0x0031-0xffff
        Attribute group type: Primary Service (0x2800)
> HCI Event: Number of Completed Packets (0x13) plen 5                                                                                                  #436 [hci0] 149.012308
        Num handles: 1
        Handle: 64 Address: FC:A8:9B:26:D2:DC (Texas Instruments)
        Count: 2
> ACL Data RX: Handle 64 flags 0x02 dlen 26                                                                                                             #437 [hci0] 149.061239
      ATT: Read By Group Type Response (0x11) len 21
        Attribute data length: 20
        Attribute group list: 1 entry
        Handle range: 0x0031-0xffff
        UUID: Vendor specific (f000ffd0-0451-4000-b000-000000000000)
< ACL Data TX: Handle 64 flags 0x00 dlen 11                                                                                                             #438 [hci0] 149.061428
      ATT: Read By Group Type Request (0x10) len 6
        Handle range: 0x0001-0xffff
        Attribute group type: Secondary Service (0x2801)
> ACL Data RX: Handle 64 flags 0x02 dlen 9                                                                                                              #439 [hci0] 149.158109
      ATT: Error Response (0x01) len 4
        Read By Group Type Request (0x10)
        Handle: 0x0001
        Error: Unsupported Group Type (0x10)
< ACL Data TX: Handle 64 flags 0x00 dlen 11                                                                                                             #440 [hci0] 149.158274
      ATT: Read By Type Request (0x08) len 6
        Handle range: 0x0001-0xffff
        Attribute type: Include (0x2802)
> HCI Event: Number of Completed Packets (0x13) plen 5                                                                                                  #441 [hci0] 149.207016
        Num handles: 1
        Handle: 64 Address: FC:A8:9B:26:D2:DC (Texas Instruments)
        Count: 2
> ACL Data RX: Handle 64 flags 0x02 dlen 9                                                                                                              #442 [hci0] 149.255781
      ATT: Error Response (0x01) len 4
        Read By Type Request (0x08)
        Handle: 0x0001
        Error: Attribute Not Found (0x0a)
< ACL Data TX: Handle 64 flags 0x00 dlen 11                                                                                                             #443 [hci0] 149.255930
      ATT: Read By Type Request (0x08) len 6
        Handle range: 0x0001-0xffff
        Attribute type: Characteristic (0x2803)
> ACL Data RX: Handle 64 flags 0x02 dlen 27                                                                                                             #444 [hci0] 149.353211
> ACL Data RX: Handle 64 flags 0x01 dlen 27                                                                                                             #445 [hci0] 149.353760
> ACL Data RX: Handle 64 flags 0x01 dlen 27                                                                                                             #446 [hci0] 149.354425
> ACL Data RX: Handle 64 flags 0x01 dlen 27                                                                                                             #447 [hci0] 149.355585
> ACL Data RX: Handle 64 flags 0x01 dlen 24                                                                                                             #448 [hci0] 149.402426
      ATT: Read By Type Response (0x09) len 127
        Attribute data length: 7
        Attribute data list: 18 entries
        Handle: 0x0002
        Value: 020300002a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x0003
            Value UUID: Device Name (0x2a00)
        Handle: 0x0004
        Value: 020500012a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x0005
            Value UUID: Appearance (0x2a01)
        Handle: 0x0006
        Value: 020700042a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x0007
            Value UUID: Peripheral Preferred Connection Parameters (0x2a04)
        Handle: 0x0009
        Value: 0e0a00232a
            Properties: 0x0e
              Read (0x02)
              Write Without Response (0x04)
              Write (0x08)
            Value Handle: 0x000a
            Value UUID: System ID (0x2a23)
        Handle: 0x000b
        Value: 020c00242a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x000c
            Value UUID: Model Number String (0x2a24)
        Handle: 0x000d
        Value: 020e00252a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x000e
            Value UUID: Serial Number String (0x2a25)
        Handle: 0x000f
        Value: 021000262a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x0010
            Value UUID: Firmware Revision String (0x2a26)
        Handle: 0x0011
        Value: 021200272a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x0012
            Value UUID: Hardware Revision String (0x2a27)
        Handle: 0x0013
        Value: 021400282a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x0014
            Value UUID: Software Revision String (0x2a28)
        Handle: 0x0015
        Value: 021600292a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x0016
            Value UUID: Manufacturer Name String (0x2a29)
        Handle: 0x0017
        Value: 0218002a2a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x0018
            Value UUID: IEEE 11073-20601 Regulatory Cert. Data List (0x2a2a)
        Handle: 0x0019
        Value: 021a00502a
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x001a
            Value UUID: PnP ID (0x2a50)
        Handle: 0x001c
        Value: 0e1d00d1ff
            Properties: 0x0e
              Read (0x02)
              Write Without Response (0x04)
              Write (0x08)
            Value Handle: 0x001d
            Value UUID: Unknown (0xffd1)
        Handle: 0x001f
        Value: 122000d2ff
            Properties: 0x12
              Read (0x02)
              Notify (0x10)
            Value Handle: 0x0020
            Value UUID: Unknown (0xffd2)
        Handle: 0x0023
        Value: 082400d3ff
            Properties: 0x08
              Write (0x08)
            Value Handle: 0x0024
            Value UUID: Unknown (0xffd3)
        Handle: 0x0026
        Value: 022700d4ff
            Properties: 0x02
              Read (0x02)
            Value Handle: 0x0027
            Value UUID: Unknown (0xffd4)
        Handle: 0x0029
        Value: 0a2a00d5ff
            Properties: 0x0a
              Read (0x02)
              Write (0x08)
            Value Handle: 0x002a
            Value UUID: Unknown (0xffd5)
        Handle: 0x002d
        Value: 122e00f1ff
            Properties: 0x12
              Read (0x02)
              Notify (0x10)
            Value Handle: 0x002e
            Value UUID: Unknown (0xfff1)
< ACL Data TX: Handle 64 flags 0x00 dlen 11                                                                                                             #449 [hci0] 149.402599
      ATT: Read By Type Request (0x08) len 6
        Handle range: 0x002e-0xffff
        Attribute type: Characteristic (0x2803)
> HCI Event: Number of Completed Packets (0x13) plen 5                                                                                                  #450 [hci0] 149.450725
        Num handles: 1
        Handle: 64 Address: FC:A8:9B:26:D2:DC (Texas Instruments)
        Count: 2```
matthewcobb commented 5 months ago

Just an update, I rewrote the bluetooth management side of this for my own purposes to connect using Bleak. https://github.com/hbldh/bleak and it is now working great on Raspberry Pi 5. It also seems to provide a much simpler way of managing device connections and callbacks.

cyrils commented 5 months ago

@matthewcobb Feel free to raise a PR against the bleak branch.

cyrils commented 3 months ago

Experimental bleak branch is now available