ESPHome  2024.7.0
airthings_wave_base.h
Go to the documentation of this file.
1 #pragma once
2 
3 // All information related to reading battery levels came from the sensors.airthings_wave
4 // project by Sverre Hamre (https://github.com/sverrham/sensor.airthings_wave)
5 
6 #ifdef USE_ESP32
7 
8 #include <esp_gattc_api.h>
9 #include <algorithm>
10 #include <iterator>
14 #include "esphome/core/component.h"
15 #include "esphome/core/log.h"
16 
17 namespace esphome {
18 namespace airthings_wave_base {
19 
21 
22 static const uint8_t ACCESS_CONTROL_POINT_COMMAND = 0x6d;
23 static const auto CLIENT_CHARACTERISTIC_CONFIGURATION_DESCRIPTOR_UUID = espbt::ESPBTUUID::from_uint16(0x2902);
24 
26  public:
27  AirthingsWaveBase() = default;
28 
29  void update() override;
30 
31  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
32  esp_ble_gattc_cb_param_t *param) override;
33 
35  void set_humidity(sensor::Sensor *humidity) { humidity_sensor_ = humidity; }
37  void set_tvoc(sensor::Sensor *tvoc) { tvoc_sensor_ = tvoc; }
39  battery_voltage_ = voltage;
40  this->read_battery_next_update_ = true;
41  }
42  void set_battery_update_interval(uint32_t interval) { battery_update_interval_ = interval; }
43 
44  protected:
45  bool is_valid_voc_value_(uint16_t voc);
46 
47  bool request_read_values_();
48  virtual void read_sensors(uint8_t *raw_value, uint16_t value_len) = 0;
49 
55 
56  uint16_t handle_;
59 
60  uint16_t acp_handle_{0};
61  uint16_t cccd_handle_{0};
63 
64  uint8_t responses_pending_{0};
65  void response_pending_();
66  void response_received_();
67  void set_response_timeout_();
68 
69  // default to *not* reading battery voltage from the device; the
70  // set_* function for the battery sensor will set this to 'true'
72  bool request_battery_();
73  void read_battery_(uint8_t *raw_value, uint16_t value_len);
75 
77  uint32_t unused1;
78  uint8_t unused2;
79  uint8_t illuminance;
80  uint8_t unused3[10];
81  uint16_t unused4[4];
82  uint16_t battery;
83  uint16_t unused5;
84  };
85 };
86 
87 } // namespace airthings_wave_base
88 } // namespace esphome
89 
90 #endif // USE_ESP32
virtual void read_sensors(uint8_t *raw_value, uint16_t value_len)=0
uint8_t pressure
Definition: tt21100.cpp:19
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void read_battery_(uint8_t *raw_value, uint16_t value_len)
void set_temperature(sensor::Sensor *temperature)
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
uint16_t temperature
Definition: sun_gtil2.cpp:26
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57