Skip to content

Commit 41eaac0

Browse files
committed
fix: filtering fake data from ds18b20
1 parent 4da548a commit 41eaac0

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

DallasTemperature.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,14 @@ int32_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, ui
558558
| neg;
559559
}
560560

561+
// detect fake data
562+
if (deviceAddress[DSROM_FAMILY] == DS18B20MODEL && scratchPad[6] == 0x0C) {
563+
if ((scratchPad[0] == 0x50 && scratchPad[1] == 0x05) ||
564+
(scratchPad[0] == 0xFF && scratchPad[1] == 0x07)) {
565+
return DEVICE_POWER_ON_RESET_RAW;
566+
}
567+
}
568+
561569
/*
562570
DS1820 and DS18S20 have a 9-bit temperature register.
563571

DallasTemperature.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
#define DEVICE_FAULT_SHORTVDD_F -421.599976
4747
#define DEVICE_FAULT_SHORTVDD_RAW -32256
4848

49+
#define DEVICE_POWER_ON_RESET_C -251
50+
#define DEVICE_POWER_ON_RESET_F -419.799988
51+
#define DEVICE_POWER_ON_RESET_RAW -32128
52+
4953
// Configuration Constants
5054
#define MAX_CONVERSION_TIMEOUT 750
5155
#define MAX_INITIALIZATION_RETRIES 3

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,6 @@ DEVICE_FAULT_SHORTGND_RAW LITERAL1
8585
DEVICE_FAULT_SHORTVDD_C LITERAL1
8686
DEVICE_FAULT_SHORTVDD_F LITERAL1
8787
DEVICE_FAULT_SHORTVDD_RAW LITERAL1
88+
DEVICE_POWER_ON_RESET_C LITERAL1
89+
DEVICE_POWER_ON_RESET_F LITERAL1
90+
DEVICE_POWER_ON_RESET_RAW LITERAL1

test/unit_test_001.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ unittest(test_error_code) {
4747
assertEqual(DEVICE_FAULT_SHORTVDD_C, -252);
4848
assertEqualFloat(DEVICE_FAULT_SHORTVDD_F, -421.6, 0.1);
4949
assertEqual(DEVICE_FAULT_SHORTVDD_RAW, -32256);
50+
51+
assertEqual(DEVICE_POWER_ON_RESET_C, -251);
52+
assertEqualFloat(DEVICE_POWER_ON_RESET_F, -419.8, 0.1);
53+
assertEqual(DEVICE_POWER_ON_RESET_RAW, -32128);
5054
}
5155

5256
// Test basic initialization and functionality of the DallasTemperature library

0 commit comments

Comments
 (0)