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