22 ESP_LOGVV(TAG,
"parse_device(): unknown MAC address.");
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));
28 if (service_datas.size() != 1) {
29 ESP_LOGE(TAG,
"Unexpected service_datas size (%d)", service_datas.size());
32 const auto &service_data = service_datas[0];
34 ESP_LOGVV(TAG,
"Service data:");
35 for (
const uint8_t
byte : service_data.data) {
36 ESP_LOGVV(TAG,
"0x%02x",
byte);
39 const auto &data = service_data.data;
41 if (data.size() < 10) {
42 ESP_LOGW(TAG,
"Service data too short: %zu", data.size());
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);
53 bool has_illuminance = data[0] & 0x1;
55 if (has_illuminance && data.size() < 18) {
56 ESP_LOGW(TAG,
"Service data too short for illuminance: %zu", data.size());
61 uint8_t counter = data[1] & 0x0f;
63 ESP_LOGVV(TAG,
"Skipping already processed counter (%u)", counter);
68 uint16_t battery_millivolt = data[2] << 8 | data[3];
69 float battery_voltage = battery_millivolt / 1000.0f;
73 if (protocol_version == 1) {
74 uint16_t temp_millicelsius = data[4] << 8 | data[5];
75 temp_celsius = temp_millicelsius / 1000.0f;
77 int16_t temp_centicelsius = data[4] << 8 | data[5];
78 temp_celsius = temp_centicelsius / 100.0f;
82 uint16_t humidity = data[6] << 8 | data[7];
83 float humidity_percent = (100.0f * humidity) / (1 << 16);
86 uint16_t soil_moisture = data[8] << 8 | data[9];
87 float moisture_percent = (100.0f * soil_moisture) / (1 << 16);
90 float illuminance = has_illuminance ? data[16] << 8 | data[17] : 0.0f;
105 if (has_illuminance) {
108 ESP_LOGE(TAG,
"No lux information is present in the BLE packet");