ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
b_parasite.cpp
Go to the documentation of this file.
1#include "b_parasite.h"
2#include "esphome/core/log.h"
3
5
6static const char *const TAG = "b_parasite";
7
9 ESP_LOGCONFIG(TAG, "b_parasite");
10 LOG_SENSOR(" ", "Battery Voltage", this->battery_voltage_);
11 LOG_SENSOR(" ", "Temperature", this->temperature_);
12 LOG_SENSOR(" ", "Humidity", this->humidity_);
13 LOG_SENSOR(" ", "Soil Moisture", this->soil_moisture_);
14 LOG_SENSOR(" ", "Illuminance", this->illuminance_);
15}
16
18 if (device.address_uint64() != address_) {
19 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
20 return false;
21 }
22 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
23 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str_to(addr_buf));
24 const auto &service_datas = device.get_service_datas();
25 if (service_datas.size() != 1) {
26 ESP_LOGE(TAG, "Unexpected service_datas size (%d)", service_datas.size());
27 return false;
28 }
29 const auto &service_data = service_datas[0];
30
31 ESP_LOGVV(TAG, "Service data:");
32 for (const uint8_t byte : service_data.data) {
33 ESP_LOGVV(TAG, "0x%02x", byte);
34 }
35
36 const auto &data = service_data.data;
37
38 if (data.size() < 10) {
39 ESP_LOGW(TAG, "Service data too short: %zu", data.size());
40 return false;
41 }
42
43 const uint8_t protocol_version = data[0] >> 4;
44 if (protocol_version != 1 && protocol_version != 2) {
45 ESP_LOGE(TAG, "Unsupported protocol version: %u", protocol_version);
46 return false;
47 }
48
49 // Some b-parasite versions have an (optional) illuminance sensor.
50 bool has_illuminance = data[0] & 0x1;
51
52 if (has_illuminance && data.size() < 18) {
53 ESP_LOGW(TAG, "Service data too short for illuminance: %zu", data.size());
54 return false;
55 }
56
57 // Counter for deduplicating messages.
58 uint8_t counter = data[1] & 0x0f;
59 if (last_processed_counter_ == counter) {
60 ESP_LOGVV(TAG, "Skipping already processed counter (%u)", counter);
61 return false;
62 }
63
64 // Battery voltage in millivolts.
65 uint16_t battery_millivolt = data[2] << 8 | data[3];
66 float battery_voltage = battery_millivolt / 1000.0f;
67
68 // Temperature in 1000 * Celsius (protocol v1) or 100 * Celsius (protocol v2).
69 float temp_celsius;
70 if (protocol_version == 1) {
71 uint16_t temp_millicelsius = data[4] << 8 | data[5];
72 temp_celsius = temp_millicelsius / 1000.0f;
73 } else {
74 int16_t temp_centicelsius = data[4] << 8 | data[5];
75 temp_celsius = temp_centicelsius / 100.0f;
76 }
77
78 // Relative air humidity in the range [0, 2^16).
79 uint16_t humidity = data[6] << 8 | data[7];
80 float humidity_percent = (100.0f * humidity) / (1 << 16);
81
82 // Relative soil moisture in [0 - 2^16).
83 uint16_t soil_moisture = data[8] << 8 | data[9];
84 float moisture_percent = (100.0f * soil_moisture) / (1 << 16);
85
86 // Ambient light in lux.
87 float illuminance = has_illuminance ? data[16] << 8 | data[17] : 0.0f;
88
89 if (battery_voltage_ != nullptr) {
90 battery_voltage_->publish_state(battery_voltage);
91 }
92 if (temperature_ != nullptr) {
93 temperature_->publish_state(temp_celsius);
94 }
95 if (humidity_ != nullptr) {
96 humidity_->publish_state(humidity_percent);
97 }
98 if (soil_moisture_ != nullptr) {
99 soil_moisture_->publish_state(moisture_percent);
100 }
101 if (illuminance_ != nullptr) {
102 if (has_illuminance) {
103 illuminance_->publish_state(illuminance);
104 } else {
105 ESP_LOGE(TAG, "No lux information is present in the BLE packet");
106 }
107 }
108
109 last_processed_counter_ = counter;
110 return true;
111}
112
113} // namespace esphome::b_parasite
bool parse_device(const ble_device_base::ESPBTDevice &device) override
sensor::Sensor * temperature_
Definition b_parasite.h:29
sensor::Sensor * soil_moisture_
Definition b_parasite.h:31
sensor::Sensor * battery_voltage_
Definition b_parasite.h:28
sensor::Sensor * illuminance_
Definition b_parasite.h:32
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
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68