core icon indicating copy to clipboard operation
core copied to clipboard

ESPHome with bluetooth proxy and iBeacon integration

Open avbor opened this issue 3 years ago • 13 comments

The problem

After upgrading to 2022.10, I tried to set up iBeacon integration along with ESP32 bluetooth proxy and ran into a problem. If I enable iBeacon in the HA app on the phone, I only get single packets through ESP (after reboot ESP or turning signal transmission on and off in the phone). In esphome console with esp32_ble_tracker enabled I also see only single packets from the phone, although they should come every second. At the same time, via BT stick everything works as it should. I can not understand where the problem might be, please help me to understand. image

What version of Home Assistant Core has the issue?

2022.10.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

iBeacon, ESPHome

Link to integration documentation on our website

https://www.home-assistant.io/integrations/ibeacon/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

avbor avatar Oct 07 '22 07:10 avbor

Hey there @bdraco, mind taking a look at this issue as it has been labeled with an integration (ibeacon) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)


ibeacon documentation ibeacon source (message by IssueLinks)

homeassistant avatar Oct 07 '22 07:10 homeassistant

Hi,

Having the same issue here with esphome and home assistant 2022.10.3. ble tracker is not switching reliable between nodes and aber a few minutes the status of the beacon (sent by companion app) will be recognized as unavailable by the ibeacon integration. Reloading the integration will solve the issue for the next minutes, afterwards it turns to unavailable again.

The distance to Bluetooth proxy looks like this:

Screenshot_2022-10-12-22-02-02-35_c3a231c25ed346e59462e84656a70e50

If I can support with any kind of log, please let me know.

Br, chris

chrisghu avatar Oct 12 '22 20:10 chrisghu

Same problem here image

BrianBoRas avatar Oct 14 '22 07:10 BrianBoRas

yep, problem is still there image image

avbor avatar Oct 15 '22 20:10 avbor

Might be fixed by https://github.com/home-assistant/core/pull/79669 but we won't know until 2022.11.x

bdraco avatar Oct 15 '22 21:10 bdraco

Might be fixed by #79669 but we won't know until 2022.11.x

I'm not sure, judging from the description. if you compare the ibeacon tracker from esphome and the ibeacon integration, they behave completely differently (although they should work on the same data).

The sensor from ibeacon esphome: image

at the same time sensor from ibeacon integration with bluetooth proxy from esp32: image

Same tag (HA application), same device (esp32 with esphome) but totally different data. I'm not at all sure it's about timeouts and packets from the tag.

avbor avatar Oct 15 '22 21:10 avbor

Tested very roughly with the dev branch 2022.11 and it seems to be much better. This is just the first impression. I will test it deeper in the next few days. Thanks for the fixes.

chrisghu avatar Oct 16 '22 20:10 chrisghu

But with the other beacon, the problem is just the opposite, it is always visible as home. Exactly like here - https://github.com/home-assistant/core/issues/79833 In my opinion, ibeacon integration at the moment is not working at all.

avbor avatar Oct 25 '22 11:10 avbor

Updating to 2022.11 did not help, still the same problem.

image

avbor avatar Nov 03 '22 07:11 avbor

I also upgraded to 2022.11 and it seems to be much better as already mentioned. Nevertheless I have still "unavailable" slots even if the phone with the home assistant app (which is transmitting the ibeacon) is right next to the esp32.

image

Is there something I can provide to debug the issue? perhaps in my case it is an issue of the App --> esp32 --> ibeacon combination? Any suggestions?

chrisghu avatar Nov 05 '22 14:11 chrisghu

image still can't use it...

avbor avatar Nov 06 '22 10:11 avbor

@avbor what kind of esp32 are you using? In my case I could also imagine that it could be an issue of the board or the esphome side as well. I do not see Bluetooth devices forwarded in home assistant. Furthermore the esp32 seems to crash after some minutes. Caused by Bluetooth traffic?

chrisghu avatar Nov 07 '22 14:11 chrisghu

@chrisghu i have a lot of random board from aliexpress =) some work worse, some work better. For now the boards work more or less stable, I have ~10 Xiaomi BT TH sensors, they work fine through the bluetooth proxy. But the ibeacons are in trouble, I can't use them yet.

