From de6578e49a09dce2225745c57b05f8865c874864 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Wed, 25 Oct 2023 13:35:13 +0200 Subject: [PATCH] update readme.md (#14) - update readme.md - create changelog.md - minor edits --- CHANGELOG.md | 87 ++++++++++++++++++++++++++ README.md | 96 +++++++++++++++++++--------- dht.cpp | 152 ++++++++++++++++----------------------------- dht.h | 26 ++++---- keywords.txt | 2 +- library.json | 4 +- library.properties | 2 +- 7 files changed, 224 insertions(+), 145 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..049150e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,87 @@ +# Change Log DHTlib + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + + +## [0.1.36] - 2023-10-25 +- update readme.md +- create changelog.md +- minor edits + + +# [0.1.35] - 2021-12-16 +- update library.json, license, #pragma once, +- move private defines to .cpp file + +# [0.1.34] - 2021-11-13 +- fix negative temperature DHT22 again (code from DHTNew) + +# [0.1.33] - 2021-02-16 +- fix #6 T-GO signal in handshake. (needed for long wires) + +# [0.1.32] - 2021-02-01 +- fix negative temperature DHT22 again (code from DHTNew) + +# [0.1.31] - 2020-12-15 +- fix negative temperature DHT22 (code from DHTNew) + +# [0.1.30] - 2020-06-30 +- own repo; + +# [0.1.29] - 2018-09-02 +- fix negative temperature DHT12 - issue #111 + +# [0.1.28] - 2018-04-03 +- refactor + +# [0.1.27] - 2018-03-26 +- added _disableIRQ flag + +# [0.1.26] - 2017-12-12 +- explicit support for AM23XX series and DHT12 + +# [0.1.25] - 2017-09-20 +- FIX https://github.com/RobTillaart/Arduino/issues/80 + +# [0.1.24] - 2017-07-27 +- FIX https://github.com/RobTillaart/Arduino/issues/33 double -> float + +# [0.1.23] - 2017-07-24 +- FIX https://github.com/RobTillaart/Arduino/issues/31 + + +---- + +## Older + +// 0.1.22 undo delayMicroseconds() for wakeups larger than 8 +// 0.1.21 replace delay with delayMicroseconds() + small fix +// 0.1.20 Reduce footprint by using uint8_t as error codes. (thanks to chaveiro) +// 0.1.19 masking error for DHT11 - FIXED (thanks Richard for noticing) +// 0.1.18 version 1.16/17 broke the DHT11 - FIXED +// 0.1.17 replaced micros() with adaptive loopcount +// removed DHTLIB_INVALID_VALUE +// added DHTLIB_ERROR_CONNECT +// added DHTLIB_ERROR_ACK_L DHTLIB_ERROR_ACK_H +// 0.1.16 masking unused bits (less errors); refactored bits[] +// 0.1.15 reduced # micros calls 2->1 in inner loop. +// 0.1.14 replace digital read with faster (~3x) code => more robust low MHz machines. +// +// 0.1.13 fix negative temperature +// 0.1.12 support DHT33 and DHT44 initial version +// 0.1.11 renamed DHTLIB_TIMEOUT +// 0.1.10 optimized faster WAKEUP + TIMEOUT +// 0.1.09 optimize size: timeout check + use of mask +// 0.1.08 added formula for timeout based upon clockspeed +// 0.1.07 added support for DHT21 +// 0.1.06 minimize footprint (2012-12-27) +// 0.1.05 fixed negative temperature bug (thanks to Roseman) +// 0.1.04 improved readability of code using DHTLIB_OK in code +// 0.1.03 added error values for temp and humidity when read failed +// 0.1.02 added error codes +// 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011) +// 0.1.00 initial version by Rob Tillaart (01/04/2011) + diff --git a/README.md b/README.md index 2c9a3d8..6175a60 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,11 @@ [![Arduino CI](https://github.com/RobTillaart/DHTlib/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) [![Arduino-lint](https://github.com/RobTillaart/DHTlib/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/DHTlib/actions/workflows/arduino-lint.yml) [![JSON check](https://github.com/RobTillaart/DHTlib/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/DHTlib/actions/workflows/jsoncheck.yml) +[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/DHTlib.svg)](https://github.com/RobTillaart/DHTlib/issues) + [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/DHTlib/blob/master/LICENSE) [![GitHub release](https://img.shields.io/github/release/RobTillaart/DHTlib.svg?maxAge=3600)](https://github.com/RobTillaart/DHTlib/releases) +[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/DHTlib.svg)](https://registry.platformio.org/libraries/robtillaart/DHTlib) # DHTlib @@ -46,48 +49,81 @@ For latest version for the DHT, check https://github.com/RobTillaart/DHTNEW | pin 4 | | GND | +## Interface + +```cpp +#include "dht.h" +``` + + +#### Constructor + +- **dht()** Constructor + + +#### Read functions per type + +- **int8_t read11(uint8_t pin)** +- **int8_t read(uint8_t pin)** +- **int8_t read12(uint8_t pin)** + +- **inline int8_t read21(uint8_t pin)** +- **inline int8_t read22(uint8_t pin)** +- **inline int8_t read33(uint8_t pin)** +- **inline int8_t read44(uint8_t pin)** +- **inline int8_t read2301(uint8_t pin)** +- **inline int8_t read2302(uint8_t pin)** +- **inline int8_t read2303(uint8_t pin)** +- **inline int8_t read2320(uint8_t pin)** +- **inline int8_t read2322(uint8_t pin)** + +- **float humidity** public class variable to access last read humidity. +- **float temperature** public class variable to access last read temperature. + + +#### Enable / disable interrupts + +- **bool getDisableIRQ()** +- **void setDisableIRQ(bool b )** + ## Operational See examples -## Versions: - -==> See DHTStable library for latest stable version for all platforms. - -- version 0.1.13 is the last stable version for both AVR and ARM -- version 0.1.14 and up are not compatible any more with pre 1.0 Arduino. -These have breaking changes wrt ARM based Arduino's e.g DUE. -- version 0.1.15 is stable version for AVR only -- version 0.1.16 and 0.1.17 have breaking changes for DHT11 -- version 0.1.18 works again for DHT11 (AVR only) -- version 0.1.19 fixed masking bug DHT11 (AVR only) -- version 0.1.20 Reduce footprint (34 bytes) by using int8_t as error codes. (thanks to chaveiro) -- version 0.1.21 replace delay with delayMicroseconds() + small fix (thanks to codingforfun) -- version 0.1.22 undo delayMicroseconds() for wakeups larger than 8 -- version 0.1.23 fix https://github.com/RobTillaart/Arduino/issues/31 -- version 0.1.24 fix https://github.com/RobTillaart/Arduino/issues/33 -- version 0.1.25 fix https://github.com/RobTillaart/Arduino/issues/80 -- version 0.1.26 Explicit support for AM23XX series and DHT12 -- version 0.1.27 added \_disableIRQ flag -- version 0.1.28 refactor -- version 0.1.29 fix negative temperature DHT12 - issue #111 -- version 0.1.30 own repo version -- version 0.1.31 fix negative temperature DHT22 (code from DHTNew) -- version 0.1.32 fix negative temperature DHT22 again (code from DHTNew) -- version 0.1.33 fix #6 T-GO signal in handshake. (needed for long wires) -- version 0.1.34 fix negative temperature DHT22 again (code from DHTNew) -- version 0.1.35 update license, library.json, #pragma once, minor edits +## Versions + +- See changelog.md +- See DHTNEW or DHTStable library for latest stable version for all platforms. ## Future -- updates only on request basis (bugs) +#### Must + - improve documentation + +#### Should + +- separate history versions CHANGELOG.md file ? + +#### Could + - improve unit tests - constants - move non public part of #defines to .cpp -- separate history versions releasenotes file ? -- + +#### Wont + +- updates only on request basis (bugs) + + +## Support + +If you appreciate my libraries, you can support the development and maintenance. +Improve the quality of the libraries by providing issues and Pull Requests, or +donate through PayPal or GitHub sponsors. + +Thank you, diff --git a/dht.cpp b/dht.cpp index b76fda7..64f0b08 100644 --- a/dht.cpp +++ b/dht.cpp @@ -1,56 +1,12 @@ // // FILE: dht.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.35 +// VERSION: 0.1.36 // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino, AVR optimized // URL: https://github.com/RobTillaart/DHTlib // http://arduino.cc/playground/Main/DHTLib // -// -// HISTORY: -// 0.1.35 2021-12-16 update library.json, license, #pragma once, -// move private defines to .cpp file -// 0.1.34 2021-11-13 fix negative temperature DHT22 again (code from DHTNew) -// 0.1.33 2021-02-16 fix #6 T-GO signal in handshake. (needed for long wires) -// 0.1.32 2021-02-01 fix negative temperature DHT22 again (code from DHTNew) -// 0.1.31 2020-12-15 fix negative temperature DHT22 (code from DHTNew) -// 0.1.30 2020-06-30 own repo; -// 0.1.29 2018-09-02 fix negative temperature DHT12 - issue #111 -// 0.1.28 2018-04-03 refactor -// 0.1.27 2018-03-26 added _disableIRQ flag -// 0.1.26 2017-12-12 explicit support for AM23XX series and DHT12 -// 0.1.25 2017-09-20 FIX https://github.com/RobTillaart/Arduino/issues/80 -// 0.1.24 2017-07-27 FIX https://github.com/RobTillaart/Arduino/issues/33 double -> float -// 0.1.23 2017-07-24 FIX https://github.com/RobTillaart/Arduino/issues/31 -// 0.1.22 undo delayMicroseconds() for wakeups larger than 8 -// 0.1.21 replace delay with delayMicroseconds() + small fix -// 0.1.20 Reduce footprint by using uint8_t as error codes. (thanks to chaveiro) -// 0.1.19 masking error for DHT11 - FIXED (thanks Richard for noticing) -// 0.1.18 version 1.16/17 broke the DHT11 - FIXED -// 0.1.17 replaced micros() with adaptive loopcount -// removed DHTLIB_INVALID_VALUE -// added DHTLIB_ERROR_CONNECT -// added DHTLIB_ERROR_ACK_L DHTLIB_ERROR_ACK_H -// 0.1.16 masking unused bits (less errors); refactored bits[] -// 0.1.15 reduced # micros calls 2->1 in inner loop. -// 0.1.14 replace digital read with faster (~3x) code => more robust low MHz machines. -// -// 0.1.13 fix negative temperature -// 0.1.12 support DHT33 and DHT44 initial version -// 0.1.11 renamed DHTLIB_TIMEOUT -// 0.1.10 optimized faster WAKEUP + TIMEOUT -// 0.1.09 optimize size: timeout check + use of mask -// 0.1.08 added formula for timeout based upon clockspeed -// 0.1.07 added support for DHT21 -// 0.1.06 minimize footprint (2012-12-27) -// 0.1.05 fixed negative temperature bug (thanks to Roseman) -// 0.1.04 improved readability of code using DHTLIB_OK in code -// 0.1.03 added error values for temp and humidity when read failed -// 0.1.02 added error codes -// 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011) -// 0.1.00 by Rob Tillaart (01/04/2011) -// -// inspired by DHT11 library +// inspired by DHT11 library #include "dht.h" @@ -63,13 +19,13 @@ #define DHTLIB_DHT_LEADING_ZEROS 6 -// max timeout is 100 microseconds. -// For a 16 MHz processor 100 microseconds is 1600 clock cycles -// loops using DHTLIB_TIMEOUT use at least 4 clock cycles -// so 100 us takes max 400 loops -// so by dividing F_CPU by 40000 we "fail" as fast as possible +// max timeout is 100 microseconds. +// For a 16 MHz processor 100 microseconds is 1600 clock cycles +// loops using DHTLIB_TIMEOUT use at least 4 clock cycles +// so 100 us takes max 400 loops +// so by dividing F_CPU by 40000 we "fail" as fast as possible #ifndef F_CPU -#define DHTLIB_TIMEOUT 1000 // should be approx. clock/40000 +#define DHTLIB_TIMEOUT 1000 // should be approx. clock/40000 #else #define DHTLIB_TIMEOUT (F_CPU/40000) #endif @@ -77,25 +33,25 @@ ///////////////////////////////////////////////////// // -// PUBLIC +// PUBLIC // int8_t dht::read11(uint8_t pin) { - // READ VALUES + // READ VALUES if (_disableIRQ) noInterrupts(); int8_t result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS); if (_disableIRQ) interrupts(); - // these bits are always zero, masking them reduces errors. + // these bits are always zero, masking them reduces errors. bits[0] &= 0x7F; bits[2] &= 0x7F; - // CONVERT AND STORE - humidity = bits[0]; // bits[1] == 0; - temperature = bits[2]; // bits[3] == 0; + // CONVERT AND STORE + humidity = bits[0]; // bits[1] == 0; + temperature = bits[2]; // bits[3] == 0; - // TEST CHECKSUM + // TEST CHECKSUM uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3]; if (bits[4] != sum) { @@ -107,20 +63,20 @@ int8_t dht::read11(uint8_t pin) int8_t dht::read12(uint8_t pin) { - // READ VALUES + // READ VALUES if (_disableIRQ) noInterrupts(); int8_t result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS); if (_disableIRQ) interrupts(); - // CONVERT AND STORE + // CONVERT AND STORE humidity = bits[0] + bits[1] * 0.1; temperature = bits[2] + (bits[3] & 0x7F) * 0.1; - if (bits[3] & 0x80) // negative temperature + if (bits[3] & 0x80) // negative temperature { temperature = -temperature; } - // TEST CHECKSUM + // TEST CHECKSUM uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3]; if (bits[4] != sum) { @@ -132,21 +88,21 @@ int8_t dht::read12(uint8_t pin) int8_t dht::read(uint8_t pin) { - // READ VALUES + // READ VALUES if (_disableIRQ) noInterrupts(); int8_t result = _readSensor(pin, DHTLIB_DHT_WAKEUP, DHTLIB_DHT_LEADING_ZEROS); if (_disableIRQ) interrupts(); - // these bits are always zero, masking them reduces errors. + // these bits are always zero, masking them reduces errors. bits[0] &= 0x03; bits[2] &= 0x83; - // CONVERT AND STORE + // CONVERT AND STORE humidity = (bits[0] * 256 + bits[1]) * 0.1; int16_t t = ((bits[2] & 0x7F) * 256 + bits[3]); if (t == 0) { - temperature = 0.0; // prevent -0.0; + temperature = 0.0; // prevent -0.0; } else { @@ -157,7 +113,7 @@ int8_t dht::read(uint8_t pin) } } - // HEXDUMP DEBUG + // HEXDUMP DEBUG /* Serial.println(); // CHECKSUM @@ -181,7 +137,7 @@ int8_t dht::read(uint8_t pin) Serial.print(humidity, 1); */ - // TEST CHECKSUM + // TEST CHECKSUM uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3]; if (bits[4] != sum) { @@ -193,11 +149,11 @@ int8_t dht::read(uint8_t pin) ///////////////////////////////////////////////////// // -// PRIVATE +// PRIVATE // int8_t dht::_readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBits) { - // INIT BUFFERVAR TO RECEIVE DATA + // INIT BUFFERVAR TO RECEIVE DATA uint8_t mask = 128; uint8_t idx = 0; @@ -207,47 +163,47 @@ int8_t dht::_readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBit uint16_t zeroLoop = DHTLIB_TIMEOUT; uint16_t delta = 0; - leadingZeroBits = 40 - leadingZeroBits; // reverse counting... + leadingZeroBits = 40 - leadingZeroBits; // reverse counting... - // replace digitalRead() with Direct Port Reads. - // reduces footprint ~100 bytes => portability issue? - // direct port read is about 3x faster + // replace digitalRead() with Direct Port Reads. + // reduces footprint ~100 bytes => portability issue? + // direct port read is about 3x faster uint8_t bit = digitalPinToBitMask(pin); uint8_t port = digitalPinToPort(pin); volatile uint8_t *PIR = portInputRegister(port); - // REQUEST SAMPLE + // REQUEST SAMPLE pinMode(pin, OUTPUT); - digitalWrite(pin, LOW); // T-be + digitalWrite(pin, LOW); // T-be if (wakeupDelay > 8) delay(wakeupDelay); else delayMicroseconds(wakeupDelay * 1000UL); - digitalWrite(pin, HIGH); // T-go + digitalWrite(pin, HIGH); // T-go pinMode(pin, INPUT); - uint16_t loopCount = DHTLIB_TIMEOUT * 2; // 200uSec max - // while(digitalRead(pin) == HIGH) + uint16_t loopCount = DHTLIB_TIMEOUT * 2; // 200 uSec max + // while(digitalRead(pin) == HIGH) while ((*PIR & bit) != LOW ) { - if (--loopCount == 0) + if (--loopCount == 0) { return DHTLIB_ERROR_CONNECT; } } - // GET ACKNOWLEDGE or TIMEOUT + // GET ACKNOWLEDGE or TIMEOUT loopCount = DHTLIB_TIMEOUT; - // while(digitalRead(pin) == LOW) - while ((*PIR & bit) == LOW ) // T-rel + // while(digitalRead(pin) == LOW) + while ((*PIR & bit) == LOW ) // T-rel { - if (--loopCount == 0) + if (--loopCount == 0) { return DHTLIB_ERROR_ACK_L; } } loopCount = DHTLIB_TIMEOUT; - // while(digitalRead(pin) == HIGH) - while ((*PIR & bit) != LOW ) // T-reh + // while(digitalRead(pin) == HIGH) + while ((*PIR & bit) != LOW ) // T-reh { if (--loopCount == 0) { @@ -257,49 +213,49 @@ int8_t dht::_readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBit loopCount = DHTLIB_TIMEOUT; - // READ THE OUTPUT - 40 BITS => 5 BYTES + // READ THE OUTPUT - 40 BITS => 5 BYTES for (uint8_t i = 40; i != 0; ) { - // WAIT FOR FALLING EDGE + // WAIT FOR FALLING EDGE state = (*PIR & bit); if (state == LOW && pstate != LOW) { - if (i > leadingZeroBits) // DHT22 first 6 bits are all zero !! DHT11 only 1 + if (i > leadingZeroBits) // DHT22 first 6 bits are all zero !! DHT11 only 1 { zeroLoop = min(zeroLoop, loopCount); delta = (DHTLIB_TIMEOUT - zeroLoop)/4; } - else if ( loopCount <= (zeroLoop - delta) ) // long -> one + else if ( loopCount <= (zeroLoop - delta) ) // long -> one { data |= mask; } mask >>= 1; - if (mask == 0) // next byte + if (mask == 0) // next byte { mask = 128; bits[idx] = data; idx++; data = 0; } - // next bit + // next bit --i; - // reset timeout flag + // reset timeout flag loopCount = DHTLIB_TIMEOUT; } pstate = state; - // Check timeout + // Check timeout if (--loopCount == 0) { return DHTLIB_ERROR_TIMEOUT; } } - // pinMode(pin, OUTPUT); - // digitalWrite(pin, HIGH); + // pinMode(pin, OUTPUT); + // digitalWrite(pin, HIGH); return DHTLIB_OK; } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/dht.h b/dht.h index 2094f04..51281a3 100644 --- a/dht.h +++ b/dht.h @@ -2,7 +2,7 @@ // // FILE: dht.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.35 +// VERSION: 0.1.36 // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino. AVR optimized // URL: https://github.com/RobTillaart/DHTlib // http://arduino.cc/playground/Main/DHTLib @@ -10,13 +10,13 @@ #if ARDUINO < 100 #include -#include // fix for broken pre 1.0 version - TODO TEST +#include // fix for broken pre 1.0 version - TODO TEST #else #include #endif -#define DHT_LIB_VERSION (F("0.1.35")) +#define DHT_LIB_VERSION (F("0.1.36")) #define DHTLIB_OK 0 #define DHTLIB_ERROR_CHECKSUM -1 @@ -30,13 +30,13 @@ class dht { public: dht() { _disableIRQ = false; }; - // return values: - // DHTLIB_OK - // DHTLIB_ERROR_CHECKSUM - // DHTLIB_ERROR_TIMEOUT - // DHTLIB_ERROR_CONNECT - // DHTLIB_ERROR_ACK_L - // DHTLIB_ERROR_ACK_H + // return values: + // DHTLIB_OK + // DHTLIB_ERROR_CHECKSUM + // DHTLIB_ERROR_TIMEOUT + // DHTLIB_ERROR_CONNECT + // DHTLIB_ERROR_ACK_L + // DHTLIB_ERROR_ACK_H int8_t read11(uint8_t pin); int8_t read(uint8_t pin); int8_t read12(uint8_t pin); @@ -50,7 +50,7 @@ class dht inline int8_t read2303(uint8_t pin) { return read(pin); }; inline int8_t read2320(uint8_t pin) { return read(pin); }; inline int8_t read2322(uint8_t pin) { return read(pin); }; - + bool getDisableIRQ() { return _disableIRQ; }; void setDisableIRQ(bool b ) { _disableIRQ = b; }; @@ -58,11 +58,11 @@ class dht float temperature; private: - uint8_t bits[5]; // buffer to receive data + uint8_t bits[5]; // buffer to receive data int8_t _readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBits); bool _disableIRQ; }; -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/keywords.txt b/keywords.txt index a1f3790..d1a5f1e 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,4 +1,4 @@ -# Syntax Colouring Map For DHT +# Syntax Colouring Map For DHTlib # Data types (KEYWORD1) diff --git a/library.json b/library.json index 71c3154..03f6115 100644 --- a/library.json +++ b/library.json @@ -15,9 +15,9 @@ "type": "git", "url": "https://github.com/RobTillaart/DHTlib.git" }, - "version": "0.1.35", + "version": "0.1.36", "license": "MIT", - "frameworks": "arduino", + "frameworks": "*", "platforms": "*", "headers": "dht.h" } diff --git a/library.properties b/library.properties index bec98c5..2f8fb14 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=DHTlib -version=0.1.35 +version=0.1.36 author=Rob Tillaart maintainer=Rob Tillaart sentence=AVR Optimized Library for DHT Temperature & Humidity Sensor on AVR only.