theengs / gateway

Multi platform Multi devices BLE Bluetooth to MQTT gateway leveraging Theengs Decoder
https://gateway.theengs.io
GNU General Public License v3.0
113 stars 31 forks source link

Gateway in Passive Mode on Raspberry Pi 4 (NixOS/Raspberry Pi 4 Kernel) does not detect Govee H5075 #166

Closed sad-goldfish closed 1 year ago

sad-goldfish commented 1 year ago

Describe the bug Gateway in Passive Mode on Raspberry Pi 4 (NixOS/Raspberry Pi 4 Kernel) does not detect Govee H5075 (or publish any other bluetooth/related messages). These devices are detected in active mode and by OpenMQTTGateway devices.

To Reproduce python -u -m TheengsGateway -H 127.0.0.1 -pa 1 -s passive -ll DEBUG -padv 1 -sd 60 -Df H5072/75 This outputs:

# Theengs Gateway Diagnostics

## Package Versions

| Name               | Value  |
|--------------------|--------|
| Theengs Gateway    | 1.1.0  |
| Theengs Decoder    | 1.5.7  |
| Bleak              | 0.20.2 |
| Bluetooth Clocks   | 0.2.0  |
| Bluetooth Numbers  | 1.1.1  |
| Paho MQTT          | 1.6.1  |
| Bluetooth Adapters | 0.15.4 |

## Python

| Name           | Value                                                                          |
|----------------|--------------------------------------------------------------------------------|
| Version        | 3.10.12                                                                        |
| Implementation | CPython                                                                        |
| Compiler       | GCC 12.2.0                                                                     |
| Executable     | /nix/store/51bbnhsqj15jvl3wfsqbajdmvxdnplai-python3-3.10.12-env/bin/python3.10 |

## Operating System

| Name         | Value                                     |
|--------------|-------------------------------------------|
| System       | Linux                                     |
| Release      | 6.1.21                                    |
| Version      | #1-NixOS SMP Tue Jan  1 00:00:00 UTC 1980 |
| Machine type | aarch64                                   |
| Distribution | NixOS 23.05 (Stoat)                       |

## Configuration

{
    "adapter": "",
    "ble_scan_time": 60,
    "ble_time_between_scans": 5,
    "discovery": 1,
    "discovery_device_name": "TheengsGateway",
    "discovery_filter": [
        "IBEACON",
        "H5072/75"
    ],
    "discovery_topic": "homeassistant/sensor",
    "hass_discovery": 1,
    "host": "127.0.0.1",
    "log_level": "DEBUG",
    "lwt_topic": "home/TheengsGateway/LWT",
    "pass": "***",
    "port": 1883,
    "presence": 0,
    "presence_topic": "home/TheengsGateway/presence",
    "publish_advdata": 1,
    "publish_all": 1,
    "publish_topic": "home/TheengsGateway/BTtoMQTT",
    "scanning_mode": "passive",
    "subscribe_topic": "home/+/BTtoMQTT/undecoded",
    "time_format": 0,
    "time_sync": [],
    "user": "***"
}

## Bluetooth adapters

Default adapter: hci0

### hci0

| Name         | Value                    |
|--------------|--------------------------|
| address      | DC:A6:32:XX:XX:XX        |
| sw_version   | nixos                    |
| hw_version   | usb:v1D6Bp0246d0542      |
| passive_scan | True                     |
| manufacturer | Raspberry Pi Trading Ltd |
| product      | None                     |
| vendor_id    | None                     |
| product_id   | None                     |

INFO:BLEGateway:Starting BLE scan
INFO:BLEGateway:Connected to MQTT Broker!
INFO:BLEGateway:Subscribed to home/+/BTtoMQTT/undecoded
DEBUG:BLEGateway:OMITTED:AdvertisementData(manufacturer_data={76: b'OMITTED'}, tx_power=12, rssi=-63)
DEBUG:BLEGateway:OMITTED:AdvertisementData(manufacturer_data={76: b'OMITTED'}, tx_power=7, rssi=-72)
INFO:BLEGateway:Sent 0 messages to MQTT

However bluetoothd, running as bluetoothd -f /etc/bluetooth/main.conf -d src/adv_monitor.c --experimental outputs something like the following every scan:

