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