ESPHome 2026.2.4
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
6namespace esphome {
7namespace atc_mithermometer {
8
9static const char *const TAG = "atc_mithermometer";
10
12 ESP_LOGCONFIG(TAG, "ATC 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 success = true;
49 }
50 if (this->signal_strength_ != nullptr)
52
53 return success;
54}
55
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
65 static uint8_t last_frame_count = 0;
66 if (last_frame_count == raw[12]) {
67 ESP_LOGVV(TAG, "parse_header(): duplicate data packet received (%hhu).", last_frame_count);
68 return {};
69 }
70 last_frame_count = raw[12];
71
72 return result;
73}
74
75bool ATCMiThermometer::parse_message_(const std::vector<uint8_t> &message, ParseResult &result) {
76 // Byte 0-5 mac in correct order
77 // Byte 6-7 Temperature in uint16
78 // Byte 8 Humidity in percent
79 // Byte 9 Battery in percent
80 // Byte 10-11 Battery in mV uint16_t
81 // Byte 12 frame packet counter
82
83 const uint8_t *data = message.data();
84 const int data_length = 13;
85
86 if (message.size() != data_length) {
87 ESP_LOGVV(TAG, "parse_message(): payload has wrong size (%d)!", message.size());
88 return false;
89 }
90
91 // temperature, 2 bytes, 16-bit signed integer (LE), 0.1 °C
92 const int16_t temperature = uint16_t(data[7]) | (uint16_t(data[6]) << 8);
93 result.temperature = temperature / 10.0f;
94
95 // humidity, 1 byte, 8-bit unsigned integer, 1.0 %
96 result.humidity = data[8];
97
98 // battery, 1 byte, 8-bit unsigned integer, 1.0 %
99 result.battery_level = data[9];
100
101 // battery, 2 bytes, 16-bit unsigned integer, 0.001 V
102 const int16_t battery_voltage = uint16_t(data[11]) | (uint16_t(data[10]) << 8);
103 result.battery_voltage = battery_voltage / 1.0e3f;
104
105 return true;
106}
107
109 if (!result.has_value()) {
110 ESP_LOGVV(TAG, "report_results(): no results available.");
111 return false;
112 }
113
114 ESP_LOGD(TAG, "Got ATC MiThermometer (%s):", address);
115
116 if (result->temperature.has_value()) {
117 ESP_LOGD(TAG, " Temperature: %.1f °C", *result->temperature);
118 }
119 if (result->humidity.has_value()) {
120 ESP_LOGD(TAG, " Humidity: %.0f %%", *result->humidity);
121 }
122 if (result->battery_level.has_value()) {
123 ESP_LOGD(TAG, " Battery Level: %.0f %%", *result->battery_level);
124 }
125 if (result->battery_voltage.has_value()) {
126 ESP_LOGD(TAG, " Battery Voltage: %.3f V", *result->battery_voltage);
127 }
128
129 return true;
130}
131
132} // namespace atc_mithermometer
133} // namespace esphome
134
135#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)
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 has_value() const
Definition optional.h:92
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