ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
thermopro_ble.cpp
Go to the documentation of this file.
1#include "thermopro_ble.h"
2#include <algorithm>
3#include <cmath>
5#include "esphome/core/log.h"
6
8
9ESPHOME_LOG_TAG(TAG, "thermopro_ble");
10
11// Largest record (company ID plus payload) of all supported devices
12static constexpr size_t MAX_DATA_SIZE = 24;
13
14struct ParseResult {
15 float temperature{NAN};
16 float external_temperature{NAN};
17 float humidity{NAN};
18 float battery_level{NAN};
19};
20
21using DeviceParser = bool (*)(const uint8_t *data, size_t data_size, ParseResult &result);
22
23// Fitted A*tanh(B*x+C)+D to the TP96x battery voltage curve, used with permission from
24// https://github.com/Bluetooth-Devices/thermopro-ble/blob/main/src/thermopro_ble/parser.py
25static float tp96_battery(uint16_t voltage) {
26 float level = 52.317286f * std::tanh(static_cast<float>(voltage) / 273.624277936f - 8.76485439394f) + 51.06925f;
27 return clamp(level, 0.0f, 100.0f);
28}
29
30// TP97x temperatures are °F offset by 54
31static float tp97_celsius(float raw) { return fahrenheit_to_celsius(raw - 54.0f); }
32
33// Some probes append 6 bytes of the base station MAC to the 7-byte frame
34static bool parse_tp96(const uint8_t *data, size_t data_size, ParseResult &result) {
35 if (data_size != 7 && data_size != 13) {
36 ESP_LOGVV(TAG, "Probe payload size %u not supported", static_cast<unsigned>(data_size));
37 return false;
38 }
39 // Temperatures are offset by 30 °C
40 result.temperature = static_cast<float>(encode_value_le<uint16_t>(data + 1)) - 30.0f;
41 result.battery_level = tp96_battery(encode_value_le<uint16_t>(data + 3));
42 result.external_temperature = static_cast<float>(encode_value_le<uint16_t>(data + 5)) - 30.0f;
43 return true;
44}
45
46// TP97x also sends the TP96 frame
47static bool parse_tp97(const uint8_t *data, size_t data_size, ParseResult &result) {
48 if (data_size != 23)
49 return parse_tp96(data, data_size, result);
50 result.external_temperature = tp97_celsius(encode_value_le<uint16_t>(data + 1));
51 result.battery_level = tp96_battery(encode_value_le<uint16_t>(data + 3));
52 // Probe tip, a float
53 result.temperature = tp97_celsius(bit_cast<float>(encode_value_le<uint32_t>(data + 5)));
54 return true;
55}
56
57static bool parse_tp3(const uint8_t *data, size_t data_size, ParseResult &result) {
58 // Battery bits: low, half, full, unknown
59 static constexpr float BATTERY_LEVELS[] = {1.0f, 50.0f, 100.0f, NAN};
60 if (data_size < 6) {
61 ESP_LOGVV(TAG, "TP3 payload size %u < 6", static_cast<unsigned>(data_size));
62 return false;
63 }
64 // Temperature and humidity all 0xFF: no reading
65 if (data[1] != 0xFF || data[2] != 0xFF || data[3] != 0xFF) {
66 result.temperature = static_cast<float>(encode_value_le<int16_t>(data + 1)) * 0.1f;
67 result.humidity = static_cast<float>(data[3]);
68 }
69 result.battery_level = BATTERY_LEVELS[data[4] & 0x3];
70 return true;
71}
72
73struct DeviceParserMapping {
74 const char *prefix;
75 DeviceParser parser;
76};
77
78static constexpr DeviceParserMapping DEVICE_PARSER_MAP[] = {
79 {"TP972", parse_tp97}, {"TP970", parse_tp97}, {"TP96", parse_tp96}, {"TP3", parse_tp3}};
80
81static DeviceParser find_device_parser(StringRef device_name) {
82 for (const auto &mapping : DEVICE_PARSER_MAP) {
83 if (device_name.starts_with(mapping.prefix))
84 return mapping.parser;
85 }
86 return nullptr;
87}
88
89static void publish_if_set(sensor::Sensor *sensor, float value) {
90 if (sensor != nullptr && !std::isnan(value))
91 sensor->publish_state(value);
92}
93
94void ThermoProBLE::dump_child_config() {
95 ESP_LOGCONFIG(TAG, "ThermoPro BLE");
96 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
97 LOG_SENSOR(" ", "External Temperature", this->external_temperature_sensor_);
98 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
99 LOG_SENSOR(" ", "Battery Level", this->battery_level_sensor_);
100 LOG_SENSOR(" ", "Signal Strength", this->signal_strength_sensor_);
101}
102
103bool ThermoProBLE::parse_device(const ble_device_base::ESPBTDevice &device) {
104 if (device.address_uint64() != this->address_)
105 return false;
106
107 DeviceParser parser = find_device_parser(device.get_name());
108 if (parser == nullptr) {
109 ESP_LOGVV(TAG, "Unknown device type %s", device.get_name().c_str());
110 return false;
111 }
112
113 PUBLISH_STATE(this->signal_strength_sensor_, device.get_rssi());
114
115 bool success = false;
116 for (const auto &mfg_data : device.get_manufacturer_datas()) {
117 size_t data_size = mfg_data.data.size() + 2;
118 if (data_size > MAX_DATA_SIZE) {
119 ESP_LOGVV(TAG, "Payload size %u > %u", static_cast<unsigned>(data_size), static_cast<unsigned>(MAX_DATA_SIZE));
120 continue;
121 }
122
123 // The parsers index the whole record, company ID included
124 uint16_t company_id = mfg_data.uuid.uuid16();
125 uint8_t data[MAX_DATA_SIZE] = {static_cast<uint8_t>(company_id), static_cast<uint8_t>(company_id >> 8)};
126 std::copy(mfg_data.data.begin(), mfg_data.data.end(), data + 2);
127
128 ParseResult result;
129 if (!parser(data, data_size, result))
130 continue;
131
132 publish_if_set(this->temperature_sensor_, result.temperature);
133 publish_if_set(this->external_temperature_sensor_, result.external_temperature);
134 publish_if_set(this->humidity_sensor_, result.humidity);
135 publish_if_set(this->battery_level_sensor_, result.battery_level);
136 success = true;
137 }
138
139 return success;
140}
141
142} // namespace esphome::thermopro_ble
uint8_t raw[35]
Definition bl0939.h:0
std::span< const uint8_t > data
bool(*)(const uint8_t *data, size_t data_size, ParseResult &result) DeviceParser
ESPHOME_LOG_TAG(TAG, "thermopro_ble")
uint32_t value
Definition hal.cpp:59
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1268
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
Definition helpers.h:2112
esp_err_t result