For some weeks I’m playing around with BLE and got it working sort of. The only thing I dislike is that it needs to connect to the ESPHome BLE server in order to get updates. In the process added some PRs to fix/improve the ESPHome documentation.
The disadvantage is that mostly only one BLE client can be connected. At least that is my experience. Might be because
I would like to have ESPHome advertise the values in the advertisement packets like Xiaomi temperature sensors do. I thought if I did ble_server.characteristic.notify
it would automatically advertise this, but it looks like that is not the case.
Can it be done? Does it need custom lambda code?
This is my current code.
Server (sends a random int every second):
esp32_ble_server:
id: ble_server
services:
- uuid: 0f502a49-9c9b-4788-b86e-5248e50d14b3
advertise: true
characteristics:
- uuid: cf7e7a4b-4f74-4cbe-9d41-bfa40f846c5a
id: ble_random_test_int
description: "BLE random test int"
broadcast: true
read: true
notify: true
value:
type: int32_t
data: 1
interval:
- interval: 1sec
then:
- ble_server.characteristic.set_value:
id: ble_test
value: !lambda
float my_float_value = rand() % 2000;
std::string my_string = str_sprintf("%.0f", my_float_value);
std::vector<unsigned char> byte_vector(my_string.begin(), my_string.end());
return byte_vector;
- ble_server.characteristic.notify:
id: ble_test
Client (logs advertisements):
esp32_ble_tracker:
scan_parameters:
continuous: false
on_ble_advertise:
- mac_address: XX:XX:XX:XX:XX:XX
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", " - %s:", format_hex_pretty(data.data).c_str());
}
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());
ESP_LOGD("ble_adv", " - %s:", format_hex_pretty(data.data).c_str());
}
on_ble_service_data_advertise:
- mac_address: XX:XX:XX:XX:XX:XX
service_uuid: 0f502a49-9c9b-4788-b86e-5248e50d14b3
then:
- lambda: |-
ESP_LOGD("ble_service_data_advertise", " - %s", x);
The only thing I’m getting is:
[17:08:14][D][ble_adv:068]: New BLE device
[17:08:14][D][ble_adv:069]: address: XX:XX:XX:XX:XX:XX
[17:08:14][D][ble_adv:070]: name: esp32-ble-server
[17:08:14][D][ble_adv:072]: Advertised service UUIDs:
[17:08:14][D][ble_adv:074]: - 0F502A49-9C9B-4788-B86E-5248E50D14B3
[17:08:14][D][ble_adv:077]: Advertised service data:
[17:08:14][D][ble_adv:083]: Advertised manufacturer data:
Without any Advertised service data
… Also tried with using capitals for service_uuid: 0f502a49-9c9b-4788-b86e-5248e50d14b3