temp, humi and batt in % working, todo: battery in mV

This commit is contained in:
Alexander Pohl 2020-09-05 09:36:49 +02:00
parent fc0ddc53b8
commit 38b7c1588a

View file

@ -52,7 +52,7 @@ bool ATCMiThermometer::parse_device(const esp32_ble_tracker::ESPBTDevice &device
optional<ParseResult> ATCMiThermometer::parse_header(const esp32_ble_tracker::ServiceData &service_data) { optional<ParseResult> ATCMiThermometer::parse_header(const esp32_ble_tracker::ServiceData &service_data) {
ParseResult result; ParseResult result;
if (!service_data.uuid.contains(0x18, 0x1A)) { if (!service_data.uuid.contains(0x1A, 0x18)) {
ESP_LOGVV(TAG, "parse_header(): no service data UUID magic bytes."); ESP_LOGVV(TAG, "parse_header(): no service data UUID magic bytes.");
return {}; return {};
} }
@ -60,12 +60,12 @@ optional<ParseResult> ATCMiThermometer::parse_header(const esp32_ble_tracker::Se
auto raw = service_data.data; auto raw = service_data.data;
static uint8_t last_frame_count = 0; static uint8_t last_frame_count = 0;
if (last_frame_count == raw[16]) { if (last_frame_count == raw[12]) {
ESP_LOGVV(TAG, "parse_xiaomi_header(): duplicate data packet received (%d).", static_cast<int>(last_frame_count)); ESP_LOGVV(TAG, "parse_xiaomi_header(): duplicate data packet received (%d).", static_cast<int>(last_frame_count));
result.is_duplicate = true; result.is_duplicate = true;
return {}; return {};
} }
last_frame_count = raw[16]; last_frame_count = raw[12];
result.is_duplicate = false; result.is_duplicate = false;
return result; return result;
@ -80,31 +80,23 @@ bool ATCMiThermometer::parse_message(const std::vector<uint8_t> &message, ParseR
// Byte 15-16 Battery in mV uint16_t // Byte 15-16 Battery in mV uint16_t
// Byte 17 frame packet counter // Byte 17 frame packet counter
const uint8_t *raw = message.data(); const uint8_t *data = message.data();
const uint8_t *data = raw + 3; const int data_length = 13;
if (message.size() != 17) { if (message.size() != data_length) {
ESP_LOGVV(TAG, "parse_message(): payload has wrong size (%d)!", data_length); ESP_LOGVV(TAG, "parse_message(): payload has wrong size (%d)!", message.size());
return false; return false;
} }
// temperature, 2 bytes, 16-bit signed integer (LE), 0.1 °C // temperature, 2 bytes, 16-bit signed integer (LE), 0.1 °C
else if (raw[0] == 0x04) { const int16_t temperature = uint16_t(data[7]) | (uint16_t(data[6]) << 8);
const int16_t temperature = uint16_t(data[0]) | (uint16_t(data[1]) << 8); result.temperature = temperature / 10.0f;
result.temperature = temperature / 10.0f;
}
// humidity, 2 bytes, 16-bit signed integer (LE), 0.1 %
else if (raw[0] == 0x06) {
const int16_t humidity = uint16_t(data[0]) | (uint16_t(data[1]) << 8);
result.humidity = humidity / 10.0f;
}
// battery, 1 byte, 8-bit unsigned integer, 1 %
else if (raw[0] == 0x0A) {
result.battery_level = data[0];
} else { // humidity, 1 byte, 8-bit unsigned integer, 1 %
return false; result.humidity = data[8];
}
// battery, 1 byte, 8-bit unsigned integer, 1 %
result.battery_level = data[9];
return true; return true;
} }