ESPHome  2022.12.8
ble_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include <vector>
9 
10 #ifdef USE_ESP32
11 #include <esp_gattc_api.h>
12 
13 namespace esphome {
14 namespace ble_client {
15 
17 
18 using data_to_value_t = std::function<float(std::vector<uint8_t>)>;
19 
20 class BLESensor : public sensor::Sensor, public PollingComponent, public BLEClientNode {
21  public:
22  void loop() override;
23  void update() override;
24  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
25  esp_ble_gattc_cb_param_t *param) override;
26  void dump_config() override;
27  float get_setup_priority() const override { return setup_priority::DATA; }
28  void set_service_uuid16(uint16_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
29  void set_service_uuid32(uint32_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
30  void set_service_uuid128(uint8_t *uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
31  void set_char_uuid16(uint16_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
32  void set_char_uuid32(uint32_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
33  void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
34  void set_descr_uuid16(uint16_t uuid) { this->descr_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
35  void set_descr_uuid32(uint32_t uuid) { this->descr_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
36  void set_descr_uuid128(uint8_t *uuid) { this->descr_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
37  void set_data_to_value(data_to_value_t &&lambda) { this->data_to_value_func_ = lambda; }
38  void set_enable_notify(bool notify) { this->notify_ = notify; }
39  uint16_t handle;
40 
41  protected:
42  float parse_data_(uint8_t *value, uint16_t value_len);
44  bool notify_;
48 };
49 
50 } // namespace ble_client
51 } // namespace esphome
52 #endif
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
float get_setup_priority() const override
Definition: ble_sensor.h:27
espbt::ESPBTUUID descr_uuid_
Definition: ble_sensor.h:47
std::function< float(std::vector< uint8_t >)> data_to_value_t
Definition: ble_sensor.h:18
void set_char_uuid32(uint32_t uuid)
Definition: ble_sensor.h:32
void set_char_uuid128(uint8_t *uuid)
Definition: ble_sensor.h:33
void set_enable_notify(bool notify)
Definition: ble_sensor.h:38
This class simplifies creating components that periodically check a state.
Definition: component.h:267
optional< data_to_value_t > data_to_value_func_
Definition: ble_sensor.h:43
float parse_data_(uint8_t *value, uint16_t value_len)
Definition: ble_sensor.cpp:106
static ESPBTUUID from_raw(const uint8_t *data)
void set_service_uuid128(uint8_t *uuid)
Definition: ble_sensor.h:30
void set_service_uuid16(uint16_t uuid)
Definition: ble_sensor.h:28
espbt::ESPBTUUID char_uuid_
Definition: ble_sensor.h:46
void set_descr_uuid32(uint32_t uuid)
Definition: ble_sensor.h:35
void set_char_uuid16(uint16_t uuid)
Definition: ble_sensor.h:31
void set_descr_uuid16(uint16_t uuid)
Definition: ble_sensor.h:34
espbt::ESPBTUUID service_uuid_
Definition: ble_sensor.h:45
void set_data_to_value(data_to_value_t &&lambda)
Definition: ble_sensor.h:37
static ESPBTUUID from_uint16(uint16_t uuid)
static ESPBTUUID from_uint32(uint32_t uuid)
void set_service_uuid32(uint32_t uuid)
Definition: ble_sensor.h:29
void set_descr_uuid128(uint8_t *uuid)
Definition: ble_sensor.h:36
Definition: a4988.cpp:4
Base-class for all sensors.
Definition: sensor.h:50
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Definition: ble_sensor.cpp:26