2018-06-13 22:38:49 +02:00
ESP32 Bluetooth Low Energy Tracker Hub
======================================
2018-05-14 21:15:49 +02:00
2018-11-14 22:12:27 +01:00
.. seo ::
2019-02-16 23:25:23 +01:00
:description: Instructions for setting up ESP32 bluetooth low energy device trackers using ESPHome.
2021-11-16 03:19:33 +01:00
:image: bluetooth.svg
2018-11-14 22:12:27 +01:00
2023-10-27 00:29:59 +02:00
The `` esp32_ble_tracker `` component creates a global hub so that you can track bluetooth low energy devices
using your ESP32 node.
2018-05-14 21:15:49 +02:00
2023-10-27 00:29:59 +02:00
See :ref: `Setting up devices <esp32_ble_tracker-setting_up_devices>` for information on how you can determine
the MAC address of a device and track it using ESPHome.
.. warning ::
The BLE software stack on the ESP32 consumes a significant amount of RAM on the device.
2024-07-15 01:40:55 +02:00
2023-10-27 00:29:59 +02:00
**Crashes are likely to occur** if you include too many additional components in your device's
configuration. Memory-intensive components such as :doc: `/components/voice_assistant` and other
audio components are most likely to cause issues.
2018-05-14 21:15:49 +02:00
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-05-14 21:15:49 +02:00
# Example configuration entry
2018-06-13 22:38:49 +02:00
esp32_ble_tracker:
2018-05-14 21:15:49 +02:00
binary_sensor:
2020-01-12 16:00:08 +01:00
- platform: ble_presence
2024-07-15 01:40:55 +02:00
mac_address: XX:XX:XX:XX:XX:XX
2020-01-12 16:00:08 +01:00
name: "ESP32 BLE Presence Google Home Mini"
2018-05-14 21:15:49 +02:00
2018-08-22 22:05:28 +02:00
sensor:
- platform: ble_rssi
2024-07-15 01:40:55 +02:00
mac_address: XX:XX:XX:XX:XX:XX
2018-08-22 22:05:28 +02:00
name: "BLE Google Home Mini RSSI value"
2020-05-27 01:40:01 +02:00
- platform: xiaomi_hhccjcy01
2024-07-15 01:40:55 +02:00
mac_address: XX:XX:XX:XX:XX:XX
2018-08-22 22:05:28 +02:00
temperature:
name: "Xiaomi MiFlora Temperature"
moisture:
name: "Xiaomi MiFlora Moisture"
illuminance:
name: "Xiaomi MiFlora Illuminance"
conductivity:
name: "Xiaomi MiFlora Soil Conductivity"
battery_level:
name: "Xiaomi MiFlora Battery Level"
2020-05-27 01:40:01 +02:00
- platform: xiaomi_lywsdcgq
2024-07-15 01:40:55 +02:00
mac_address: XX:XX:XX:XX:XX:XX
2018-08-22 22:05:28 +02:00
temperature:
name: "Xiaomi MiJia Temperature"
humidity:
name: "Xiaomi MiJia Humidity"
battery_level:
name: "Xiaomi MiJia Battery Level"
2018-10-04 18:13:58 +02:00
.. note ::
The first time this component is enabled for an ESP32, the code partition needs to be
2018-10-07 20:32:38 +02:00
resized. Please flash the ESP32 via USB when adding this to your configuration. After that,
you can use OTA updates again.
2018-10-04 18:13:58 +02:00
2020-05-20 02:13:39 +02:00
.. _config-esp32_ble_tracker:
2018-10-04 18:13:58 +02:00
2018-05-14 21:15:49 +02:00
Configuration variables:
2018-08-24 22:44:01 +02:00
------------------------
2018-05-14 21:15:49 +02:00
2019-10-19 16:33:02 +02:00
- **scan_parameters** (*Optional* ): Advanced parameters for configuring the scan behavior of the ESP32.
2021-09-14 16:42:08 +02:00
See also `this guide by Texas Instruments <https://dev.ti.com/tirex/explore/content/simplelink_academy_cc2640r2sdk_5_10_02_00/modules/blestack/ble_scan_adv_basic/ble_scan_adv_basic.html#scanning-basics> `__
2019-10-19 16:33:02 +02:00
for reference.
- **interval** (*Optional* , :ref: `config-time` ): The interval between each consecutive scan window.
This is the time the ESP spends on each of the 3 BLE advertising channels.
Defaults to `` 320ms `` .
- **window** (*Optional* , :ref: `config-time` ): The time the ESP is actively listening for packets
on a channel during each scan interval. If this is close to the `` interval `` value, the ESP will
2022-01-21 03:43:55 +01:00
spend more time listening to packets (but also consume more power). Defaults to `` 30ms ``
2019-10-19 16:33:02 +02:00
- **duration** (*Optional* , :ref: `config-time` ): The duration of each complete scan. This has no real
impact on the device but can be used to debug the BLE stack. Defaults to `` 5min `` .
- **active** (*Optional* , boolean): Whether to actively send scan requests to request more data
after having received an advertising packet. With some devices this is necessary to receive all data,
2022-09-07 09:25:16 +02:00
but also drains those devices' power a bit more. Some devices don't need this, in that case
you can save power and RF pollution by setting it to `` false `` . Defaults to `` true `` .
2022-09-14 01:10:15 +02:00
- **continuous** (*Optional* , boolean): Whether to scan continuously (forever) or to only scan when
asked to start a scan (with start_scan action). Defaults to `` true `` .
2019-10-19 16:33:02 +02:00
2018-06-06 09:22:35 +02:00
- **id** (*Optional* , :ref: `config-id` ): Manually specify the ID for this ESP32 BLE Hub.
2018-05-14 21:15:49 +02:00
2020-04-28 01:56:54 +02:00
Automations:
- **on_ble_advertise** (*Optional* , :ref: `Automation <automation>` ): An automation to perform
when a Bluetooth advertising is received. See :ref: `esp32_ble_tracker-on_ble_advertise` .
- **on_ble_manufacturer_data_advertise** (*Optional* , :ref: `Automation <automation>` ): An automation to
2021-09-14 16:42:08 +02:00
perform when a Bluetooth advertising with manufacturer data is received. See
2020-04-28 01:56:54 +02:00
:ref: `esp32_ble_tracker-on_ble_manufacturer_data_advertise` .
- **on_ble_service_data_advertise** (*Optional* , :ref: `Automation <automation>` ): An automation to
perform when a Bluetooth advertising with service data is received. See
:ref: `esp32_ble_tracker-on_ble_service_data_advertise` .
2022-09-14 01:10:15 +02:00
- **on_scan_end** (*Optional* , :ref: `Automation <automation>` ): An automation to perform when
a BLE scan has completed (the duration of the scan). This works with continuous set to true or false.
2022-09-14 04:57:48 +02:00
2020-04-28 01:56:54 +02:00
ESP32 Bluetooth Low Energy Tracker Automation
---------------------------------------------
.. _esp32_ble_tracker-on_ble_advertise:
2022-09-14 01:10:15 +02:00
`` on_ble_advertise `` Trigger
***** ***** ***** ***** ***** ***** ***** ***** ***** ***
2020-04-28 01:56:54 +02:00
This automation will be triggered when a Bluetooth advertising is received. A variable `` x `` of type
:apiclass: `esp32_ble_tracker::ESPBTDevice` is passed to the automation for use in lambdas.
.. code-block :: yaml
esp32_ble_tracker:
on_ble_advertise:
2024-07-15 01:40:55 +02:00
- mac_address:
- XX:XX:XX:XX:XX:XX
- XX:XX:XX:XX:XX:XX
2020-04-28 01:56:54 +02:00
then:
- lambda: |-
ESP_LOGD("ble_adv", "New BLE device");
ESP_LOGD("ble_adv", " address: %s", x.address_str().c_str());
ESP_LOGD("ble_adv", " name: %s", x.get_name().c_str());
ESP_LOGD("ble_adv", " Advertised service UUIDs:");
for (auto uuid : x.get_service_uuids()) {
ESP_LOGD("ble_adv", " - %s", uuid.to_string().c_str());
}
ESP_LOGD("ble_adv", " Advertised service data:");
for (auto data : x.get_service_datas()) {
ESP_LOGD("ble_adv", " - %s: (length %i)", data.uuid.to_string().c_str(), data.data.size());
}
ESP_LOGD("ble_adv", " Advertised manufacturer data:");
for (auto data : x.get_manufacturer_datas()) {
ESP_LOGD("ble_adv", " - %s: (length %i)", data.uuid.to_string().c_str(), data.data.size());
}
Configuration variables:
2023-06-09 02:53:34 +02:00
- **mac_address** (*Optional* , list of MAC Address): The MAC address to filter for this automation.
2020-04-28 01:56:54 +02:00
- See :ref: `Automation <automation>` .
.. _esp32_ble_tracker-on_ble_manufacturer_data_advertise:
2022-09-14 01:10:15 +02:00
`` on_ble_manufacturer_data_advertise `` Trigger
***** ***** ***** ***** ***** ***** ***** ***** ***** ***
2020-04-28 01:56:54 +02:00
2023-11-20 10:58:06 +01:00
This automation will be triggered when a Bluetooth advertising with manufacturer data is received. A
2020-04-28 01:56:54 +02:00
variable `` x `` of type `` std::vector<uint8_t> `` is passed to the automation for use in lambdas.
.. code-block :: yaml
sensor:
- platform: template
name: "BLE Sensor"
id: ble_sensor
esp32_ble_tracker:
on_ble_manufacturer_data_advertise:
2024-07-15 01:40:55 +02:00
- mac_address: XX:XX:XX:XX:XX:XX
2020-04-28 01:56:54 +02:00
manufacturer_id: 0590
then:
- lambda: |-
if (x[0] != 0x7b || x[1] != 0x61) return;
int value = x[2] + (x[3] << 8);
id(ble_sensor).publish_state(value);
Configuration variables:
- **mac_address** (*Optional* , MAC Address): The MAC address to filter for this automation.
2021-02-20 22:02:46 +01:00
- **manufacturer_id** (**Required** , string): 16 bit, 32 bit, or 128 bit BLE Manufacturer ID.
2020-04-28 01:56:54 +02:00
- See :ref: `Automation <automation>` .
.. _esp32_ble_tracker-on_ble_service_data_advertise:
2022-09-14 01:10:15 +02:00
`` on_ble_service_data_advertise `` Trigger
***** ***** ***** ***** ***** ***** ***** ***** ***** ***
2020-04-28 01:56:54 +02:00
This automation will be triggered when a Bluetooth advertising with service data is received. A
variable `` x `` of type `` std::vector<uint8_t> `` is passed to the automation for use in lambdas.
.. code-block :: yaml
sensor:
- platform: template
name: "BLE Sensor"
id: ble_sensor
esp32_ble_tracker:
on_ble_service_data_advertise:
2024-07-15 01:40:55 +02:00
- mac_address: XX:XX:XX:XX:XX:XX
2020-04-28 01:56:54 +02:00
service_uuid: 181A
then:
- lambda: 'id(ble_sensor).publish_state(x[0]);'
Configuration variables:
- **mac_address** (*Optional* , MAC Address): The MAC address to filter for this automation.
2021-02-20 22:02:46 +01:00
- **service_uuid** (**Required** , string): 16 bit, 32 bit, or 128 bit BLE Service UUID.
2020-04-28 01:56:54 +02:00
- See :ref: `Automation <automation>` .
2022-09-14 01:10:15 +02:00
`` on_scan_end `` Trigger
***** ***** ***** ***** ***** ***** ***** ***** ***** ***
This automation will be triggered when a Bluetooth scanning sequence has completed. If running
2022-09-14 04:57:48 +02:00
with continuous set to true, this will trigger every time the scan completes (the duration of
2022-09-14 01:10:15 +02:00
a scan).
.. code-block :: yaml
esp32_ble_tracker:
on_scan_end:
- then:
- lambda: |-
ESP_LOGD("ble_auto", "The scan has ended!");
Configuration variables:
- None
- See :ref: `Automation <automation>` .
`` esp32_ble_tracker.start_scan `` Action
***** ***** ***** ***** ***** ***** ***** ***** ***** ***
2022-09-14 04:57:48 +02:00
Start a Bluetooth scan. If there is a scan already in progress, then the action is ignored.
2022-09-14 01:10:15 +02:00
.. code-block :: yaml
esp32_ble_tracker:
scan_parameters:
continuous: false
2022-09-14 04:57:48 +02:00
on_...:
- esp32_ble_tracker.start_scan:
2022-09-14 01:10:15 +02:00
Configuration variables:
- **continuous** (*Optional* , boolean): Whether to start the scan in continuous mode. Defaults to `` false ``
.. note ::
This action can also be written in :ref: `lambdas <config-lambda>` :
.. code-block :: yaml
esp32_ble_tracker:
id: ble_tracker_id
.. code-block :: cpp
id(ble_tracker_id).start_scan()
2022-09-14 04:57:48 +02:00
`` esp32_ble_tracker.stop_scan `` Action
***** ***** ***** ***** ***** ***** ***** ***** ***** ***
Stops the bluetooth scanning. It can be started again with the above start scan action.
.. code-block :: yaml
esp32_ble_tracker:
on_...:
- esp32_ble_tracker.stop_scan:
2024-02-27 07:04:03 +01:00
Use on single-core chips
------------------------
On dual-core devices the WiFi component runs on core 1, while this component runs on core 0.
When using this component on single core chips such as the ESP32-C3 both WiFi and `` ble_tracker `` must run on
the same core, and this has been known to cause issues when connecting to WiFi. A work-around for this is to
enable the tracker only while the native API is connected. The following config will achieve this:
.. code-block :: yaml
esp32_ble_tracker:
scan_parameters:
continuous: false
api:
encryption:
key: !secret encryption_key
on_client_connected:
- esp32_ble_tracker.start_scan:
continuous: true
on_client_disconnected:
- esp32_ble_tracker.stop_scan:
2018-06-01 18:10:00 +02:00
See Also
2018-08-24 22:44:01 +02:00
--------
2018-05-14 21:15:49 +02:00
2022-09-07 09:25:16 +02:00
- :doc: `text_sensor/ble_scanner`
- :doc: `sensor/ble_rssi`
- :doc: `sensor/b_parasite`
2022-09-07 09:29:58 +02:00
- :doc: `sensor/xiaomi_ble`
2022-09-07 09:25:16 +02:00
- :doc: `sensor/xiaomi_miscale`
- :doc: `sensor/inkbird_ibsth1_mini`
- :doc: `sensor/mopeka_pro_check`
- :doc: `sensor/ruuvitag`
- :doc: `ble_client`
- :doc: `bluetooth_proxy`
2019-05-12 22:44:59 +02:00
- :apiref: `esp32_ble_tracker/esp32_ble_tracker.h`
2018-08-24 22:44:01 +02:00
- `ESP32 BLE for Arduino <https://github.com/nkolban/ESP32_BLE_Arduino> `__ by `Neil Kolban <https://github.com/nkolban> `__ .
2019-02-07 13:54:45 +01:00
- :ghedit: `Edit`