ESPHome  2022.12.8
airthings_wave_plus.cpp
Go to the documentation of this file.
1 #include "airthings_wave_plus.h"
2 
3 #ifdef USE_ESP32
4 
5 namespace esphome {
6 namespace airthings_wave_plus {
7 
8 static const char *const TAG = "airthings_wave_plus";
9 
10 void AirthingsWavePlus::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
11  esp_ble_gattc_cb_param_t *param) {
12  switch (event) {
13  case ESP_GATTC_OPEN_EVT: {
14  if (param->open.status == ESP_GATT_OK) {
15  ESP_LOGI(TAG, "Connected successfully!");
16  }
17  break;
18  }
19 
20  case ESP_GATTC_DISCONNECT_EVT: {
21  ESP_LOGW(TAG, "Disconnected!");
22  break;
23  }
24 
25  case ESP_GATTC_SEARCH_CMPL_EVT: {
26  this->handle_ = 0;
28  if (chr == nullptr) {
29  ESP_LOGW(TAG, "No sensor characteristic found at service %s char %s", service_uuid_.to_string().c_str(),
31  break;
32  }
33  this->handle_ = chr->handle;
35 
37  break;
38  }
39 
40  case ESP_GATTC_READ_CHAR_EVT: {
41  if (param->read.conn_id != this->parent()->get_conn_id())
42  break;
43  if (param->read.status != ESP_GATT_OK) {
44  ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
45  break;
46  }
47  if (param->read.handle == this->handle_) {
48  read_sensors_(param->read.value, param->read.value_len);
49  }
50  break;
51  }
52 
53  default:
54  break;
55  }
56 }
57 
58 void AirthingsWavePlus::read_sensors_(uint8_t *raw_value, uint16_t value_len) {
59  auto *value = (WavePlusReadings *) raw_value;
60 
61  if (sizeof(WavePlusReadings) <= value_len) {
62  ESP_LOGD(TAG, "version = %d", value->version);
63 
64  if (value->version == 1) {
65  ESP_LOGD(TAG, "ambient light = %d", value->ambientLight);
66 
67  this->humidity_sensor_->publish_state(value->humidity / 2.0f);
68  if (is_valid_radon_value_(value->radon)) {
69  this->radon_sensor_->publish_state(value->radon);
70  }
71  if (is_valid_radon_value_(value->radon_lt)) {
72  this->radon_long_term_sensor_->publish_state(value->radon_lt);
73  }
74  this->temperature_sensor_->publish_state(value->temperature / 100.0f);
75  this->pressure_sensor_->publish_state(value->pressure / 50.0f);
76  if (is_valid_co2_value_(value->co2)) {
77  this->co2_sensor_->publish_state(value->co2);
78  }
79  if (is_valid_voc_value_(value->voc)) {
80  this->tvoc_sensor_->publish_state(value->voc);
81  }
82 
83  // This instance must not stay connected
84  // so other clients can connect to it (e.g. the
85  // mobile app).
86  parent()->set_enabled(false);
87  } else {
88  ESP_LOGE(TAG, "Invalid payload version (%d != 1, newer version or not a Wave Plus?)", value->version);
89  }
90  }
91 }
92 
93 bool AirthingsWavePlus::is_valid_radon_value_(uint16_t radon) { return 0 <= radon && radon <= 16383; }
94 
95 bool AirthingsWavePlus::is_valid_voc_value_(uint16_t voc) { return 0 <= voc && voc <= 16383; }
96 
97 bool AirthingsWavePlus::is_valid_co2_value_(uint16_t co2) { return 0 <= co2 && co2 <= 16383; }
98 
101  if (!parent()->enabled) {
102  ESP_LOGW(TAG, "Reconnecting to device");
103  parent()->set_enabled(true);
104  parent()->connect();
105  } else {
106  ESP_LOGW(TAG, "Connection in progress");
107  }
108  }
109 }
110 
112  auto status = esp_ble_gattc_read_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->handle_,
113  ESP_GATT_AUTH_REQ_NONE);
114  if (status) {
115  ESP_LOGW(TAG, "Error sending read request for sensor, status=%d", status);
116  }
117 }
118 
120  LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
121  LOG_SENSOR(" ", "Radon", this->radon_sensor_);
122  LOG_SENSOR(" ", "Radon Long Term", this->radon_long_term_sensor_);
123  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
124  LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
125  LOG_SENSOR(" ", "CO2", this->co2_sensor_);
126  LOG_SENSOR(" ", "TVOC", this->tvoc_sensor_);
127 }
128 
130  : PollingComponent(10000),
131  service_uuid_(esp32_ble_tracker::ESPBTUUID::from_raw(SERVICE_UUID)),
132  sensors_data_characteristic_uuid_(esp32_ble_tracker::ESPBTUUID::from_raw(CHARACTERISTIC_UUID)) {}
133 
134 } // namespace airthings_wave_plus
135 } // namespace esphome
136 
137 #endif // USE_ESP32
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
esp32_ble_tracker::ESPBTUUID sensors_data_characteristic_uuid_
This class simplifies creating components that periodically check a state.
Definition: component.h:267
void set_enabled(bool enabled)
Definition: ble_client.cpp:37
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:72
uint8_t status
Definition: bl0942.h:23
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Definition: a4988.cpp:4
void read_sensors_(uint8_t *value, uint16_t value_len)
espbt::ClientState node_state
Definition: ble_client.h:38