Path /org/bleak/13305/547963826912 reserved for Adv Monitor app :1.35
src/adv_monitor.c:parse_rssi_and_timeout() Adv Monitor at /org/bleak/13305/547963826912 initiated with high RSSI threshold 127, high RSSI threshold timeout 0, low RSSI threshold 127, low RSSI threshold timeout 0,>
src/adv_monitor.c:merged_pattern_add() Monitor state: Adding -> Stable
src/adv_monitor.c:monitor_proxy_added_cb() Adv Monitor allocated for the object at path /org/bleak/13305/547963826912
src/adv_monitor.c:add_adv_patterns_monitor_cb() Adv monitor with handle:0x0001 added
src/adv_monitor.c:merged_pattern_process_next_step() Monitor state: Stable -> Stable
src/adv_monitor.c:monitor_state_active() Calling Activate() on Adv Monitor of owner :1.35 at path /org/bleak/13305/547963826912
src/adv_monitor.c:adv_monitor_device_found_callback() hci0 addr OMITTED, rssi -66 flags 0x0000 ad_data_len 17
src/adv_monitor.c:adv_monitor_filter_rssi() Calling DeviceFound() on Adv Monitor of owner :1.35 at path /org/bleak/13305/547963826912
src/adv_monitor.c:adv_monitor_device_found_callback() hci0 addr OMITTED, rssi -87 flags 0x0004 ad_data_len 4
src/adv_monitor.c:adv_monitor_device_found_callback() hci0 addr A4:C1:38:ED:17:14, rssi -44 flags 0x0000 ad_data_len 31
src/adv_monitor.c:adv_monitor_device_found_callback() hci0 addr OMITTED, rssi -90 flags 0x0000 ad_data_len 18
src/adv_monitor.c:adv_monitor_filter_rssi() Calling DeviceFound() on Adv Monitor of owner :1.35 at path /org/bleak/13305/547963826912
src/adv_monitor.c:adv_monitor_device_found_callback() hci0 addr A4:C1:38:B9:5A:9E, rssi -75 flags 0x0000 ad_data_len 31
src/adv_monitor.c:adv_monitor_device_found_callback() hci0 addr A4:C1:38:4C:9D:D3, rssi -84 flags 0x0000 ad_data_len 31
src/adv_monitor.c:adv_monitor_device_found_callback() hci0 addr OMITTED, rssi -68 flags 0x0004 ad_data_len 8
src/adv_monitor.c:adv_monitor_device_found_callback() hci0 addr OMITTED, rssi -94 flags 0x0004 ad_data_len 28
src/adv_monitor.c:monitor_proxy_removed_cb() Adv Monitor removed in state 03 with path /org/bleak/13305/547963826912
src/adv_monitor.c:merged_pattern_remove() Monitor state: Removing -> Stable
src/adv_monitor.c:app_destroy() Destroy Adv Monitor app :1.35 at path /org/bleak/13305/547963826912
Path /org/bleak/13305/547963826912 removed along with Adv Monitor app :1.35
src/adv_monitor.c:remove_adv_monitor_cb() Adv monitor with handle:0x0001 removed from kernel
src/adv_monitor.c:merged_pattern_process_next_step() Monitor state: Stable -> Stable

The Govee devices with MACs starting A4:C1:38 are listed in the bluetoothd log but not the TheengsGateway log and no bluetooth messages are published. This is the case whether or not -Df is used. MACs of different devices are ommited. Expected behavior Govee Sensor data published to the MQTT server.

Additional context hciconfig -a:

hci0:   Type: Primary  Bus: UART
    BD Address: DC:A6:32:5A:04:FC  ACL MTU: 1021:8  SCO MTU: 64:1
    UP RUNNING
    RX bytes:33211 acl:0 sco:0 events:1535 errors:0
    TX bytes:69890 acl:0 sco:0 commands:727 errors:0
    Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
    Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
    Link policy: RSWITCH SNIFF
    Link mode: PERIPHERAL ACCEPT
    Name: 'nixos'
    Class: 0x400000
    Service Classes: Telephony
    Device Class: Miscellaneous,
    HCI Version: 5.0 (0x9)  Revision: 0x17e
    LMP Version: 5.0 (0x9)  Subversion: 0x6119
    Manufacturer: Cypress Semiconductor (305)

sudo hciconfig hci0 lestates

Supported link layer states:
    YES Non-connectable Advertising State
    YES Scannable Advertising State
    YES Connectable Advertising State
    YES Directed Advertising State
    YES Passive Scanning State
    YES Active Scanning State
    YES Initiating State/Connection State in Central Role
    YES Connection State in the Peripheral Role
    YES Non-connectable Advertising State and Passive Scanning State combination
    YES Scannable Advertising State and Passive Scanning State combination
    YES Connectable Advertising State and Passive Scanning State combination
    YES Directed Advertising State and Passive Scanning State combination
    YES Non-connectable Advertising State and Active Scanning State combination
    YES Scannable Advertising State and Active Scanning State combination
    YES Connectable Advertising State and Active Scanning State combination
    YES Directed Advertising State and Active Scanning State combination
    YES Non-connectable Advertising State and Initiating State combination
    YES Scannable Advertising State and Initiating State combination
    YES Non-connectable Advertising State and Central Role combination
    YES Scannable Advertising State and Central Role combination
    YES Non-connectable Advertising State and Peripheral Role combination
    YES Scannable Advertising State and Peripheral Role combination
    YES Passive Scanning State and Initiating State combination
    YES Active Scanning State and Initiating State combination
    YES Passive Scanning State and Central Role combination
    YES Active Scanning State and Central Role combination
    YES Passive Scanning State and Peripheral Role combination
    YES Active Scanning State and Peripheral Role combination
    YES Initiating State and Central Role combination/Central Role and Central Role combination
DigiH commented 1 year ago

These devices are detected in active mode and by OpenMQTTGateway devices.

This is correct, the Govee H5075, as well as other Govee devices, requires active scanning to be recognised and decoded by Theengs Decoder, with either Theengs Gateway or OpenMQTTGateway.

This is due to the fact that the device name needs to be taken into account to differentiate between different Govee device models and their decoders. The device name is only being broadcast with active scanning.

This can also be seen if Advertisement and Advanced Data is set to true for OpenMQTTGateway or -padv PUBLISH_ADVDATA to 1 for Theengs Gateway. The resulting published MQTT JSON then includes "acts":true to indicate that this devide decoder requires active scanning.

sad-goldfish commented 1 year ago

Ah, that explains it @DigiH. Although, I do have -padv 1 but I don't see any JSON message mentioning "acts":true. The only message I see under home/TheengsGateway (mosquitto_sub -v -t home/TheengsGateway/\#) is home/TheengsGateway/LWT online.

EDIT: I do see it when active scanning is turned on.

DigiH commented 1 year ago

@sad-goldfish You should see it once you have active scanning enabled on Theengs Gateway, along with the decoded keys for your H5075.

-s {active,passive}