Closed DavesCodeMusings closed 2 weeks ago
Similar experiments with other GATT characteristics, like Temperature, had the same "unknown" values.
The solution most BLE beacons use is to send broadcast data in the advertising header. There is a "manufacturer data" field that can be used for this purpose. There's no standard format. The following code communicates a battery level in the advertisement.
// Manufacturer Data field must include the company ID as the first two octets. Data follows.
// It needs to be in little endian format, so 0x09A3, registered to Arduino SA is 0xA3, 0x09.
// Company ID source: https://www.bluetooth.com/specifications/assigned-numbers/
// Below is the Arduino company ID followed by "BATT:100%" in ASCII.
byte data[11] = { 0xA3, 0x09, 0x42, 0x41, 0x54, 0x54, 0x3A, 0x31, 0x30, 0x30, 0x25};
BLE.setManufacturerData(data, 11);
Using a BLE scanner, like Nordic Semiconductor's nRF, will reveal the Manufacturer Data in the advertisement. Changing the format to Text(UTF-8) shows the "BATT:100%" message.
ESPHome can access it via logging information. https://community.home-assistant.io/t/esp32-ble-tracker-how-to-log-look-at-bluetooth-advertising-data/693925/2
Removed ble_client
and sensor
from ESPHome yaml definition. New version is in: devkitv1_2.yml
Because the device goes into deep sleep periodically, and sometimes in the middle of polling, ESPHome returns NaN (not a number) for the battery level and Home Assistant shows it as Unknown.