avbor avatar Nov 07 '22 14:11 avbor

I'm having the same experience. However, it is weird when I try to figure out the issue.

The bluetooth proxy doesn't track my phone consistently, regardless of beacon app. Tried both HA integrated transmitter and 3party apps. However, they do track my radbeacons without a single drop.

So it is the phone then? Nope, I installed espresense on identical hardware and now my phone is tracked without a single drop as well.

So it is only the combination of btproxy and phone that doesn't work. Could it be the changing addresses that is the issue?

ikrEErki avatar Nov 15 '22 20:11 ikrEErki

The bluetooth proxy doesn't track my phone consistently, regardless of beacon app. Tried both HA integrated transmitter and 3party apps. However, they do track my radbeacons without a single drop.

This is my experience too. While using a Tile as iBeacon, it works as it should. While using android (Samsung Note 10) with HA app or third party app, it is intermittent like in the screenshot. I have checked that I did not have power saving, or any other restrictions but it still isn't consistant.

mkelley88 avatar Nov 16 '22 13:11 mkelley88

I have exactly the same problem. On HA 2022.11.2, and ESPHome 2022.11.1. I have tried all sorts of scan parameters, but still get very inconsistent detection of an android phone sitting next to an ESP32.

Here is the tracking via ESPHome proxy and iBeacon: image

Here is the same phone being tracked by ESPresense, using an identical ESP32, also right next to the phone: image

adwuk avatar Nov 17 '22 11:11 adwuk

There is a stability issue that isn't fixed in the ESP32s yet

There is a branch here if you would like to try the potential fix

https://github.com/esphome/issues/issues/3761#issuecomment-1324176431

bdraco avatar Nov 23 '22 16:11 bdraco

@bdraco Thanks for the suggestion. I have tried the branch you mentioned in the post above but the signal is still interrupted:

image

It seems to be better than before. Thanks for working on that topic. Can I support with some measures?

chrisghu avatar Nov 23 '22 19:11 chrisghu

Same behavior here: Screenshot from 2022-11-23 10-16-46

With a Samsung Galaxy Tab S6 Lite at 10cm from the ESP32. The environment is quite noisy (Wifi, Zigbee...) but according to this post, I'm not alone.

I've tried a bunch of configuration (interval, window,transmit power, advertise mode...) without success.

After filtering on my UUID, I clearly see that the ESP32 is not to blame (even though if sometimes I have no traces for 10s while the Companion application is supposed to send the frames every second).

For reference, here's my ESP32 config:

substitutions:
  name: esp32-2
  comment: "Test"

# Core information that ESPHome needs to create firmwares
esphome:
  name: ${name}
  name_add_mac_suffix: true
  comment: ${comment}
  project:
    name: esphome.${name}
    version: "1.0"

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:
  level: DEBUG
  # logs:
  #   esp32_ble_tracker: VERY_VERBOSE

# Enable Home Assistant API
api:
  # encryption:
  #   key: !secret api_key

# Upload firmwares binaries without having to use a USB
ota:
  safe_mode: true
  password: !secret ota_password

# Wi-Fi connection configuration
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  domain: !secret domain

# Configuring Wi-Fi via USB
improv_serial:

# Buttons to add
button:
- platform: safe_mode
  name: "Safe Mode Boot"
- platform: restart
  name: "Restart"

# Activate webserver
web_server:
  port: 80
  auth:
    username: !secret web_server_user
    password: !secret web_server_password    

dashboard_import:
  package_import_url: github://esphome/bluetooth-proxies/esp32-generic.yaml@main

# Bluetooth configuration
bluetooth_proxy:
  active: true

esp32_ble_tracker:
  scan_parameters:
    interval: 1100ms
    window: 1100ms
    active: true
  on_ble_advertise:
      then:
        - lambda: |-
            if(x.get_ibeacon().has_value()) {
              auto ibeacon = x.get_ibeacon().value();
              if (strcmp(ibeacon.get_uuid().to_string().c_str(), "<UUID>") == 0) {
                ESP_LOGD("ble_adv", "PIXEL3 found: rssi %d", x.get_rssi());
              }
            }

