ESPHome  2024.10.2
pvvx_mithermometer.cpp
Go to the documentation of this file.
1 #include "pvvx_mithermometer.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 
6 namespace esphome {
7 namespace pvvx_mithermometer {
8 
9 static const char *const TAG = "pvvx_mithermometer";
10 
12  ESP_LOGCONFIG(TAG, "PVVX MiThermometer");
13  LOG_SENSOR(" ", "Temperature", this->temperature_);
14  LOG_SENSOR(" ", "Humidity", this->humidity_);
15  LOG_SENSOR(" ", "Battery Level", this->battery_level_);
16  LOG_SENSOR(" ", "Battery Voltage", this->battery_voltage_);
17 }
18 
20  if (device.address_uint64() != this->address_) {
21  ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
22  return false;
23  }
24  ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
25 
26  bool success = false;
27  for (auto &service_data : device.get_service_datas()) {
28  auto res = parse_header_(service_data);
29  if (!res.has_value()) {
30  continue;
31  }
32  if (!(parse_message_(service_data.data, *res))) {
33  continue;
34  }
35  if (!(report_results_(res, device.address_str()))) {
36  continue;
37  }
38  if (res->temperature.has_value() && this->temperature_ != nullptr)
39  this->temperature_->publish_state(*res->temperature);
40  if (res->humidity.has_value() && this->humidity_ != nullptr)
41  this->humidity_->publish_state(*res->humidity);
42  if (res->battery_level.has_value() && this->battery_level_ != nullptr)
43  this->battery_level_->publish_state(*res->battery_level);
44  if (res->battery_voltage.has_value() && this->battery_voltage_ != nullptr)
45  this->battery_voltage_->publish_state(*res->battery_voltage);
46  if (this->signal_strength_ != nullptr)
47  this->signal_strength_->publish_state(device.get_rssi());
48  success = true;
49  }
50 
51  return success;
52 }
53 
55  ParseResult result;
56  if (!service_data.uuid.contains(0x1A, 0x18)) {
57  ESP_LOGVV(TAG, "parse_header(): no service data UUID magic bytes.");
58  return {};
59  }
60 
61  auto raw = service_data.data;
62 
63  static uint8_t last_frame_count = 0;
64  if (last_frame_count == raw[13]) {
65  ESP_LOGVV(TAG, "parse_header(): duplicate data packet received (%hhu).", last_frame_count);
66  return {};
67  }
68  last_frame_count = raw[13];
69 
70  return result;
71 }
72 
73 bool PVVXMiThermometer::parse_message_(const std::vector<uint8_t> &message, ParseResult &result) {
74  /*
75  All data little endian
76  uint8_t size; // = 19
77  uint8_t uid; // = 0x16, 16-bit UUID
78  uint16_t UUID; // = 0x181A, GATT Service 0x181A Environmental Sensing
79  uint8_t MAC[6]; // [0] - lo, .. [5] - hi digits
80  int16_t temperature; // x 0.01 degree [6,7]
81  uint16_t humidity; // x 0.01 % [8,9]
82  uint16_t battery_mv; // mV [10,11]
83  uint8_t battery_level; // 0..100 % [12]
84  uint8_t counter; // measurement count [13]
85  uint8_t flags; [14]
86  */
87 
88  const uint8_t *data = message.data();
89  const int data_length = 15;
90 
91  if (message.size() != data_length) {
92  ESP_LOGVV(TAG, "parse_message(): payload has wrong size (%d)!", message.size());
93  return false;
94  }
95 
96  // int16_t temperature; // x 0.01 degree [6,7]
97  const int16_t temperature = int16_t(data[6]) | (int16_t(data[7]) << 8);
98  result.temperature = temperature / 1.0e2f;
99 
100  // uint16_t humidity; // x 0.01 % [8,9]
101  const int16_t humidity = uint16_t(data[8]) | (uint16_t(data[9]) << 8);
102  result.humidity = humidity / 1.0e2f;
103 
104  // uint16_t battery_mv; // mV [10,11]
105  const int16_t battery_voltage = uint16_t(data[10]) | (uint16_t(data[11]) << 8);
106  result.battery_voltage = battery_voltage / 1.0e3f;
107 
108  // uint8_t battery_level; // 0..100 % [12]
109  result.battery_level = uint8_t(data[12]);
110 
111  return true;
112 }
113 
114 bool PVVXMiThermometer::report_results_(const optional<ParseResult> &result, const std::string &address) {
115  if (!result.has_value()) {
116  ESP_LOGVV(TAG, "report_results(): no results available.");
117  return false;
118  }
119 
120  ESP_LOGD(TAG, "Got PVVX MiThermometer (%s):", address.c_str());
121 
122  if (result->temperature.has_value()) {
123  ESP_LOGD(TAG, " Temperature: %.2f °C", *result->temperature);
124  }
125  if (result->humidity.has_value()) {
126  ESP_LOGD(TAG, " Humidity: %.2f %%", *result->humidity);
127  }
128  if (result->battery_level.has_value()) {
129  ESP_LOGD(TAG, " Battery Level: %.0f %%", *result->battery_level);
130  }
131  if (result->battery_voltage.has_value()) {
132  ESP_LOGD(TAG, " Battery Voltage: %.3f V", *result->battery_voltage);
133  }
134 
135  return true;
136 }
137 
138 } // namespace pvvx_mithermometer
139 } // namespace esphome
140 
141 #endif
bool parse_message_(const std::vector< uint8_t > &message, ParseResult &result)
uint8_t raw[35]
Definition: bl0939.h:19
bool has_value() const
Definition: optional.h:87
bool report_results_(const optional< ParseResult > &result, const std::string &address)
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
const std::vector< ServiceData > & get_service_datas() const
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
bool contains(uint8_t data1, uint8_t data2) const
Definition: ble_uuid.cpp:126
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
optional< ParseResult > parse_header_(const esp32_ble_tracker::ServiceData &service_data)