ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
atc_mithermometer.cpp
Go to the documentation of this file.
1#include "atc_mithermometer.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5
7
8static const char *const TAG = "atc_mithermometer";
9
11 ESP_LOGCONFIG(TAG, "ATC MiThermometer");
12 LOG_SENSOR(" ", "Temperature", this->temperature_);
13 LOG_SENSOR(" ", "Humidity", this->humidity_);
14 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
15 LOG_SENSOR(" ", "Battery Voltage", this->battery_voltage_);
16}
17
19 if (device.address_uint64() != this->address_) {
20 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
21 return false;
22 }
23 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
24 const char *addr_str = device.address_str_to(addr_buf);
25 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_str);
26
27 bool success = false;
28 for (auto &service_data : device.get_service_datas()) {
29 auto res = parse_header_(service_data);
30 if (!res.has_value()) {
31 continue;
32 }
33 if (!(parse_message_(service_data.data, *res))) {
34 continue;
35 }
36 if (!(report_results_(res, addr_str))) {
37 continue;
38 }
39 if (res->temperature.has_value() && this->temperature_ != nullptr)
40 this->temperature_->publish_state(*res->temperature);
41 if (res->humidity.has_value() && this->humidity_ != nullptr)
42 this->humidity_->publish_state(*res->humidity);
43 if (res->battery_level.has_value() && this->battery_level_ != nullptr)
44 this->battery_level_->publish_state(*res->battery_level);
45 if (res->battery_voltage.has_value() && this->battery_voltage_ != nullptr)
46 this->battery_voltage_->publish_state(*res->battery_voltage);
47 success = true;
48 }
49 if (this->signal_strength_ != nullptr)
51
52 return success;
53}
54
55optional<ParseResult> ATCMiThermometer::parse_header_(const esp32_ble_tracker::ServiceData &service_data) {
56 ParseResult result;
57 if (!service_data.uuid.contains(0x1A, 0x18)) {
58 ESP_LOGVV(TAG, "parse_header(): no service data UUID magic bytes.");
59 return {};
60 }
61
62 auto raw = service_data.data;
63 if (raw.size() < 13) {
64 ESP_LOGVV(TAG, "parse_header_(): service data too short (%zu).", raw.size());
65 return {};
66 }
67
68 if (this->last_frame_count_ == raw[12]) {
69 ESP_LOGVV(TAG, "parse_header(): duplicate data packet received (%hhu).", this->last_frame_count_);
70 return {};
71 }
72 this->last_frame_count_ = raw[12];
73
74 return result;
75}
76
77bool ATCMiThermometer::parse_message_(const std::vector<uint8_t> &message, ParseResult &result) {
78 // Byte 0-5 mac in correct order
79 // Byte 6-7 Temperature in uint16
80 // Byte 8 Humidity in percent
81 // Byte 9 Battery in percent
82 // Byte 10-11 Battery in mV uint16_t
83 // Byte 12 frame packet counter
84
85 const uint8_t *data = message.data();
86 const int data_length = 13;
87
88 if (message.size() != data_length) {
89 ESP_LOGVV(TAG, "parse_message(): payload has wrong size (%d)!", message.size());
90 return false;
91 }
92
93 // temperature, 2 bytes, 16-bit signed integer (LE), 0.1 °C
94 const int16_t temperature = uint16_t(data[7]) | (uint16_t(data[6]) << 8);
95 result.temperature = temperature / 10.0f;
96
97 // humidity, 1 byte, 8-bit unsigned integer, 1.0 %
98 result.humidity = data[8];
99
100 // battery, 1 byte, 8-bit unsigned integer, 1.0 %
101 result.battery_level = data[9];
102
103 // battery, 2 bytes, 16-bit unsigned integer, 0.001 V
104 const int16_t battery_voltage = uint16_t(data[11]) | (uint16_t(data[10]) << 8);
105 result.battery_voltage = battery_voltage / 1.0e3f;
106
107 return true;
108}
109
110bool ATCMiThermometer::report_results_(const optional<ParseResult> &result, const char *address) {
111 if (!result.has_value()) {
112 ESP_LOGVV(TAG, "report_results(): no results available.");
113 return false;
114 }
115
116 ESP_LOGD(TAG, "Got ATC MiThermometer (%s):", address);
117
118 if (result->temperature.has_value()) {
119 ESP_LOGD(TAG, " Temperature: %.1f °C", *result->temperature);
120 }
121 if (result->humidity.has_value()) {
122 ESP_LOGD(TAG, " Humidity: %.0f %%", *result->humidity);
123 }
124 if (result->battery_level.has_value()) {
125 ESP_LOGD(TAG, " Battery Level: %.0f %%", *result->battery_level);
126 }
127 if (result->battery_voltage.has_value()) {
128 ESP_LOGD(TAG, " Battery Voltage: %.3f V", *result->battery_voltage);
129 }
130
131 return true;
132}
133
134} // namespace esphome::atc_mithermometer
135
136#endif
uint8_t address
Definition bl0906.h:4
uint8_t raw[35]
Definition bl0939.h:0
bool report_results_(const optional< ParseResult > &result, const char *address)
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
bool parse_message_(const std::vector< uint8_t > &message, ParseResult &result)
optional< ParseResult > parse_header_(const esp32_ble_tracker::ServiceData &service_data)
const char * address_str_to(char *buf) const
Buffer overload: writes "XX:XX:XX:XX:XX:XX\0" into buf (>= 18 bytes), returns buf.
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:190
bool contains(uint8_t data1, uint8_t data2) const
True if the UUID value contains the adjacent byte pair (data1, data2).
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