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