ESPHome  2022.12.8
airthings_wave_mini.cpp
Go to the documentation of this file.
1 #include "airthings_wave_mini.h"
2 
3 #ifdef USE_ESP32
4 
5 namespace esphome {
6 namespace airthings_wave_mini {
7 
8 static const char *const TAG = "airthings_wave_mini";
9 
10 void AirthingsWaveMini::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 AirthingsWaveMini::read_sensors_(uint8_t *raw_value, uint16_t value_len) {
59  auto *value = (WaveMiniReadings *) raw_value;
60 
61  if (sizeof(WaveMiniReadings) <= value_len) {
62  this->humidity_sensor_->publish_state(value->humidity / 100.0f);
63  this->pressure_sensor_->publish_state(value->pressure / 50.0f);
64  this->temperature_sensor_->publish_state(value->temperature / 100.0f - 273.15f);
65  if (is_valid_voc_value_(value->voc)) {
66  this->tvoc_sensor_->publish_state(value->voc);
67  }
68 
69  // This instance must not stay connected
70  // so other clients can connect to it (e.g. the
71  // mobile app).
72  parent()->set_enabled(false);
73  }
74 }
75 
76 bool AirthingsWaveMini::is_valid_voc_value_(uint16_t voc) { return 0 <= voc && voc <= 16383; }
77 
80  if (!parent()->enabled) {
81  ESP_LOGW(TAG, "Reconnecting to device");
82  parent()->set_enabled(true);
83  parent()->connect();
84  } else {
85  ESP_LOGW(TAG, "Connection in progress");
86  }
87  }
88 }
89 
91  auto status = esp_ble_gattc_read_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->handle_,
92  ESP_GATT_AUTH_REQ_NONE);
93  if (status) {
94  ESP_LOGW(TAG, "Error sending read request for sensor, status=%d", status);
95  }
96 }
97 
99  LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
100  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
101  LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
102  LOG_SENSOR(" ", "TVOC", this->tvoc_sensor_);
103 }
104 
106  : PollingComponent(10000),
107  service_uuid_(esp32_ble_tracker::ESPBTUUID::from_raw(SERVICE_UUID)),
108  sensors_data_characteristic_uuid_(esp32_ble_tracker::ESPBTUUID::from_raw(CHARACTERISTIC_UUID)) {}
109 
110 } // namespace airthings_wave_mini
111 } // namespace esphome
112 
113 #endif // USE_ESP32
void read_sensors_(uint8_t *value, uint16_t value_len)
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
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Definition: a4988.cpp:4
espbt::ClientState node_state
Definition: ble_client.h:38