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

ESPHome with bluetooth proxy and iBeacon integration #79781

Closed
avbor opened this issue Oct 7, 2022 · 28 comments · Fixed by #82668
Closed

ESPHome with bluetooth proxy and iBeacon integration #79781

avbor opened this issue Oct 7, 2022 · 28 comments · Fixed by #82668
Assignees

Comments

@avbor
Copy link

avbor commented Oct 7, 2022

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

@homeassistant
Copy link
Contributor

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)

@chrisghu
Copy link

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

@BrianBoRas
Copy link

Same problem here
image

@avbor
Copy link
Author

avbor commented Oct 15, 2022

yep, problem is still there
image
image

@bdraco
Copy link
Member

bdraco commented Oct 15, 2022

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

@avbor
Copy link
Author

avbor commented Oct 15, 2022

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.

@chrisghu
Copy link

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.

@avbor
Copy link
Author

avbor commented Oct 25, 2022

But with the other beacon, the problem is just the opposite, it is always visible as home. Exactly like here - #79833
In my opinion, ibeacon integration at the moment is not working at all.

@avbor
Copy link
Author

avbor commented Nov 3, 2022

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

image

@chrisghu
Copy link

chrisghu commented Nov 5, 2022

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?

@avbor
Copy link
Author

avbor commented Nov 6, 2022

image
still can't use it...

@chrisghu
Copy link

chrisghu commented Nov 7, 2022

@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?

@avbor
Copy link
Author

avbor commented Nov 7, 2022

@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.

@ikrEErki
Copy link

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?

@mkelley88
Copy link

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.

@adwuk
Copy link

adwuk commented Nov 17, 2022

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

@bdraco
Copy link
Member

bdraco commented Nov 23, 2022

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

esphome/issues#3761 (comment)

@chrisghu
Copy link

chrisghu commented Nov 23, 2022

@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?

@aqwserf
Copy link

aqwserf commented Nov 23, 2022

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:

@bdraco
Copy link
Member

bdraco commented Nov 24, 2022

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:

@aqwserf
Copy link

aqwserf commented Nov 24, 2022

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)

@bdraco
Copy link
Member

bdraco commented Nov 24, 2022

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.

@aqwserf
Copy link

aqwserf commented Nov 24, 2022

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

aioesphome.txt

@bdraco
Copy link
Member

bdraco commented Nov 24, 2022

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
Copy link
Member

bdraco commented Nov 24, 2022

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

@aqwserf
Copy link

aqwserf commented Nov 24, 2022

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

@aqwserf
Copy link

aqwserf commented Nov 24, 2022

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

@bdraco
Copy link
Member

bdraco commented Nov 24, 2022

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 added a commit to bdraco/home-assistant that referenced this issue Nov 24, 2022
…ing unavailable

Since we do not call back devices that do not change their advertising
data we need to fetch the latest advertisment/service_info when considering
if the beacon has going unavailable.

fixes home-assistant#79781
frenck pushed a commit that referenced this issue Nov 24, 2022
frenck pushed a commit that referenced this issue Nov 30, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Dec 24, 2022
zelch pushed a commit to zelch/iBeacon that referenced this issue May 2, 2024
zelch pushed a commit to zelch/iBeacon that referenced this issue May 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
9 participants