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