I've been able though to make it work using the ESP32 Bluetooth Low Energy Tracker Hub but it involves additional configuration and I'd like to avoid having a device sensor per ESP.

I've also tried to use the aioesphomeapi to see if it's an API issue without success.

To be accurate, I don't have any bluetooth_tracker or bluetooth_le_tracker in HA, just using the ESPHome bluetooth proxy.

And for reference, some similar issues:

  • https://github.com/esphome/issues/issues/2200
  • https://github.com/esphome/issues/issues/2445

aqwserf avatar Nov 23 '22 22:11 aqwserf

Same behavior here:

Screenshot from 2022-11-23 10-16-46

With a Samsung Galaxy Tab S6 Lite at 10cm from the ESP32. The environment is quite noisy (Wifi, Zigbee...) but according to this post, I'm not alone.

I've tried a bunch of configuration (interval, window,transmit power, advertise mode...) without success.

After filtering on my UUID, I clearly see that the ESP32 is not to blame (even though if sometimes I have no traces for 10s while the Companion application is supposed to send the frames every second).

Can you explain what you mean by that? Are you seeing the device come over the aioesphomeapi connection every second?

For reference, here's my ESP32 config:


substitutions:

  name: esp32-2

  comment: "Test"



# Core information that ESPHome needs to create firmwares

esphome:

  name: ${name}

  name_add_mac_suffix: true

  comment: ${comment}

  project:

    name: esphome.${name}

    version: "1.0"



esp32:

  board: esp32dev

  framework:

    type: arduino



# Enable logging

logger:

  level: DEBUG

  # logs:

  #   esp32_ble_tracker: VERY_VERBOSE



# Enable Home Assistant API

api:

  # encryption:

  #   key: !secret api_key



# Upload firmwares binaries without having to use a USB

ota:

  safe_mode: true

  password: !secret ota_password



# Wi-Fi connection configuration

wifi:

  ssid: !secret wifi_ssid

  password: !secret wifi_password

  domain: !secret domain



# Configuring Wi-Fi via USB

improv_serial:



# Buttons to add

button:

- platform: safe_mode

  name: "Safe Mode Boot"

- platform: restart

  name: "Restart"



# Activate webserver

web_server:

  port: 80

  auth:

    username: !secret web_server_user

    password: !secret web_server_password    



dashboard_import:

  package_import_url: github://esphome/bluetooth-proxies/esp32-generic.yaml@main



# Bluetooth configuration

bluetooth_proxy:

  active: true



esp32_ble_tracker:

  scan_parameters:

    interval: 1100ms

    window: 1100ms

    active: true

  on_ble_advertise:

      then:

        - lambda: |-

            if(x.get_ibeacon().has_value()) {

              auto ibeacon = x.get_ibeacon().value();

              if (strcmp(ibeacon.get_uuid().to_string().c_str(), "<UUID>") == 0) {

                ESP_LOGD("ble_adv", "PIXEL3 found: rssi %d", x.get_rssi());

              }

            }

I've been able though to make it work using the ESP32 Bluetooth Low Energy Tracker Hub but it involves additional configuration and I'd like to avoid having a device sensor per ESP.

I've also tried to use the aioesphomeapi to see if it's an API issue without success.

To be accurate, I don't have any bluetooth_tracker or bluetooth_le_tracker in HA, just using the ESPHome bluetooth proxy.

And for reference, some similar issues:

  • https://github.com/esphome/issues/issues/2200

  • https://github.com/esphome/issues/issues/2445

bdraco avatar Nov 24 '22 00:11 bdraco

Can you explain what you mean by that? Are you seeing the device come over the aioesphomeapi connection every second?

I do but I'm not sure. As I said, I've tried to play with aioesphomeapi without success. I mean, using some code I can see some logs like this:

BluetoothLEAdvertisement(address=85073187394417, name='', rssi=-90, service_uuids=[], service_data={}, manufacturer_data={76: b'\x02\x15$\xd8\x08v\xa7&E\x98\x82\xe3`\xc5%\xaduW\x00d\x00\x01\xc5'})
BluetoothLEAdvertisement(address=85073187394417, name='', rssi=-88, service_uuids=[], service_data={}, manufacturer_data={76: b'\x02\x15$\xd8\x08v\xa7&E\x98\x82\xe3`\xc5%\xaduW\x00d\x00\x01\xc5'})

