ESPHome 2026.3.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 const uint8_t protocol_version = data[0] >> 4;
42 if (protocol_version != 1 && protocol_version != 2) {
43 ESP_LOGE(TAG, "Unsupported protocol version: %u", protocol_version);
44 return false;
45 }
46
47 // Some b-parasite versions have an (optional) illuminance sensor.
48 bool has_illuminance = data[0] & 0x1;
49
50 // Counter for deduplicating messages.
51 uint8_t counter = data[1] & 0x0f;
52 if (last_processed_counter_ == counter) {
53 ESP_LOGVV(TAG, "Skipping already processed counter (%u)", counter);
54 return false;
55 }
56
57 // Battery voltage in millivolts.
58 uint16_t battery_millivolt = data[2] << 8 | data[3];
59 float battery_voltage = battery_millivolt / 1000.0f;
60
61 // Temperature in 1000 * Celsius (protocol v1) or 100 * Celsius (protocol v2).
62 float temp_celsius;
63 if (protocol_version == 1) {
64 uint16_t temp_millicelsius = data[4] << 8 | data[5];
65 temp_celsius = temp_millicelsius / 1000.0f;
66 } else {
67 int16_t temp_centicelsius = data[4] << 8 | data[5];
68 temp_celsius = temp_centicelsius / 100.0f;
69 }
70
71 // Relative air humidity in the range [0, 2^16).
72 uint16_t humidity = data[6] << 8 | data[7];
73 float humidity_percent = (100.0f * humidity) / (1 << 16);
74
75 // Relative soil moisture in [0 - 2^16).
76 uint16_t soil_moisture = data[8] << 8 | data[9];
77 float moisture_percent = (100.0f * soil_moisture) / (1 << 16);
78
79 // Ambient light in lux.
80 float illuminance = has_illuminance ? data[16] << 8 | data[17] : 0.0f;
81
82 if (battery_voltage_ != nullptr) {
83 battery_voltage_->publish_state(battery_voltage);
84 }
85 if (temperature_ != nullptr) {
86 temperature_->publish_state(temp_celsius);
87 }
88 if (humidity_ != nullptr) {
89 humidity_->publish_state(humidity_percent);
90 }
91 if (soil_moisture_ != nullptr) {
92 soil_moisture_->publish_state(moisture_percent);
93 }
94 if (illuminance_ != nullptr) {
95 if (has_illuminance) {
96 illuminance_->publish_state(illuminance);
97 } else {
98 ESP_LOGE(TAG, "No lux information is present in the BLE packet");
99 }
100 }
101
102 last_processed_counter_ = counter;
103 return true;
104}
105
106} // namespace b_parasite
107} // namespace esphome
108
109#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:65
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7