From 488aec2166482f30a81cbc4a5649bceff619d440 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Fri, 22 Nov 2024 18:45:17 +0100 Subject: [PATCH 1/3] fix #100, negative representation --- CHANGELOG.md | 6 ++++++ README.md | 7 +++++++ dhtnew.cpp | 27 +++++++++++++++++++-------- dhtnew.h | 4 ++-- library.json | 2 +- library.properties | 2 +- 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28c09b7..60b9ebb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.5.0] - 2024-11-22 +- fix #100, different negative algorithm support +- time to bump version + +---- + ## [0.4.21] - 2024-10-09 - add dhtnew_pulse_diag_ext.ino for extended diagnosis. diff --git a/README.md b/README.md index 9f08458..75b872a 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,13 @@ Feedback (both positive and negative) about the AM232X sensors is welcome. **Note: check the datasheet how to connect!** +### 0.5.0 Negative temperature + +Apparently there are DHT22's which use another representation for negative temperatures. +Since 0.5.0 the library automatically detects this and chooses the right algorithm. +See issue #100. + + ### Related - https://github.com/RobTillaart/DHTNew diff --git a/dhtnew.cpp b/dhtnew.cpp index b97fed6..fcfa9f6 100644 --- a/dhtnew.cpp +++ b/dhtnew.cpp @@ -1,7 +1,7 @@ // // FILE: dhtnew.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.4.21 +// VERSION: 0.5.0 // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino // URL: https://github.com/RobTillaart/DHTNEW // @@ -193,17 +193,28 @@ int DHTNEW::_read() else // DHT22, DHT33, DHT44, compatible + Si7021 { _humidity = (_bits[0] * 256 + _bits[1]) * 0.1; - int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]); - if (t == 0) + // positive temperature? + if ((_bits[2] & 0x80) != 0x80 ) { - _temperature = 0.0; // prevent -0.0; + int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]); + if (t == 0) + { + _temperature = 0.0; // prevent -0.0; + } + _temperature = t * 0.1; } - else + else // negative temperature { - _temperature = t * 0.1; - if((_bits[2] & 0x80) == 0x80 ) + // See issue #100 - 2 different representations + if ((_bits[2] & 0x40) != 0x40 ) + { + int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]); + _temperature = t * -0.1; + } + else { - _temperature = -_temperature; + int16_t t = (_bits[2] << 8) + _bits[3]; // 16 bits, as raw int + _temperature = t * 0.1; } } } diff --git a/dhtnew.h b/dhtnew.h index f57dcf4..bd484c5 100644 --- a/dhtnew.h +++ b/dhtnew.h @@ -2,7 +2,7 @@ // // FILE: dhtnew.h // AUTHOR: Rob Tillaart -// VERSION: 0.4.21 +// VERSION: 0.5.0 // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino // URL: https://github.com/RobTillaart/DHTNEW // @@ -18,7 +18,7 @@ #include "Arduino.h" -#define DHTNEW_LIB_VERSION (F("0.4.21")) +#define DHTNEW_LIB_VERSION (F("0.5.0")) #define DHTLIB_OK 0 diff --git a/library.json b/library.json index 878b32b..9fdbe36 100644 --- a/library.json +++ b/library.json @@ -13,7 +13,7 @@ "type": "git", "url": "https://github.com/RobTillaart/DHTNEW.git" }, - "version": "0.4.21", + "version": "0.5.0", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/library.properties b/library.properties index 02fdeab..1f43fe2 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=DHTNEW -version=0.4.21 +version=0.5.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for DHT temperature and humidity sensor, with automatic sensortype recognition. From 08745f9d39d157b5696d16ff81b26eb96b02a995 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Fri, 22 Nov 2024 19:02:23 +0100 Subject: [PATCH 2/3] fix #100, negative representation --- dhtnew.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/dhtnew.cpp b/dhtnew.cpp index fcfa9f6..dfce078 100644 --- a/dhtnew.cpp +++ b/dhtnew.cpp @@ -193,6 +193,32 @@ int DHTNEW::_read() else // DHT22, DHT33, DHT44, compatible + Si7021 { _humidity = (_bits[0] * 256 + _bits[1]) * 0.1; + // See issue #100 - 2 different representations + if ((_bits[2] & 0x40) == 0x40 ) // negative + { + int16_t t = (_bits[2] << 8) + _bits[3]; // 16 bits, as raw int + _temperature = t * 0.1; + } + else + { + int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]); + if (t == 0) + { + _temperature = 0.0; // prevent -0.0; + } + else + { + if ((_bits[2] & 0x80) == 0x80 ) // negative + { + t = -t; + } + _temperature = t * 0.1; + } + } + } + +/* + // original patch // positive temperature? if ((_bits[2] & 0x80) != 0x80 ) { @@ -218,7 +244,7 @@ int DHTNEW::_read() } } } - +*/ // HEXDUMP DEBUG /* From 4be16b7d1d4d45b561fd06ec20b1c17baaa33f1f Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Sat, 23 Nov 2024 09:32:20 +0100 Subject: [PATCH 3/3] smaller footprint --- README.md | 9 ++++++--- dhtnew.cpp | 33 ++++----------------------------- 2 files changed, 10 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 75b872a..2c714a5 100644 --- a/README.md +++ b/README.md @@ -60,8 +60,11 @@ Feedback (both positive and negative) about the AM232X sensors is welcome. ### 0.5.0 Negative temperature Apparently there are DHT22's which use another representation for negative temperatures. -Since 0.5.0 the library automatically detects this and chooses the right algorithm. -See issue #100. +Since 0.5.0 the library automatically detects which representation is used by the sensor +and chooses the correct algorithm to decode the negative temperature. + +See issue #100 (solution) and #57 and #52 before. +See also https://arduino.stackexchange.com/questions/86448/dht22-sensor-reading-code-interprets-negative-values-weirdly ### Related @@ -70,7 +73,7 @@ See issue #100. - https://github.com/RobTillaart/DHTStable - https://github.com/RobTillaart/DHT_Simulator - https://www.kandrsmith.org/RJS/Misc/Hygrometers/calib_many.html (interesting) -- https://github.com/RobTillaart/Temperature (conversions, dewPoint, heatindex etc) +- https://github.com/RobTillaart/Temperature (conversions, dewPoint, heat index etc.) ## DHT PIN layout from left to right diff --git a/dhtnew.cpp b/dhtnew.cpp index dfce078..de65f56 100644 --- a/dhtnew.cpp +++ b/dhtnew.cpp @@ -193,34 +193,9 @@ int DHTNEW::_read() else // DHT22, DHT33, DHT44, compatible + Si7021 { _humidity = (_bits[0] * 256 + _bits[1]) * 0.1; - // See issue #100 - 2 different representations - if ((_bits[2] & 0x40) == 0x40 ) // negative - { - int16_t t = (_bits[2] << 8) + _bits[3]; // 16 bits, as raw int - _temperature = t * 0.1; - } - else - { - int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]); - if (t == 0) - { - _temperature = 0.0; // prevent -0.0; - } - else - { - if ((_bits[2] & 0x80) == 0x80 ) // negative - { - t = -t; - } - _temperature = t * 0.1; - } - } - } - -/* - // original patch + // positive temperature? - if ((_bits[2] & 0x80) != 0x80 ) + if ((_bits[2] & 0x80) != 0x80 ) { int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]); if (t == 0) @@ -237,14 +212,14 @@ int DHTNEW::_read() int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]); _temperature = t * -0.1; } - else + else { int16_t t = (_bits[2] << 8) + _bits[3]; // 16 bits, as raw int _temperature = t * 0.1; } } } -*/ + // HEXDUMP DEBUG /*