but my python skills don't allow me to print it with my UUID. And I don't even know if BluetoothLEAdvertisement is the right thing to watch. But if you have some tips, let me know!

Instead, I've watched my ESP log and HA dashboard: Screenshot from 2022-11-24 09-29-35 Screenshot from 2022-11-24 09-29-46

As you can see, the ESP could see my device while HA could not, based on the time.

So I guess ESP is not to blame unless something goes wrong with the API.

BTW, does someone has a Wireshark dissector for the API? Would be great to debug things. Or something to decode API requests in HA?

EDIT: seems like

service: logger.set_level
data:
  aioesphomeapi: debug

did the trick ^^

2022-11-24 09:56:33.847 DEBUG (MainThread) [aioesphomeapi.connection] esp32-2-9dd5e4 @ xxx: Got message of type <class 'api_pb2.BluetoothLEAdvertisementResponse'>: address: 119376193025443
rssi: -92
manufacturer_data {
uuid: "0x004C"
data: "\002\025J\032\003\202\014\002G\374\223RkFz||k\000\000\000\000\277"
}
2022-11-24 09:56:34.147 DEBUG (MainThread) [aioesphomeapi.connection] esp32-2-9dd5e4 @ xxx: Got message of type <class 'api_pb2.BluetoothLEAdvertisementResponse'>: address: 119376193025443
rssi: -90
manufacturer_data {
uuid: "0x004C"
data: "\002\025J\032\003\202\014\002G\374\223RkFz||k\000\000\000\000\277"
}

data is still littile-endianed/encoded/byted but I can see things

EDIT2: seems like Companion is not to blame either. Tried with Beacon Simulator and I have the same behavior.

EDIT3: I think aioesphome is not to blame either, I see a lot a requests coming in HA in debug (even though I'm unable to decode them)

aqwserf avatar Nov 24 '22 08:11 aqwserf

If you turn on debug logs for aioesphomeapi you can see all the Bluetooth packets being forwarded.

I'll need to get a snapshot of the actual packets over a 30m period for the ibeacon device to understand what is going wrong.

bdraco avatar Nov 24 '22 15:11 bdraco

@bdraco : here it is. Probably around an hour of log. Or do you need a pcap?

aioesphome.txt

aqwserf avatar Nov 24 '22 16:11 aqwserf

Just a guess but it looks like it goes unavailable a bit after the source picks a new random address.

It might be seeing the old random address disappear from the stack and marking the beacon unavailable.... just a guess though at this point

bdraco avatar Nov 24 '22 16:11 bdraco

Maybe it's taking so long to rotate the Mac and it never getting fresh data because it never changes and the unavailable time is the time between when the Mac is changed to a new one and the data is last called back because it hasn't changed and the filter won't send it again because it's static

I think we could fix that by having the random unavailable function refetch the latest time for the address from the history

bdraco avatar Nov 24 '22 16:11 bdraco

@bdraco good news! Let me know if you need anything else

aqwserf avatar Nov 24 '22 16:11 aqwserf

If the problem is the MAC address, why I do have a working configuration using ESP32 Bluetooth Low Energy Tracker Hub?

...
esp32_ble_tracker:
  scan_parameters:
    interval: 1100ms
    window: 1100ms
    active: true

binary_sensor:
  - platform: ble_presence
    ibeacon_uuid: !secret phone_ibeacon
    name: "Phone iBeacon"
  - platform: ble_presence
    ibeacon_uuid: !secret tab_ibeacon
    name: "Tablette iBeacon"

using these parameters I do have a better accuracy:

image

aqwserf avatar Nov 24 '22 16:11 aqwserf

There is no filtering layer with the ESP32. You get the full firehose of data to the component which works fine with a low interval and only two or three components. The Bluetooth integration only calls back data when it changes (we can't send every integration the firehose or the system would melt down) and ibeacon data is static

bdraco avatar Nov 24 '22 17:11 bdraco