From 41eaac008485f9d81a9642a7f2db5fce22fd809e Mon Sep 17 00:00:00 2001 From: Yurii Date: Sat, 17 Jan 2026 19:04:01 +0300 Subject: [PATCH 1/2] fix: filtering fake data from ds18b20 --- DallasTemperature.cpp | 8 ++++++++ DallasTemperature.h | 4 ++++ keywords.txt | 3 +++ test/unit_test_001.cpp | 4 ++++ 4 files changed, 19 insertions(+) diff --git a/DallasTemperature.cpp b/DallasTemperature.cpp index fd15d85..bc5a607 100644 --- a/DallasTemperature.cpp +++ b/DallasTemperature.cpp @@ -558,6 +558,14 @@ int32_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, ui | neg; } + // detect fake data + if (deviceAddress[DSROM_FAMILY] == DS18B20MODEL && scratchPad[6] == 0x0C) { + if ((scratchPad[0] == 0x50 && scratchPad[1] == 0x05) || + (scratchPad[0] == 0xFF && scratchPad[1] == 0x07)) { + return DEVICE_POWER_ON_RESET_RAW; + } + } + /* DS1820 and DS18S20 have a 9-bit temperature register. diff --git a/DallasTemperature.h b/DallasTemperature.h index 8bc1d44..eb2efe5 100644 --- a/DallasTemperature.h +++ b/DallasTemperature.h @@ -46,6 +46,10 @@ #define DEVICE_FAULT_SHORTVDD_F -421.599976 #define DEVICE_FAULT_SHORTVDD_RAW -32256 +#define DEVICE_POWER_ON_RESET_C -251 +#define DEVICE_POWER_ON_RESET_F -419.799988 +#define DEVICE_POWER_ON_RESET_RAW -32128 + // Configuration Constants #define MAX_CONVERSION_TIMEOUT 750 #define MAX_INITIALIZATION_RETRIES 3 diff --git a/keywords.txt b/keywords.txt index a471c3e..4ca9b2c 100644 --- a/keywords.txt +++ b/keywords.txt @@ -85,3 +85,6 @@ DEVICE_FAULT_SHORTGND_RAW LITERAL1 DEVICE_FAULT_SHORTVDD_C LITERAL1 DEVICE_FAULT_SHORTVDD_F LITERAL1 DEVICE_FAULT_SHORTVDD_RAW LITERAL1 +DEVICE_POWER_ON_RESET_C LITERAL1 +DEVICE_POWER_ON_RESET_F LITERAL1 +DEVICE_POWER_ON_RESET_RAW LITERAL1 \ No newline at end of file diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index 54fefc2..8779e7a 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -47,6 +47,10 @@ unittest(test_error_code) { assertEqual(DEVICE_FAULT_SHORTVDD_C, -252); assertEqualFloat(DEVICE_FAULT_SHORTVDD_F, -421.6, 0.1); assertEqual(DEVICE_FAULT_SHORTVDD_RAW, -32256); + + assertEqual(DEVICE_POWER_ON_RESET_C, -251); + assertEqualFloat(DEVICE_POWER_ON_RESET_F, -419.8, 0.1); + assertEqual(DEVICE_POWER_ON_RESET_RAW, -32128); } // Test basic initialization and functionality of the DallasTemperature library From ffc7de927d5d6c604cb2cdf75838a194a3e6e7b9 Mon Sep 17 00:00:00 2001 From: Yurii Date: Sat, 24 Jan 2026 21:58:38 +0300 Subject: [PATCH 2/2] refactor: added `DEVICE_INSUFFICIENT_POWER_RAW` for insufficient power error --- DallasTemperature.cpp | 9 +++++---- DallasTemperature.h | 4 ++++ keywords.txt | 5 ++++- test/unit_test_001.cpp | 4 ++++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/DallasTemperature.cpp b/DallasTemperature.cpp index bc5a607..29b7bc5 100644 --- a/DallasTemperature.cpp +++ b/DallasTemperature.cpp @@ -558,11 +558,12 @@ int32_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, ui | neg; } - // detect fake data - if (deviceAddress[DSROM_FAMILY] == DS18B20MODEL && scratchPad[6] == 0x0C) { - if ((scratchPad[0] == 0x50 && scratchPad[1] == 0x05) || - (scratchPad[0] == 0xFF && scratchPad[1] == 0x07)) { + // detect POR and insufficient power conditions + if (deviceAddress[DSROM_FAMILY] == DS18B20MODEL) { + if (scratchPad[0] == 0x50 && scratchPad[1] == 0x05 && scratchPad[6] == 0x0C) { return DEVICE_POWER_ON_RESET_RAW; + } else if (scratchPad[0] == 0xFF && scratchPad[1] == 0x07) { + return DEVICE_INSUFFICIENT_POWER_RAW; } } diff --git a/DallasTemperature.h b/DallasTemperature.h index eb2efe5..49cb3f5 100644 --- a/DallasTemperature.h +++ b/DallasTemperature.h @@ -50,6 +50,10 @@ #define DEVICE_POWER_ON_RESET_F -419.799988 #define DEVICE_POWER_ON_RESET_RAW -32128 +#define DEVICE_INSUFFICIENT_POWER_C -250 +#define DEVICE_INSUFFICIENT_POWER_F -418.0 +#define DEVICE_INSUFFICIENT_POWER_RAW -32000 + // Configuration Constants #define MAX_CONVERSION_TIMEOUT 750 #define MAX_INITIALIZATION_RETRIES 3 diff --git a/keywords.txt b/keywords.txt index 4ca9b2c..8a87ce2 100644 --- a/keywords.txt +++ b/keywords.txt @@ -87,4 +87,7 @@ DEVICE_FAULT_SHORTVDD_F LITERAL1 DEVICE_FAULT_SHORTVDD_RAW LITERAL1 DEVICE_POWER_ON_RESET_C LITERAL1 DEVICE_POWER_ON_RESET_F LITERAL1 -DEVICE_POWER_ON_RESET_RAW LITERAL1 \ No newline at end of file +DEVICE_POWER_ON_RESET_RAW LITERAL1 +DEVICE_INSUFFICIENT_POWER_C LITERAL1 +DEVICE_INSUFFICIENT_POWER_F LITERAL1 +DEVICE_INSUFFICIENT_POWER_RAW LITERAL1 \ No newline at end of file diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index 8779e7a..2ce9f76 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -51,6 +51,10 @@ unittest(test_error_code) { assertEqual(DEVICE_POWER_ON_RESET_C, -251); assertEqualFloat(DEVICE_POWER_ON_RESET_F, -419.8, 0.1); assertEqual(DEVICE_POWER_ON_RESET_RAW, -32128); + + assertEqual(DEVICE_INSUFFICIENT_POWER_C, -250); + assertEqualFloat(DEVICE_INSUFFICIENT_POWER_F, -418.0, 0.1); + assertEqual(DEVICE_INSUFFICIENT_POWER_RAW, -32000); } // Test basic initialization and functionality of the DallasTemperature library