Properly calculate negative temperatures in sm300d2 (#2335)

Co-authored-by: Matt Hallacy <github@poptix.net>
This commit is contained in:
poptix 2021-09-20 02:44:18 -05:00 committed by GitHub
parent 945ed5d3bd
commit 81685573e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,8 +29,11 @@ void SM300D2Sensor::update() {
}
uint16_t calculated_checksum = this->sm300d2_checksum_(response);
// Occasionally the checksum has a +/- 0x80 offset. Negative temperatures are
// responsible for some of these. The rest are unknown/undocumented.
if ((calculated_checksum != response[SM300D2_RESPONSE_LENGTH - 1]) &&
(calculated_checksum - 0x80 != response[SM300D2_RESPONSE_LENGTH - 1])) {
(calculated_checksum - 0x80 != response[SM300D2_RESPONSE_LENGTH - 1]) &&
(calculated_checksum + 0x80 != response[SM300D2_RESPONSE_LENGTH - 1])) {
ESP_LOGW(TAG, "SM300D2 Checksum doesn't match: 0x%02X!=0x%02X", response[SM300D2_RESPONSE_LENGTH - 1],
calculated_checksum);
this->status_set_warning();
@ -46,7 +49,10 @@ void SM300D2Sensor::update() {
const uint16_t tvoc = (response[6] * 256) + response[7];
const uint16_t pm_2_5 = (response[8] * 256) + response[9];
const uint16_t pm_10_0 = (response[10] * 256) + response[11];
const float temperature = response[12] + (response[13] * 0.1);
// A negative value is indicated by adding 0x80 (128) to the temperature value
const float temperature = ((response[12] + (response[13] * 0.1)) > 128)
? (((response[12] + (response[13] * 0.1)) - 128) * -1)
: response[12] + (response[13] * 0.1);
const float humidity = response[14] + (response[15] * 0.1);
ESP_LOGD(TAG, "Received CO₂: %u ppm", co2);