Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MeterW1therm.cpp to catch erroneous temperature values #610

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/protocols/MeterW1therm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,50 @@ bool MeterW1therm::W1sysHWif::readTemp(const std::string &device, double &value)
print(log_debug, "CRC not ok from %s (%s)", "w1t", dev.c_str(), buffer);
} else {
// crc ok
// now parse t=<value>
// now check for other errors:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is misplaced...
the result of fgets() is not only for error-checking, it's also what the reading is parsed from.

if (fgets(buffer, 100, fp)) { // e.g. 07 01 55 00 7f ff 0c 10 18 t=16437

///// Start of new code: Catch DS18B20-specific errors. /////
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe put this into a function to isolate it, instead of surrounding with comments?
private check_quirks(device,buffer){...
if (!check_quirks(device,buffer)) return false;

/// For reference: https://github.com/cpetrich/counterfeit_DS18B20
/// Check byte 6 of scratchpad to recognize error readings.
/// Genuine DS18B20 compute byte 6 as the difference from
/// 00001111 & <byte 0> to 00010000; most clones always use 0x0c.
/// Genuine DS18B20 are still in the 00000xxxxxxx address range
/// as of 2023; clones are not.

// Check for sensor family DS18B20 via device == "28*":
if (device[0]=='2' && device[1]=='8') {
if (!strncmp(buffer,"00 00 00 00 00 00 00 00 00",26)) {
// According to DS18B20 datasheet, this scratchpad-reading MUST be wrong.
print(log_debug, "only zeroes from %s (%s), "
"the 1wire-bus is probably unstable", "w1t", dev.c_str(), buffer);
return false;
}
// if (buffer == "?? ?? ?? ?? ?? ?? 0c*"), maybe an error has happened:
if (buffer[18]=='0' && buffer[19]=='c') {
// only do this handling for genuine DS18B20, e.g. device="28-0000*":
if (device[3]=='0' && buffer[4]=='0' && buffer[5]=='0' && buffer[6]=='0') {
// if (buffer == "50 05*"), 0x0550/16 = 85 degrees Celsius
if (!strncmp(buffer,"50 05",5)) {
// Sensor returned default boot-up value; did not complete temperature conversion.
print(log_debug, "85 degree error from %s (%s), "
"the 1wire-bus is probably unstable", "w1t", dev.c_str(), buffer);
return false;
}
// (if buffer == "ff 07*"), 0x07ff/16 = 127.9375 degrees Celsius
if (!strncmp(buffer,"ff 07",5)) {
// Sensor returned code for insufficient power during temperature conversion.
// This can happen in parasitic 2-wire circuits with floating Vcc pins.
print(log_debug, "insufficient power error from %s (%s), "
"maybe Vcc was left floating", "w1t", dev.c_str(), buffer);
return false;
}
}
}
}
///// End of new code. /////

// now parse t=<value>
char *pos = strstr(buffer, "t=");
if (pos) {
pos += 2;
Expand Down