21 ESP_LOGVV(TAG,
"parse_device(): unknown MAC address.");
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));
27 if (service_datas.size() != 1) {
28 ESP_LOGE(TAG,
"Unexpected service_datas size (%d)", service_datas.size());
31 const auto &service_data = service_datas[0];
33 ESP_LOGVV(TAG,
"Service data:");
34 for (
const uint8_t
byte : service_data.data) {
35 ESP_LOGVV(TAG,
"0x%02x",
byte);
38 const auto &data = service_data.data;
40 if (data.size() < 10) {
41 ESP_LOGW(TAG,
"Service data too short: %zu", data.size());
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);
52 bool has_illuminance = data[0] & 0x1;
54 if (has_illuminance && data.size() < 18) {
55 ESP_LOGW(TAG,
"Service data too short for illuminance: %zu", data.size());
60 uint8_t counter = data[1] & 0x0f;
62 ESP_LOGVV(TAG,
"Skipping already processed counter (%u)", counter);
67 uint16_t battery_millivolt = data[2] << 8 | data[3];
68 float battery_voltage = battery_millivolt / 1000.0f;
72 if (protocol_version == 1) {
73 uint16_t temp_millicelsius = data[4] << 8 | data[5];
74 temp_celsius = temp_millicelsius / 1000.0f;
76 int16_t temp_centicelsius = data[4] << 8 | data[5];
77 temp_celsius = temp_centicelsius / 100.0f;
81 uint16_t humidity = data[6] << 8 | data[7];
82 float humidity_percent = (100.0f * humidity) / (1 << 16);
85 uint16_t soil_moisture = data[8] << 8 | data[9];
86 float moisture_percent = (100.0f * soil_moisture) / (1 << 16);
89 float illuminance = has_illuminance ? data[16] << 8 | data[17] : 0.0f;
104 if (has_illuminance) {
107 ESP_LOGE(TAG,
"No lux information is present in the BLE packet");