19 ESP_LOGVV(TAG,
"parse_device(): unknown MAC address.");
22 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
23 ESP_LOGVV(TAG,
"parse_device(): MAC address %s found.", device.
address_str_to(addr_buf));
25 if (service_datas.size() != 1) {
26 ESP_LOGE(TAG,
"Unexpected service_datas size (%d)", service_datas.size());
29 const auto &service_data = service_datas[0];
31 ESP_LOGVV(TAG,
"Service data:");
32 for (
const uint8_t
byte : service_data.data) {
33 ESP_LOGVV(TAG,
"0x%02x",
byte);
36 const auto &data = service_data.data;
38 if (data.size() < 10) {
39 ESP_LOGW(TAG,
"Service data too short: %zu", data.size());
43 const uint8_t protocol_version = data[0] >> 4;
44 if (protocol_version != 1 && protocol_version != 2) {
45 ESP_LOGE(TAG,
"Unsupported protocol version: %u", protocol_version);
50 bool has_illuminance = data[0] & 0x1;
52 if (has_illuminance && data.size() < 18) {
53 ESP_LOGW(TAG,
"Service data too short for illuminance: %zu", data.size());
58 uint8_t counter = data[1] & 0x0f;
60 ESP_LOGVV(TAG,
"Skipping already processed counter (%u)", counter);
65 uint16_t battery_millivolt = data[2] << 8 | data[3];
66 float battery_voltage = battery_millivolt / 1000.0f;
70 if (protocol_version == 1) {
71 uint16_t temp_millicelsius = data[4] << 8 | data[5];
72 temp_celsius = temp_millicelsius / 1000.0f;
74 int16_t temp_centicelsius = data[4] << 8 | data[5];
75 temp_celsius = temp_centicelsius / 100.0f;
79 uint16_t humidity = data[6] << 8 | data[7];
80 float humidity_percent = (100.0f * humidity) / (1 << 16);
83 uint16_t soil_moisture = data[8] << 8 | data[9];
84 float moisture_percent = (100.0f * soil_moisture) / (1 << 16);
87 float illuminance = has_illuminance ? data[16] << 8 | data[17] : 0.0f;
102 if (has_illuminance) {
105 ESP_LOGE(TAG,
"No lux information is present in the BLE packet");