ESPHome 2025.6.3
Loading...
Searching...
No Matches
ble_sensor.cpp
Go to the documentation of this file.
1#include "ble_sensor.h"
4#include "esphome/core/log.h"
6
7#ifdef USE_ESP32
8
9namespace esphome {
10namespace ble_client {
11
12static const char *const TAG = "ble_sensor";
13
15
17 LOG_SENSOR("", "BLE Sensor", this);
18 ESP_LOGCONFIG(TAG,
19 " MAC address : %s\n"
20 " Service UUID : %s\n"
21 " Characteristic UUID: %s\n"
22 " Descriptor UUID : %s\n"
23 " Notifications : %s",
24 this->parent()->address_str().c_str(), this->service_uuid_.to_string().c_str(),
25 this->char_uuid_.to_string().c_str(), this->descr_uuid_.to_string().c_str(), YESNO(this->notify_));
26 LOG_UPDATE_INTERVAL(this);
27}
28
29void BLESensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
30 esp_ble_gattc_cb_param_t *param) {
31 switch (event) {
32 case ESP_GATTC_OPEN_EVT: {
33 if (param->open.status == ESP_GATT_OK) {
34 ESP_LOGI(TAG, "[%s] Connected successfully!", this->get_name().c_str());
35 break;
36 }
37 break;
38 }
39 case ESP_GATTC_CLOSE_EVT: {
40 ESP_LOGW(TAG, "[%s] Disconnected!", this->get_name().c_str());
41 this->status_set_warning();
42 this->publish_state(NAN);
43 break;
44 }
45 case ESP_GATTC_SEARCH_CMPL_EVT: {
46 this->handle = 0;
47 auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
48 if (chr == nullptr) {
49 this->status_set_warning();
50 this->publish_state(NAN);
51 ESP_LOGW(TAG, "No sensor characteristic found at service %s char %s", this->service_uuid_.to_string().c_str(),
52 this->char_uuid_.to_string().c_str());
53 break;
54 }
55 this->handle = chr->handle;
56 if (this->descr_uuid_.get_uuid().len > 0) {
57 auto *descr = chr->get_descriptor(this->descr_uuid_);
58 if (descr == nullptr) {
59 this->status_set_warning();
60 this->publish_state(NAN);
61 ESP_LOGW(TAG, "No sensor descriptor found at service %s char %s descr %s",
62 this->service_uuid_.to_string().c_str(), this->char_uuid_.to_string().c_str(),
63 this->descr_uuid_.to_string().c_str());
64 break;
65 }
66 this->handle = descr->handle;
67 }
68 if (this->notify_) {
69 auto status = esp_ble_gattc_register_for_notify(this->parent()->get_gattc_if(),
70 this->parent()->get_remote_bda(), chr->handle);
71 if (status) {
72 ESP_LOGW(TAG, "esp_ble_gattc_register_for_notify failed, status=%d", status);
73 }
74 } else {
75 this->node_state = espbt::ClientState::ESTABLISHED;
76 }
77 break;
78 }
79 case ESP_GATTC_READ_CHAR_EVT: {
80 if (param->read.status != ESP_GATT_OK) {
81 ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
82 break;
83 }
84 if (param->read.handle == this->handle) {
86 this->publish_state(this->parse_data_(param->read.value, param->read.value_len));
87 }
88 break;
89 }
90 case ESP_GATTC_NOTIFY_EVT: {
91 ESP_LOGD(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(),
92 param->notify.handle, param->notify.value[0]);
93 if (param->notify.handle != this->handle)
94 break;
95 this->publish_state(this->parse_data_(param->notify.value, param->notify.value_len));
96 break;
97 }
98 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
99 if (param->reg_for_notify.handle == this->handle) {
100 if (param->reg_for_notify.status != ESP_GATT_OK) {
101 ESP_LOGW(TAG, "Error registering for notifications at handle %d, status=%d", param->reg_for_notify.handle,
102 param->reg_for_notify.status);
103 break;
104 }
105 this->node_state = espbt::ClientState::ESTABLISHED;
106 ESP_LOGD(TAG, "Register for notify on %s complete", this->char_uuid_.to_string().c_str());
107 }
108 break;
109 }
110 default:
111 break;
112 }
113}
114
115float BLESensor::parse_data_(uint8_t *value, uint16_t value_len) {
116 if (this->data_to_value_func_.has_value()) {
117 std::vector<uint8_t> data(value, value + value_len);
118 return (*this->data_to_value_func_)(data);
119 } else {
120 return value[0];
121 }
122}
123
125 if (this->node_state != espbt::ClientState::ESTABLISHED) {
126 ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->get_name().c_str());
127 return;
128 }
129 if (this->handle == 0) {
130 ESP_LOGW(TAG, "[%s] Cannot poll, no service or characteristic found", this->get_name().c_str());
131 return;
132 }
133
134 auto status = esp_ble_gattc_read_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->handle,
135 ESP_GATT_AUTH_REQ_NONE);
136 if (status) {
137 this->status_set_warning();
138 this->publish_state(NAN);
139 ESP_LOGW(TAG, "[%s] Error sending read request for sensor, status=%d", this->get_name().c_str(), status);
140 }
141}
142
143} // namespace ble_client
144} // namespace esphome
145#endif
uint8_t status
Definition bl0942.h:8
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
const StringRef & get_name() const
espbt::ESPBTUUID service_uuid_
Definition ble_sensor.h:45
optional< data_to_value_t > data_to_value_func_
Definition ble_sensor.h:43
float parse_data_(uint8_t *value, uint16_t value_len)
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
espbt::ESPBTUUID descr_uuid_
Definition ble_sensor.h:47
espbt::ESPBTUUID char_uuid_
Definition ble_sensor.h:46
std::string to_string() const
Definition ble_uuid.cpp:171
esp_bt_uuid_t get_uuid() const
Definition ble_uuid.cpp:170
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
bool has_value() const
Definition optional.h:87
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7