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