Thanks for this. I’ve been playing with it on an Olimex esp32-gateway with ESPHome version 2023.7.0.
I started by adding id(ble_switch).turn_off()
to the end of the ble_sensor
lambda which parses the values. So it turns on every half hour, then turns off immediately after getting a reading… or after two minutes if not. Since this is the only BLE device used with this esphome, I also start/stop the scan when the ble_switch turns on/off:
- platform: ble_client
id: ble_switch
ble_client_id: ble_yc01
name: "Enable BLE-YC01"
restore_mode: ALWAYS_ON
on_turn_on:
- esp32_ble_tracker.start_scan:
continuous: true
on_turn_off:
- esp32_ble_tracker.stop_scan:
I tried triggering the update immediately from the ble_client
on_connect
lambda right after the ->get_characteristic()
call; wouldn’t it be nice if we could read the values once there, and not have the periodic ‘Cannot poll, not connected’ warnings?
#### Official connection to the BLE device with the desired characteristic ####
- lambda: |-
ESP_LOGD("ble_client_lambda", "Connected to BLE-YC01");
id(ble_yc01_ble_connected).publish_state(true);
id(led_output).turn_on();
esphome::ble_client::BLEClient* client = id(ble_yc01);
auto service_uuid = 0xFF01;
auto char_uuid = 0xFF02;
esphome::ble_client::BLECharacteristic* chr = client->get_characteristic(service_uuid, char_uuid);
if (chr == nullptr) {
ESP_LOGW("ble_client", "[0xFF01] Characteristic not found. State update can not be written.");
}
esphome::ble_client::BLESensor *s = id(ble_yc01_sensor);
s->update();
It doesn’t work though… the sensor isn’t in state espbt::ClientState::ESTABLISHED
yet…
[16:20:15][D][ble_adv:144]: New BLE device
[16:20:15][D][ble_adv:145]: address: C0:00:00:01:67:D7
[16:20:15][D][ble_adv:146]: name: BLE-YC01
[16:20:15][D][ble_adv:147]: Advertised service UUIDs:
[16:20:15][D][ble_adv:149]: - 0xFF01
[16:20:15][D][ble_adv:151]: Advertised service data:
[16:20:15][D][ble_adv:155]: Advertised manufacturer data:
[16:20:15][D][esp32_ble_client:048]: [0] [C0:00:00:01:67:D7] Found device
[16:20:15][D][esp32_ble_tracker:213]: Pausing scan to make connection...
[16:20:15][W][component:204]: Component esp32_ble_tracker took a long time for an operation (0.13 s).
[16:20:15][W][component:205]: Components should block for at most 20-30ms.
[16:20:15][V][esp32_ble:178]: (BLE) gap_event_handler - 18
[16:20:15][I][esp32_ble_client:064]: [0] [C0:00:00:01:67:D7] 0x00 Attempting BLE connection
[16:20:18][VV][scheduler:226]: Running interval 'update' with interval=60000 last_execution=4294944760 (now=37465)
[16:20:19][V][esp32_ble:206]: (BLE) gattc_event [esp_gatt_if: 4] - 40
[16:20:19][V][esp32_ble_client:114]: [0] [C0:00:00:01:67:D7] gattc_event_handler: event=40 gattc_if=4
[16:20:19][V][esp32_ble:206]: (BLE) gattc_event [esp_gatt_if: 4] - 2
[16:20:19][V][esp32_ble_client:114]: [0] [C0:00:00:01:67:D7] gattc_event_handler: event=2 gattc_if=4
[16:20:19][V][esp32_ble_client:129]: [0] [C0:00:00:01:67:D7] ESP_GATTC_OPEN_EVT
[16:20:19][I][ble_rssi_sensor:027]: [BLE-YC01 RSSI] Connected successfully!
[16:20:19][I][ble_sensor:031]: [ble_yc01_sensor] Connected successfully!
[16:20:20][W][ble_sensor:117]: [ble_yc01_sensor] Cannot poll, not connected
[16:20:20][V][esp32_ble:206]: (BLE) gattc_event [esp_gatt_if: 4] - 46
[16:20:20][V][esp32_ble_client:114]: [0] [C0:00:00:01:67:D7] gattc_event_handler: event=46 gattc_if=4
[16:20:20][V][esp32_ble:206]: (BLE) gattc_event [esp_gatt_if: 4] - 7
[16:20:20][V][esp32_ble_client:114]: [0] [C0:00:00:01:67:D7] gattc_event_handler: event=7 gattc_if=4
[16:20:20][V][esp32_ble:206]: (BLE) gattc_event [esp_gatt_if: 4] - 7
[16:20:20][V][esp32_ble_client:114]: [0] [C0:00:00:01:67:D7] gattc_event_handler: event=7 gattc_if=4
[16:20:20][V][esp32_ble:206]: (BLE) gattc_event [esp_gatt_if: 4] - 7
[16:20:20][V][esp32_ble_client:114]: [0] [C0:00:00:01:67:D7] gattc_event_handler: event=7 gattc_if=4
[16:20:20][V][esp32_ble:206]: (BLE) gattc_event [esp_gatt_if: 4] - 6
[16:20:20][V][esp32_ble_client:114]: [0] [C0:00:00:01:67:D7] gattc_event_handler: event=6 gattc_if=4
[16:20:20][V][esp32_ble_client:189]: [0] [C0:00:00:01:67:D7] ESP_GATTC_SEARCH_CMPL_EVT
[16:20:20][V][esp32_ble_client:192]: [0] [C0:00:00:01:67:D7] Service UUID: 0x1800
[16:20:20][V][esp32_ble_client:194]: [0] [C0:00:00:01:67:D7] start_handle: 0x1 end_handle: 0x7
[16:20:20][V][esp32_ble_client:192]: [0] [C0:00:00:01:67:D7] Service UUID: 0x1801
[16:20:20][V][esp32_ble_client:194]: [0] [C0:00:00:01:67:D7] start_handle: 0x8 end_handle: 0xb
[16:20:20][V][esp32_ble_client:192]: [0] [C0:00:00:01:67:D7] Service UUID: 0xFF01
[16:20:20][V][esp32_ble_client:194]: [0] [C0:00:00:01:67:D7] start_handle: 0xc end_handle: 0xffff
[16:20:20][I][esp32_ble_client:196]: [0] [C0:00:00:01:67:D7] Connected
[16:20:20][W][ble_client:208]: on_connect lambda
[16:20:20][V][esp32_ble_client:069]: [0] [C0:00:00:01:67:D7] characteristic 0xFF02, handle 0xe, properties 0x1a
[16:20:20][V][esp32_ble_client:069]: [0] [C0:00:00:01:67:D7] characteristic 0xFF10, handle 0x12, properties 0x2
[16:20:20][W][ble_sensor:117]: [ble_yc01_sensor] Cannot poll, not connected
[16:20:20][W][ble_client:215]: [0xFF01] Characteristic found!
[16:20:20][D][ble_client_lambda:223]: Connected to BLE-YC01
[16:20:20][D][binary_sensor:036]: 'BLE Connected': Sending state ON
[16:20:20][V][mqtt:486]: Publish(topic='pool/binary_sensor/ble_connected/state' payload='ON' retain=1)
[16:20:20][W][ble_sensor:117]: [ble_yc01_sensor] Cannot poll, not connected
[16:20:20][V][esp32_ble:206]: (BLE) gattc_event [esp_gatt_if: 4] - 18
[16:20:20][V][esp32_ble_client:114]: [0] [C0:00:00:01:67:D7] gattc_event_handler: event=18 gattc_if=4
[16:20:20][V][esp32_ble_client:160]: [0] [C0:00:00:01:67:D7] cfg_mtu status 0, mtu 32
[16:20:20][W][component:204]: Component esp32_ble took a long time for an operation (0.21 s).
[16:20:20][W][component:205]: Components should block for at most 20-30ms.
[16:20:20][D][esp32_ble_tracker:245]: Starting scan...
... scan results...
[16:20:30][VV][scheduler:226]: Running interval 'update' with interval=10000 last_execution=39662 (now=49663)
[16:20:30][V][esp32_ble:206]: (BLE) gattc_event [esp_gatt_if: 4] - 3
[16:20:30][V][esp32_ble_client:114]: [0] [C0:00:00:01:67:D7] gattc_event_handler: event=3 gattc_if=4
[16:20:30][D][ble_client.receive:399]: value received with 29 bytes: [\xff\xa1\xfc\xf2\xfd-\xfc3\xfd-U\xaa"\xfdxfa\xa9\xfc)\xff\xbe\xff\xff\xff\xff_T]
[16:20:30][V][sensor:043]: 'BLE-YC01 Temperature': Received new state 20.400000
[16:20:30][D][sensor:094]: 'BLE-YC01 Temperature': Sending state 20.40000 °C with 2 decimals of accuracy
Can we make it call the sensor’s update()
method as soon as it becomes ready? What event is it waiting for? Those calls to ->get_characteristic()
don’t seem to be making any difference; if I take them out completely, it still connects to the device, and the next periodic update of the sensor works.