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
ESP32 BT Classic component #4736
base: dev
Are you sure you want to change the base?
Conversation
Hey there @RoboMagus, CODEOWNERS = ["@RoboMagus"] And run (message by NeedsCodeownersLabel) |
Can this integrate with HA as a drevice tracker, so that eg. a tracked bt mac address could be selected/registered to the user to contribute to the presence state of that user? |
I guess that that should be possible, though I'm not sure if esphome can already expose device tracker entities. |
@nagyrobi After finding feature-request/204 I've done some minor tests that indicate it is possible to have these BT Classic scan results trigger a service: device_tracker.see
data:
host_name: TestDevice
mac: aa:bb:cc:dd:ee:f3
location_name: office
source_type: bluetooth Though for me personally it is a problem that the entitiy that this creates inside HA does not have a However, these newly created entities can then be linked to a person to see if they are at home. |
This is important and worth to be documented. |
In what sense though? All of the options that are currently provided are described in the esphome-docs PR:
The |
That's exactly what the Cookbook part of the site is for. You could link to the examples there. |
ee1c4e4
to
c9b0594
Compare
Hey there @jesserockz, mind taking a look at this pull request as it has been labeled with an integration ( |
3b8b239
to
928520b
Compare
I'm reverting the state of this Pr back to draft for now. |
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions. |
Hey RoboMagus, Do you have any possibility to check why the whole device gets blocked as described above? |
Added my Ipad to the list. But as soon my Iphone is out of reach, the ESPHome firmware freezes or waits. As soon as my Iphone is within reach, the freeze ends and the ESPHome firmware continues execution. I would have expected that if you have defined two or more devices (iphones, ipads), the scan would continue with the next etc and not wait "forever" |
Hey @krambriw, thank you for testing this component as well! What you're describing are exactly the edge-cases that caused my to mark this PR as draft again after some time of development. What exactly is causing these issues I'm not quite sure about, but its the ESP32's BT stack that starts producing garbage at some point. From what I've found is that resetting the BT stack has no impact, but a full reset of the device often solves the weird BT stack responses. Over the holidays I've resumed testing with newer frameworks and though this looks promising so far (no BT stack issues in about 3 days of up-time) I do need to perform more testing before I'd consider the whole thing to be stable enough for integration. If you're able to test on ESP-IDF versions greater than esp32:
framework:
type: esp-idf
version: 5.1.2
platform_version: 6.4.0 Also, could you set logging to |
Hello and thank you! |
So it freezed again. Below is part of the log with my comments inserted. My feeling, just a feeling, is that everything works fine as expected as long as ALL listed devices can be found during the scans. But as soon as one is not found, the scan continues and finds the others but after a while, the scan stopps completely and (part of) the firmware freezes or stops in a kind of waiting state Like in my case, it freezes at 23:56 and just waits. During the following time period the ESPHome is not responding to any mqtt commands. The bluetooth on my ipad is still on but since it is off in my iphone the scan seems halted. At 06:35 bluetooth in my iphone was turned on again. The freeze automatically ended, the ESPHome rebooted, scan started and both my iphone and ipad were found at the first scan Would it not be possible to internally detect this freeze state and then simpy reboot to restart? Best regards, Walter (lines with 'blue' is just turning the LED on/off, lines with blrestart timer is just a watchdog that is supposed to fire & reboot if the the esp would stop executing but it is also not working while the whole firmware is freezed) INFO Upload took 11.77 seconds, waiting for result... [21:24:07][I][app:062]: setup() finished successfully! 21:24:04 Updated and uploaded version started. Both my iphone and ipad where found during scans [21:24:09][D][esp32_bt_classic:136]: Start scanning for 00:11:22:33:44:55 21:25:56 iphone was not found since I turned bluetooth off in the phone [21:25:56][D][esp32_bt_classic:209]: Device '00:11:22:33:44:55' scan result: ESP_BT_STATUS_FAIL (1) 22:51:35 was last time it found my iPad [22:51:00][D][esp32_bt_classic:136]: Start scanning for 55:44:33:22:11:00 23:05:04 last entry in log, firmware freeze, ESPHome freeze, not possible to communicate via mqtt [23:04:59][D][esp32_bt_classic:136]: Start scanning for 55:44:33:22:11:00 06:35:53 turning bluetooth on in my iPhone, ESPHome restarts and scanning starts, my iphone and ipad are now found again [06:36:23][D][esp32_bt_classic:136]: Start scanning for 00:11:22:33:44:55 |
So with the freezing of the device you mean that the whole ESPHome device becomes unresponsive? I first assumed it'd be only the Bluetooth scanning parts, as that would align with the issues I ran into myself.
I did implement a crude BT stack error detection in my local code-base for just this purpose, but simply restarting an ESP device not something I'd want anyone to rely on considering any other tasks that this device could be running. Based on current testing with newer IDF frameworks this error state previously observed seems to have been resolved. Though this probably won't help you much right now, as the symptoms you've shared indicate another issue. Could you share the full device config you're using to test this component with, as well as the specific ESP device you're using? To check how close your device is to hitting memory issues, could you also please add sensors from the debug component to your config? Values that these sensors expose could help in figuring out what exactly is going on. Thanks for your testing btw, even though the issues you're facing could be different from what I'm expecting its good to know if there are some incompatibilities with the BT Classic component so that these can be documented :) |
Yes, exactly. But it can't be completely dead since it recovers as soon as I turns the bluetooth in the phone on again. So it is listening somehow. Once it then moves on it reboots, I think this is due to the setting for the mqtt server connection (reboot timeout: 5 min) Attached is my .yaml file as .txt with the modifications (scanning every 10 sec to stress test) Best regards, Walter |
An update. Happy to report, it has worked just perfectly since I made the update and restarted the device yesterday. It does not freeze anymore, I can send and receieve messages via MQTT. So I cannot see the problem anymore. What I additionally did in the .yaml file was commented out the things controlling the blue LED and the restart timer, don't know if this helped to save some memory or so. Attached is the updated .yaml I have also pasted some parts of the log below, including some debug sensor values (free and fragmentation could however not be used since I have an ESP32) I will let it run like this and we will see how it behaves but for the moment, everything looks ok Best regards, Walter [08:24:34][D][sensor:093]: 'Free PSRAM': Sending state 0.00000 B with 0 decimals of accuracy |
Unfortunately there is something not working. After hours in operation, the scanning does not find my devices any longer. The ESP is not frozen, it still reports log events via MQTT and it can receive commands Below is the log from the time when it stopped working. It did find my iphone perfectly until I left my house (bluetooth in my ipad was off at that time). A short while thereafter, it seems it restarted for some reason. Later when I returned home, it could not find my phone. I then turned on bluetooth in my ipad but same result, it was not able to find it either. I then did reboot the ESP and it finds both. Don't know if this helps [09:57:58][I][esp32_bt_classic:203]: Found device '00:11:22:33:44:55' (Walters iPhone 13) with 4 scans remaining [10:32:49][I][ota:117]: Boot seems successful, resetting boot loop counter. [10:40:04][W][esp32_bt_classic:213]: Device '00:11:22:33:44:55' not found on final scan. Removing from scan list. [15:05:09][I][mqtt:274]: MQTT Connected! [15:05:10][I][app:062]: setup() finished successfully! |
Just some additional info, I now got this error message in the log [17:04:16][I][esp32_bt_classic:203]: Found device '00:11:22:33:44:55' (Walters iPhone 13) with 4 scans remaining [17:23:31][W][esp32_bt_classic:213]: Device '55:44:33:22:11:00' not found on final scan. Removing from scan list. |
It is definitely when I receive this error message the scanning stops from working. I have made a logger in Node-RED that waits for this error message and when it comes, it restarts the ESP device. It can run for many hours before the error happens. I will let it run for some days/weeks to see how frequent it is (I realize restarting upon error is not the cure but it keeps the device running for now) |
Thanks for the additional information. It seems that I have not set the BT stack trace levels correctly myself, as the At first glance btm_inq_rmt_name_failed seems to be called also when the requested device is just not present. But it's definitely something I should look into. 👍 |
Hello, thanks for looking into it. If it helps, I provide the current log result from my setup using Node-RED to restart the device when the error occurs (the restarting has worked fine every time, just to keep the device in operation). The time between errors is random. Sometimes it can work for more than a day, sometimes just some hours 2024-01-14T06:20:13.208Z Begin looking for errors |
Just a follow up. The time between errors is random. Sometimes it can work for more than a day, sometimes just some hours ...... |
I tried now also to build it for a WEMOS LOLIN C3 Mini. Such a small device would be nice to use if you plan to have multiple "checkpoints" around for various automation use cases In my .yaml I have
but as soon as I add esp32_bt_classic: I get the following errors below. Could it be something about the C3 does not support BT classic? ` |
Found the answer & reason here: |
What does this implement/fix?
BLE Presence is great, but cannot work with all devices. I've had a ported, slimmed down version of Andrewjfreyer's
monitor
running on a couple of ESP32's locally for over a year to take care of my BT (classic) presence sensing needs.This PR brings the functionality of my first attempt at porting the BT presence monitor (ESP32-MQTT-Bluetooth-Monitor) to ESPHome.
In a nutshell, BT Classic devices are detected by performing a device name query for a known MAC address. If the device responds, it is present. If it does not respond in the allowed number of attempts, it is assumed absent.
This PR is currently kept W.I.P. untill the following items are dealt with:
Scan Triggers
andScan Result Listners
Note:
Getting the ESP32 to setup using the correct BT Mode using
esp-idf
turned out to require some refactoring of other (ble) components. For this purpose I have extracted common definitions / functionality into a separate component:esp32_bt_common
, though I'm not sure if that would be the best solution. Open to suggestions! 😉Types of changes
Related issue or feature (if applicable): N/a
Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#2873
Test Environment
ESP8266incompatible
RP2040incompatible
Example entry for
config.yaml
:Checklist:
tests/
folder).If user exposed functionality or configuration variables are added/changed: