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