From a8ebdcbdccdddf26684b26acd281f34f9875b99b Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Sun, 24 Mar 2024 11:55:13 +0100 Subject: [PATCH 1/4] update GitHub actions --- .github/FUNDING.yml | 3 +- .github/workflows/arduino-lint.yml | 6 +- .github/workflows/arduino_test_runner.yml | 4 +- .github/workflows/jsoncheck.yml | 8 +-- CHANGELOG.md | 8 ++- LICENSE | 2 +- README.md | 53 ++++++++++------- dhtnew.cpp | 2 +- dhtnew.h | 4 +- .../dhtnew_adaptive_delay.ino | 42 +++++++------- examples/dhtnew_array/dhtnew_array.ino | 6 +- examples/dhtnew_debug/dhtnew_debug.ino | 24 ++++---- examples/dhtnew_endless/dhtnew_endless.ino | 28 ++++----- .../dhtnew_endless_insideFunction.ino | 26 ++++----- examples/dhtnew_minimum/dhtnew_minimum.ino | 24 ++++---- .../dhtnew_minimum_insideFunction.ino | 22 +++---- .../dhtnew_powerDown/dhtnew_powerDown.ino | 32 +++++----- .../dhtnew_pulse_diag/dhtnew_pulse_diag.ino | 58 +++++++++---------- examples/dhtnew_runtime/dhtnew_runtime.ino | 26 ++++----- .../dhtnew_setReadDelay.ino | 32 +++++----- .../dhtnew_suppressError.ino | 16 ++--- examples/dhtnew_test/dhtnew_test.ino | 28 ++++----- .../dhtnew_waitForRead/dhtnew_waitForRead.ino | 30 +++++----- .../dhtnew_waitForRead_nonBlocking.ino | 30 +++++----- library.json | 2 +- library.properties | 2 +- 26 files changed, 268 insertions(+), 250 deletions(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 90d9ab4..da1c857 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,4 +1,5 @@ # These are supported funding model platforms -github: RobTillaart +GitHub: RobTillaart +custom: "https://www.paypal.me/robtillaart" diff --git a/.github/workflows/arduino-lint.yml b/.github/workflows/arduino-lint.yml index 8a26f14..7f8f4ef 100644 --- a/.github/workflows/arduino-lint.yml +++ b/.github/workflows/arduino-lint.yml @@ -1,13 +1,13 @@ - name: Arduino-lint on: [push, pull_request] jobs: lint: runs-on: ubuntu-latest + timeout-minutes: 5 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: arduino/arduino-lint-action@v1 with: library-manager: update - compliance: strict + compliance: strict \ No newline at end of file diff --git a/.github/workflows/arduino_test_runner.yml b/.github/workflows/arduino_test_runner.yml index fadfa90..393c225 100644 --- a/.github/workflows/arduino_test_runner.yml +++ b/.github/workflows/arduino_test_runner.yml @@ -6,12 +6,14 @@ on: [push, pull_request] jobs: runTest: runs-on: ubuntu-latest + timeout-minutes: 20 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: 2.6 - run: | + sudo sysctl vm.mmap_rnd_bits=28 gem install arduino_ci arduino_ci.rb diff --git a/.github/workflows/jsoncheck.yml b/.github/workflows/jsoncheck.yml index 37a1129..1cbb5e2 100644 --- a/.github/workflows/jsoncheck.yml +++ b/.github/workflows/jsoncheck.yml @@ -9,10 +9,10 @@ on: jobs: test: runs-on: ubuntu-latest + timeout-minutes: 5 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: json-syntax-check - uses: limitusus/json-syntax-check@v1 + uses: limitusus/json-syntax-check@v2 with: - pattern: "\\.json$" - + pattern: "\\.json$" \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 2998d38..cf1c34e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [0.4.19] - 2023-10-25 +## [0.4.19] - 2024-03-24 +- Fix #95, units of T & H +- update GitHub actions - update readme.md +- minor edits + +## [0.4.19] - 2023-10-25 +- update readme.md ## [0.4.18] - 2023-01-09 - update license to 2023 diff --git a/LICENSE b/LICENSE index 562b0ff..a435724 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2017-2023 Rob Tillaart +Copyright (c) 2017-2024 Rob Tillaart Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 8957ded..acf5fdd 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ This is the main development library of all my DHT libraries. Supports DHT11, DHT22, DHT33, DHT44, AM2301, AM2302, AM2303 as these all have the same protocol. Note there are differences e.g. DHT11 has no negative temperature, no decimals, and a longer wakeup time. +The DHTNew library returns Temperature in degrees Celsius and Humidity in 0.0 - 100.0%. + #### Sonoff Si7021 @@ -56,36 +58,38 @@ Feedback (both positive and negative) about the AM232X sensors is welcome. - https://github.com/RobTillaart/DHTNew - 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) ## DHT PIN layout from left to right -| Front | | Description | -|:------|:----:|:--------------| -| pin 1 | | VCC | -| pin 2 | | DATA | -| pin 3 | | Not Connected | -| pin 4 | | GND | +| Front | | Description | +|:--------|:----:|:----------------| +| pin 1 | | VCC | +| pin 2 | | DATA | +| pin 3 | | Not Connected | +| pin 4 | | GND | **Note: check the datasheet how to connect!** ## Specification DHT22 -| Model | DHT22 | Notes | -|:--------------------------|:-----------------------|:------| -| Power supply | 3.3 - 6 V DC | -| Output signal | digital signal via single-bus | -| Sensing element | Polymer capacitor | -| Operating range | humidity 0-100% RH | temperature -40° - 80° Celsius -| Accuracy humidity | ±2% RH(Max ±5% RH) | temperature < ±0.5° Celsius -| Resolution or sensitivity | humidity 0.1% RH | temperature 0.1° Celsius -| Repeatability humidity | ±1% RH | temperature ±0.2° Celsius -| Humidity hysteresis | ±0.3% RH | -| Long-term Stability | ±0.5% RH/year | -| Sensing period | Average: 2s | -| Interchangeability | fully interchangeable | -| Dimensions | small 14 x 18 x 5.5 mm | big 22 x 28 x 5 mm | +| Model | DHT22 | Notes | +|:----------------------------|:-------------------------|:--------| +| Power supply | 3.3 - 6 V DC | +| Output signal | digital signal via single-bus | +| Sensing element | Polymer capacitor | +| Operating range | humidity 0-100% RH | temperature -40° - 80° Celsius +| Accuracy humidity | ±2% RH(Max ±5% RH) | temperature < ±0.5° Celsius +| Resolution or sensitivity | humidity 0.1% RH | temperature 0.1° Celsius +| Repeatability humidity | ±1% RH | temperature ±0.2° Celsius +| Humidity hysteresis | ±0.3% RH | +| Long-term Stability | ±0.5% RH/year | +| Sensing period | Average: 2s | +| Interchangeability | fully interchangeable | +| Dimensions | small 14 x 18 x 5.5 mm | big 22 x 28 x 5 mm | ## Interface @@ -116,9 +120,12 @@ Since 0.4.14 type 70 is added for **experimental** Sonoff Si7021 support. - **int read()** reads a new temperature and humidity from the sensor - **uint32_t lastRead()** returns milliseconds since last **read()** -- **float getHumidity()** returns last read value (float) or -999 in case of error. +- **float getHumidity()** returns last read humidity = 0.0-100.0 %. +In case of an error it returns **DHTLIB_INVALID_VALUE** == -999. Note this error value can be suppressed by **setSuppressError(bool)**. -- **float getTemperature()** returns last read value (float) or -999 in case of error. +- **float getTemperature()** returns last read temperature (float) in Celsius. +Range depends on the sensor. +In case of an error it returns **DHTLIB_INVALID_VALUE** == -999. Note this error value can be suppressed by **setSuppressError(bool)**. @@ -298,6 +305,8 @@ fix #86, define constants explicit as float. Update readme.md and library.\* about support for AM2320/21/22. 35. (0.4.19) Update readme.md +36. (0.4.20) +Update GitHub actions and readme.md ## Future diff --git a/dhtnew.cpp b/dhtnew.cpp index 681cc10..33bee9f 100644 --- a/dhtnew.cpp +++ b/dhtnew.cpp @@ -1,7 +1,7 @@ // // FILE: dhtnew.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.4.19 +// VERSION: 0.4.20 // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino // URL: https://github.com/RobTillaart/DHTNEW // diff --git a/dhtnew.h b/dhtnew.h index fcc91c6..bbaa708 100644 --- a/dhtnew.h +++ b/dhtnew.h @@ -2,7 +2,7 @@ // // FILE: dhtnew.h // AUTHOR: Rob Tillaart -// VERSION: 0.4.19 +// VERSION: 0.4.20 // 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.19")) +#define DHTNEW_LIB_VERSION (F("0.4.20")) #define DHTLIB_OK 0 diff --git a/examples/dhtnew_adaptive_delay/dhtnew_adaptive_delay.ino b/examples/dhtnew_adaptive_delay/dhtnew_adaptive_delay.ino index 8a4c264..0c38325 100644 --- a/examples/dhtnew_adaptive_delay/dhtnew_adaptive_delay.ino +++ b/examples/dhtnew_adaptive_delay/dhtnew_adaptive_delay.ino @@ -6,30 +6,30 @@ // // -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND - -// Note: -// Adaptive delay makes no sense anymore as the DHTNEW lib catches reads that -// are done faster than READ_DELAY apart (see dhtnew.cpp file). -// that said it is the goal to get this adaptability into the library one day. -// A way to do this is to add a function auto_calibrate() that finds the timing -// where reading fails and use that value + safety margin (20%?) +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND + +// Note: +// Adaptive delay makes no sense anymore as the DHTNEW lib catches reads that +// are done faster than READ_DELAY apart (see dhtnew.cpp file). +// that said it is the goal to get this adaptability into the library one day. +// A way to do this is to add a function auto_calibrate() that finds the timing +// where reading fails and use that value + safety margin (20%?) #include -DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 +DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println("dhtnew_adaptive_delay.ino"); @@ -37,8 +37,8 @@ void setup() Serial.println(DHTNEW_LIB_VERSION); Serial.println(); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); Serial.println("\n1. Type detection test, first run might take longer to determine type"); Serial.println("STAT\tHUMI\tTEMP\tTIME\tTYPE"); @@ -97,7 +97,7 @@ void loop() void test() { static uint16_t dht_delay = 600; - // READ DATA + // READ DATA uint32_t start = micros(); int chk = mySensor.read(); uint32_t stop = micros(); @@ -144,7 +144,7 @@ void test() } dht_delay = constrain(dht_delay, 100, 5000); - // DISPLAY DATA + // DISPLAY DATA Serial.print(mySensor.getHumidity(), 1); Serial.print(",\t"); Serial.print(mySensor.getTemperature(), 1); @@ -159,5 +159,5 @@ void test() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_array/dhtnew_array.ino b/examples/dhtnew_array/dhtnew_array.ino index 1fbf131..1694380 100644 --- a/examples/dhtnew_array/dhtnew_array.ino +++ b/examples/dhtnew_array/dhtnew_array.ino @@ -55,7 +55,7 @@ void loop() void test(int idx) { - // READ DATA + // READ DATA uint32_t start = micros(); int chk = ar[idx].read(); uint32_t stop = micros(); @@ -99,7 +99,7 @@ void test(int idx) break; } - // DISPLAY DATA + // DISPLAY DATA Serial.print(ar[idx].getHumidity(), 1); Serial.print(",\t"); Serial.print(ar[idx].getTemperature(), 1); @@ -112,5 +112,5 @@ void test(int idx) } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_debug/dhtnew_debug.ino b/examples/dhtnew_debug/dhtnew_debug.ino index 848910a..261ad49 100644 --- a/examples/dhtnew_debug/dhtnew_debug.ino +++ b/examples/dhtnew_debug/dhtnew_debug.ino @@ -4,18 +4,18 @@ // PURPOSE: DHTNEW library debug sketch for Arduino // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include -DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 +DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 void setup() @@ -26,17 +26,17 @@ void setup() Serial.println(DHTNEW_LIB_VERSION); Serial.println(); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); } void loop() { delay(2000); - mySensor.read(); // put print statements in core lib (see read()) + mySensor.read(); // put print statements in core lib (see read()) } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_endless/dhtnew_endless.ino b/examples/dhtnew_endless/dhtnew_endless.ino index 1157feb..5667550 100644 --- a/examples/dhtnew_endless/dhtnew_endless.ino +++ b/examples/dhtnew_endless/dhtnew_endless.ino @@ -5,18 +5,18 @@ // DATE: 2020-06-04 // (c) : MIT -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include -DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 +DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 uint32_t count = 0; uint32_t start, stop; @@ -26,7 +26,7 @@ uint32_t errors[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println("DHT_endless.ino"); @@ -34,15 +34,15 @@ void setup() Serial.println(DHTNEW_LIB_VERSION); Serial.println(); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); } void loop() { count++; - // show counters for OK and errors. + // show counters for OK and errors. if (count % 50 == 0) { Serial.println(); @@ -115,7 +115,7 @@ void loop() errors[9]++; break; } - // DISPLAY DATA + // DISPLAY DATA Serial.print(mySensor.getHumidity(), 1); Serial.print(",\t"); Serial.print(mySensor.getTemperature(), 1); @@ -128,5 +128,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_endless_insideFunction/dhtnew_endless_insideFunction.ino b/examples/dhtnew_endless_insideFunction/dhtnew_endless_insideFunction.ino index 4181f4b..a7eb61d 100644 --- a/examples/dhtnew_endless_insideFunction/dhtnew_endless_insideFunction.ino +++ b/examples/dhtnew_endless_insideFunction/dhtnew_endless_insideFunction.ino @@ -5,13 +5,13 @@ // DATE: 2021-02-19 // (c) : MIT -// DHT PINs' layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PINs' layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include @@ -25,7 +25,7 @@ uint32_t errors[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println("DHT_endless.ino"); @@ -33,8 +33,8 @@ void setup() Serial.println(DHTNEW_LIB_VERSION); Serial.println(); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); } @@ -47,7 +47,7 @@ void DHTt(const uint8_t pin) DHTNEW mySensor(pin); count++; - // show counters for OK and errors. + // show counters for OK and errors. if (count % 50 == 0) { Serial.println(); @@ -120,7 +120,7 @@ void DHTt(const uint8_t pin) errors[9]++; break; } - // DISPLAY DATA + // DISPLAY DATA Serial.print(mySensor.getHumidity(), 1); Serial.print(",\t"); Serial.print(mySensor.getTemperature(), 1); @@ -138,6 +138,6 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_minimum/dhtnew_minimum.ino b/examples/dhtnew_minimum/dhtnew_minimum.ino index f98677b..6422198 100644 --- a/examples/dhtnew_minimum/dhtnew_minimum.ino +++ b/examples/dhtnew_minimum/dhtnew_minimum.ino @@ -4,30 +4,30 @@ // PURPOSE: DHTNEW library test sketch // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include -DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 +DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println(__FILE__); Serial.println(); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); Serial.println("BEFORE OFFSET"); delay(2000); @@ -55,5 +55,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_minimum_insideFunction/dhtnew_minimum_insideFunction.ino b/examples/dhtnew_minimum_insideFunction/dhtnew_minimum_insideFunction.ino index 58f9738..da0b3b2 100644 --- a/examples/dhtnew_minimum_insideFunction/dhtnew_minimum_insideFunction.ino +++ b/examples/dhtnew_minimum_insideFunction/dhtnew_minimum_insideFunction.ino @@ -4,13 +4,13 @@ // PURPOSE: DHTNEW library test sketch // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include @@ -20,7 +20,7 @@ uint64_t previousMillis; void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println(__FILE__); @@ -32,8 +32,8 @@ void DHTt(uint8_t pin) { DHTNEW mySensor(pin); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); if (millis() - mySensor.lastRead() > 2000) { @@ -52,5 +52,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_powerDown/dhtnew_powerDown.ino b/examples/dhtnew_powerDown/dhtnew_powerDown.ino index 73687f5..285cee6 100644 --- a/examples/dhtnew_powerDown/dhtnew_powerDown.ino +++ b/examples/dhtnew_powerDown/dhtnew_powerDown.ino @@ -4,17 +4,17 @@ // PURPOSE: DHTNEW library test sketch for Arduino // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND -// to see the effect one must apply a voltmeter to the data pin of the sensor -// during the low power mode. Measuring during communication will disrupt the -// data transfer. +// to see the effect one must apply a voltmeter to the data pin of the sensor +// during the low power mode. Measuring during communication will disrupt the +// data transfer. #include @@ -24,15 +24,15 @@ DHTNEW mySensor(16); void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println("dhtnew_test.ino"); Serial.print("LIBRARY VERSION: "); Serial.println(DHTNEW_LIB_VERSION); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); Serial.println("\nstartup"); delay(2000); @@ -43,7 +43,7 @@ void setup() int rv = mySensor.read(); if (rv != DHTLIB_OK) { - Serial.println(rv); // will print -7 when measuring voltage + Serial.println(rv); // will print -7 when measuring voltage } Serial.print(mySensor.getHumidity(), 1); Serial.print(",\t"); @@ -58,7 +58,7 @@ void setup() Serial.println("switch sensor on (and wait 2 seconds)"); mySensor.powerUp(); - // wait for 2 seconds. + // wait for 2 seconds. delay(2000); Serial.println("read sensor with 2 second interval"); @@ -80,5 +80,5 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_pulse_diag/dhtnew_pulse_diag.ino b/examples/dhtnew_pulse_diag/dhtnew_pulse_diag.ino index 25fc141..7b594d1 100644 --- a/examples/dhtnew_pulse_diag/dhtnew_pulse_diag.ino +++ b/examples/dhtnew_pulse_diag/dhtnew_pulse_diag.ino @@ -4,13 +4,13 @@ // PURPOSE: DHTNEW library pulse measurement tool - diagnostics // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include "Arduino.h" @@ -28,13 +28,13 @@ uint8_t idx = 0; void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println("dhtnew_pulse_diag.ino"); Serial.println(); - // default should be HIGH + // default should be HIGH pinMode(_dataPin, OUTPUT); digitalWrite(_dataPin, HIGH); } @@ -104,61 +104,61 @@ void dump() void measure() { count++; - // yield(); // handle pending interrupts + // yield(); // handle pending interrupts - // reset measurements table + // reset measurements table idx = 0; t = micros(); for (int i = 0; i < 100; i++) times[i] = 0; times[idx++] = micros(); - // REQUEST SAMPLE - SEND WAKEUP TO SENSOR + // REQUEST SAMPLE - SEND WAKEUP TO SENSOR pinMode(_dataPin, OUTPUT); digitalWrite(_dataPin, LOW); - // add 10% extra for timing inaccuracies in sensor. + // add 10% extra for timing inaccuracies in sensor. delayMicroseconds(_wakeupDelay * 1100UL); Serial.print("awake "); times[idx++] = micros(); - // HOST GIVES CONTROL TO SENSOR + // HOST GIVES CONTROL TO SENSOR pinMode(_dataPin, INPUT_PULLUP); - // DISABLE INTERRUPTS when clock in the bits - // noInterrupts(); // gives problems on AVR + // DISABLE INTERRUPTS when clock in the bits + // noInterrupts(); // gives problems on AVR times[idx++] = micros(); - // SENSOR PULLS LOW after 20-40 us => if stays HIGH ==> device not ready + // SENSOR PULLS LOW after 20-40 us => if stays HIGH ==> device not ready while (digitalRead(_dataPin) == HIGH); Serial.print("2 "); times[idx++] = micros(); - // SENSOR STAYS LOW for ~80 us => or TIMEOUT + // SENSOR STAYS LOW for ~80 us => or TIMEOUT while (digitalRead(_dataPin) == LOW); Serial.print("3 "); times[idx++] = micros(); - // SENSOR STAYS HIGH for ~80 us => or TIMEOUT + // SENSOR STAYS HIGH for ~80 us => or TIMEOUT while (digitalRead(_dataPin) == HIGH); times[idx++] = micros(); Serial.print("4 "); - // SENSOR HAS NOW SEND ACKNOWLEDGE ON WAKEUP - // NOW IT SENDS THE BITS + // SENSOR HAS NOW SEND ACKNOWLEDGE ON WAKEUP + // NOW IT SENDS THE BITS - // READ THE OUTPUT - 40 BITS => 5 BYTES + // READ THE OUTPUT - 40 BITS => 5 BYTES uint32_t start = micros(); uint8_t i = 40; for (i = 40; i != 0; i--) { times[idx++] = micros(); - // EACH BIT START WITH ~50 us LOW + // EACH BIT START WITH ~50 us LOW while (digitalRead(_dataPin) == LOW) { if (micros() - start > 10000) @@ -171,9 +171,9 @@ void measure() } times[idx++] = micros(); - // DURATION OF HIGH DETERMINES 0 or 1 - // 26-28 us ==> 0 - // 70 us ==> 1 + // DURATION OF HIGH DETERMINES 0 or 1 + // 26-28 us ==> 0 + // 70 us ==> 1 while (digitalRead(_dataPin) == HIGH) { if (micros() - start > 10000) @@ -187,8 +187,8 @@ void measure() Serial.print("5 "); times[idx++] = micros(); - // After 40 bits the sensor pulls the line LOW for 50 us - // TODO: should we wait? + // After 40 bits the sensor pulls the line LOW for 50 us + // TODO: should we wait? while (digitalRead(_dataPin) == LOW); Serial.print("6 "); @@ -206,4 +206,4 @@ void measure() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_runtime/dhtnew_runtime.ino b/examples/dhtnew_runtime/dhtnew_runtime.ino index f9a44e9..e941649 100644 --- a/examples/dhtnew_runtime/dhtnew_runtime.ino +++ b/examples/dhtnew_runtime/dhtnew_runtime.ino @@ -4,33 +4,33 @@ // PURPOSE: DHTNEW library test sketch // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include -DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 +DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 uint32_t lastTime = 0; void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println(__FILE__); Serial.print("DHTNEW_LIB_VERSION: "); Serial.println(DHTNEW_LIB_VERSION); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); } @@ -52,9 +52,9 @@ void loop() } } - // Do other things here + // Do other things here } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_setReadDelay/dhtnew_setReadDelay.ino b/examples/dhtnew_setReadDelay/dhtnew_setReadDelay.ino index 1888ad6..24e2a1d 100644 --- a/examples/dhtnew_setReadDelay/dhtnew_setReadDelay.ino +++ b/examples/dhtnew_setReadDelay/dhtnew_setReadDelay.ino @@ -4,23 +4,23 @@ // PURPOSE: DHTNEW library waitForRead example sketch for Arduino // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include -DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 +DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println(__FILE__); @@ -28,10 +28,10 @@ void setup() Serial.println(DHTNEW_LIB_VERSION); Serial.println(); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); - delay(2000); // boot time + delay(2000); // boot time mySensor.setWaitForReading(true); @@ -60,7 +60,7 @@ void setup() } } - // safety margin of 100 uSec + // safety margin of 100 uSec rd += 100; mySensor.setReadDelay(rd); Serial.print("\nreadDelay set to (ms) : "); @@ -71,8 +71,8 @@ void setup() void loop() { - // Note: the library prevents reads faster than readDelay... - // it will return previous values for T & H + // Note: the library prevents reads faster than readDelay... + // it will return previous values for T & H int chk = mySensor.read(); Serial.print(millis()); Serial.print("\t"); @@ -127,5 +127,5 @@ void printStatus(int chk) } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_suppressError/dhtnew_suppressError.ino b/examples/dhtnew_suppressError/dhtnew_suppressError.ino index 19f730a..ec413a3 100644 --- a/examples/dhtnew_suppressError/dhtnew_suppressError.ino +++ b/examples/dhtnew_suppressError/dhtnew_suppressError.ino @@ -4,16 +4,16 @@ // PURPOSE: DHTNEW library test sketch // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND -// run sketch without connected sensor to see effect +// run sketch without connected sensor to see effect #include diff --git a/examples/dhtnew_test/dhtnew_test.ino b/examples/dhtnew_test/dhtnew_test.ino index f735dba..36a8e1c 100644 --- a/examples/dhtnew_test/dhtnew_test.ino +++ b/examples/dhtnew_test/dhtnew_test.ino @@ -4,23 +4,23 @@ // PURPOSE: DHTNEW library test sketch for Arduino // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include -DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 +DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println("dhtnew_test.ino"); @@ -28,8 +28,8 @@ void setup() Serial.println(DHTNEW_LIB_VERSION); Serial.println(); - // MKR1010 - // mySensor.setDisableIRQ(false); + // MKR1010 + // mySensor.setDisableIRQ(false); Serial.println("\n1. Type detection test, first run might take longer to determine type"); Serial.println("STAT\tHUMI\tTEMP\tTIME\tTYPE"); @@ -86,7 +86,7 @@ void loop() void test() { - // READ DATA + // READ DATA uint32_t start = micros(); int chk = mySensor.read(); uint32_t stop = micros(); @@ -127,7 +127,7 @@ void test() break; } - // DISPLAY DATA + // DISPLAY DATA Serial.print(mySensor.getHumidity(), 1); Serial.print(",\t"); Serial.print(mySensor.getTemperature(), 1); @@ -140,5 +140,5 @@ void test() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_waitForRead/dhtnew_waitForRead.ino b/examples/dhtnew_waitForRead/dhtnew_waitForRead.ino index 349a5f5..8e2c6d0 100644 --- a/examples/dhtnew_waitForRead/dhtnew_waitForRead.ino +++ b/examples/dhtnew_waitForRead/dhtnew_waitForRead.ino @@ -4,23 +4,23 @@ // PURPOSE: DHTNEW library waitForRead example sketch for Arduino // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include -DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 +DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println("\n"); @@ -29,8 +29,8 @@ void setup() Serial.println(DHTNEW_LIB_VERSION); Serial.println(); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); Serial.println("This sketch shows the use of the setWaitForReading() flag (default value is false)."); Serial.println("Setting the flag to true will make the sketch wait until the sensor is ready to take another reading."); @@ -66,16 +66,16 @@ void loop() void test() { - // READ DATA + // READ DATA uint32_t start = micros(); int chk = mySensor.read(); uint32_t stop = micros(); uint32_t duration = stop - start; - // DISPLAY IF OLD OR NEW DATA + // DISPLAY IF OLD OR NEW DATA if (duration > 50){Serial.print("NEW\t");}else{Serial.print("OLD\t");} - // DISPLAY DATA + // DISPLAY DATA Serial.print(mySensor.getHumidity(), 1); Serial.print(",\t"); Serial.print(mySensor.getTemperature(), 1); @@ -122,5 +122,5 @@ void test() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/dhtnew_waitForRead_nonBlocking/dhtnew_waitForRead_nonBlocking.ino b/examples/dhtnew_waitForRead_nonBlocking/dhtnew_waitForRead_nonBlocking.ino index c2a10f6..a7e81f2 100644 --- a/examples/dhtnew_waitForRead_nonBlocking/dhtnew_waitForRead_nonBlocking.ino +++ b/examples/dhtnew_waitForRead_nonBlocking/dhtnew_waitForRead_nonBlocking.ino @@ -4,23 +4,23 @@ // PURPOSE: DHTNEW non-blocking wait for read example sketch for Arduino // URL: https://github.com/RobTillaart/DHTNew -// DHT PIN layout from left to right -// ================================= -// FRONT : DESCRIPTION -// pin 1 : VCC -// pin 2 : DATA -// pin 3 : Not Connected -// pin 4 : GND +// DHT PIN layout from left to right +// ================================= +// FRONT : DESCRIPTION +// pin 1 : VCC +// pin 2 : DATA +// pin 3 : Not Connected +// pin 4 : GND #include -DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 +DHTNEW mySensor(5); // ESP 16 UNO 5 MKR1010 5 void setup() { - while(!Serial); // MKR1010 needs this + while(!Serial); // MKR1010 needs this Serial.begin(115200); Serial.println("\n"); @@ -29,8 +29,8 @@ void setup() Serial.println(DHTNEW_LIB_VERSION); Serial.println(); - // MKR1010 needs this - // mySensor.setDisableIRQ(false); + // MKR1010 needs this + // mySensor.setDisableIRQ(false); Serial.println("This example shows how you can use the output of the read() function to implement non-blocking waiting for read."); Serial.println("In this example, Arduino continuously polls the read() function and returns fresh data (or an error) only when the read delay is over."); @@ -42,7 +42,7 @@ void setup() void loop() { - // READ DATA + // READ DATA uint32_t start = micros(); int chk = mySensor.read(); uint32_t stop = micros(); @@ -81,7 +81,7 @@ void loop() break; } - // DISPLAY DATA + // DISPLAY DATA Serial.print(mySensor.getHumidity(), 1); Serial.print(",\t"); Serial.print(mySensor.getTemperature(), 1); @@ -92,10 +92,10 @@ void loop() Serial.println(mySensor.getType()); } - // do some other stuff + // do some other stuff delay(100); } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/library.json b/library.json index 98e54ea..1445a46 100644 --- a/library.json +++ b/library.json @@ -13,7 +13,7 @@ "type": "git", "url": "https://github.com/RobTillaart/DHTNEW.git" }, - "version": "0.4.19", + "version": "0.4.20", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/library.properties b/library.properties index b55dbc5..66d3ec8 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=DHTNEW -version=0.4.19 +version=0.4.20 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for DHT temperature and humidity sensor, with automatic sensortype recognition. From cc4e3814cf4d6517c859cb428e7f29373d9d48c2 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Sun, 24 Mar 2024 15:35:51 +0100 Subject: [PATCH 2/4] refactor --- CHANGELOG.md | 6 ++-- README.md | 67 ++++++++++++++++++++++++++---------------- dhtnew.cpp | 49 +++++++++++++++++------------- dhtnew.h | 11 +++---- test/unit_test_001.cpp | 56 +++++++++++++++++++++++------------ 5 files changed, 118 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf1c34e..71e6913 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,10 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [0.4.19] - 2024-03-24 -- Fix #95, units of T & H +## [0.4.20] - 2024-03-24 +- fix #95, units of T & H - update GitHub actions +- refactor **setType()** map invalid types to 0 - update readme.md +- update unit test (minor) - minor edits diff --git a/README.md b/README.md index acf5fdd..ec3b727 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ This is the main development library of all my DHT libraries. Supports DHT11, DHT22, DHT33, DHT44, AM2301, AM2302, AM2303 as these all have the same protocol. Note there are differences e.g. DHT11 has no negative temperature, no decimals, and a longer wakeup time. -The DHTNew library returns Temperature in degrees Celsius and Humidity in 0.0 - 100.0%. +The DHTNew library returns Temperature in degrees Celsius and Humidity in 0.0 - 100.0 %RH. +For converting temperature to Fahrenheit or Kelvin, see https://github.com/RobTillaart/Temperature. #### Sonoff Si7021 @@ -45,7 +46,7 @@ Not tested myself, but AM2320 is confirmed to work, see https://github.com/RobTi As the AM2321 and AM2322 are quite identical according to the datasheet, those are expected to work too. To use the library one should call **setType(22)** as the protocol is identical to the DHT22. -If there are differences in operation type (23) will be implemented. +If there are differences in operation type (23) will be elaborated. The value 23 is now mapped upon 22 code. Feedback (both positive and negative) about the AM232X sensors is welcome. @@ -78,16 +79,16 @@ Feedback (both positive and negative) about the AM232X sensors is welcome. | Model | DHT22 | Notes | |:----------------------------|:-------------------------|:--------| -| Power supply | 3.3 - 6 V DC | +| Power supply | 3.3 - 6.0 V DC | | Output signal | digital signal via single-bus | -| Sensing element | Polymer capacitor | -| Operating range | humidity 0-100% RH | temperature -40° - 80° Celsius +| Sensing element | polymer capacitor | +| Operating range | humidity 0.0-100.0% RH | temperature -40° - 80° Celsius | Accuracy humidity | ±2% RH(Max ±5% RH) | temperature < ±0.5° Celsius | Resolution or sensitivity | humidity 0.1% RH | temperature 0.1° Celsius -| Repeatability humidity | ±1% RH | temperature ±0.2° Celsius +| Repeatability humidity | ±1.0% RH | temperature ±0.2° Celsius | Humidity hysteresis | ±0.3% RH | | Long-term Stability | ±0.5% RH/year | -| Sensing period | Average: 2s | +| Sensing period | average: 2 s | | Interchangeability | fully interchangeable | | Dimensions | small 14 x 18 x 5.5 mm | big 22 x 28 x 5 mm | @@ -108,22 +109,24 @@ In case of 0, **getType()** will try to determine type. Since 0.4.14 type 70 is added for **experimental** Sonoff Si7021 support. - **void setType(uint8_t type = 0)** allows to force the type of the sensor. -| type | sensors | notes | -|:------:|:---------------:|:---------------| -| 0 | not defined | | -| 11 | DHT11 | | -| 22 | DHT22 a.o | most others | -| 70 | Sonoff Si7021 | experimental | +| Type | Sensors | Notes | +|:-------:|:---------------:|:--------| +| 0 | not defined | +| 11 | DHT11 | +| 22 | DHT22 a.o | most others +| 23 | DHT22 a.o | mapped to 22 for now +| 70 | Sonoff Si7021 | experimental +| other | sets to 0 | 0.4.20 ### Base interface -- **int read()** reads a new temperature and humidity from the sensor +- **int read()** reads a new temperature (Celsius) and humidity (%RH) from the sensor. - **uint32_t lastRead()** returns milliseconds since last **read()** -- **float getHumidity()** returns last read humidity = 0.0-100.0 %. +- **float getHumidity()** returns last read humidity = 0.0 - 100.0 %RH. In case of an error it returns **DHTLIB_INVALID_VALUE** == -999. Note this error value can be suppressed by **setSuppressError(bool)**. -- **float getTemperature()** returns last read temperature (float) in Celsius. +- **float getTemperature()** returns last read temperature in Celsius. Range depends on the sensor. In case of an error it returns **DHTLIB_INVALID_VALUE** == -999. Note this error value can be suppressed by **setSuppressError(bool)**. @@ -131,8 +134,12 @@ Note this error value can be suppressed by **setSuppressError(bool)**. ### Offset -Adding offsets works well in normal range however they might introduce under- or overflow at the ends of the sensor range. -humidity is constrained to 0..100% in the code. For temperature such constrain would be type dependant, so not done. +Adding offsets works well in normal range however they might introduce +under- or overflow at the ends of the sensor range. +Humidity is in % RH. +Humidity is constrained to 0.0 - 100.0 % in the code. +Temperature is in degrees Celsius. +For temperature such constrain would be type dependant, so it is not done. - **void setHumOffset(float offset)** typical < ±5% RH. - **void setTempOffset(float offset)** typical < ±2°C. @@ -144,18 +151,21 @@ humidity is constrained to 0..100% in the code. For temperature such constrain w Functions to adjust the communication with the sensor. -- **void setDisableIRQ(bool b )** allows or suppresses interrupts during core read function to keep timing as correct as possible. **Note AVR + MKR1010** +- **void setDisableIRQ(bool b )** allows or suppresses interrupts during core +read function to keep timing as correct as possible. **Note AVR + MKR1010** - **bool getDisableIRQ()** returns the above setting. Default **true**. - **void setWaitForReading(bool b )** flag to enforce a blocking wait. - **bool getWaitForReading()** returns the above setting. -- **void setReadDelay(uint16_t rd = 0)** To tune the time it waits before actual read. This reduces the blocking time. +- **void setReadDelay(uint16_t rd = 0)** To tune the time it waits before actual read. +This reduces the blocking time. Default depends on type. 1000 ms (dht11) or 2000 ms (dht22). set readDelay to 0 will reset to datasheet values AFTER a call to **read()**. - **uint16_t getReadDelay()** returns the above setting. - **void powerDown()** pulls dataPin down to reduce power consumption - **void powerUp()** restarts the sensor, note one must wait up to two seconds. -- **void setSuppressError(bool b)** suppress error values of -999 => you need to check the return value of read() instead. -This is used to keep spikes out of your graphs / logs. +- **void setSuppressError(bool b)** suppress error values of -999 => +You need to check the return value of read() instead. +This is used to keep spikes out of your plotter / graphs / logs. - **bool getSuppressError()** returns the above setting. @@ -327,13 +337,20 @@ Update GitHub actions and readme.md #### Could -- improve unit test +- test compatibility => table. - investigate temperature constraining (type dependant) -- fix DHTLIB_VALUE_OUT_OF_RANGE code -- move all code from .h to .cpp + +```cpp +if (type == 11) temp = constrain(temp, 0, 100); +if (type == 22) temp = constrain(temp, -40, 80); +etc. +``` + #### Wont +- move all code from .h to .cpp + ## Support diff --git a/dhtnew.cpp b/dhtnew.cpp index 33bee9f..96d19a4 100644 --- a/dhtnew.cpp +++ b/dhtnew.cpp @@ -47,10 +47,10 @@ void DHTNEW::reset() _wakeupDelay = 0; _type = 0; - _humOffset = (float)0.0; - _tempOffset = (float)0.0; - _humidity = (float)0.0; - _temperature = (float)0.0; + _humOffset = 0.0; + _tempOffset = 0.0; + _humidity = 0.0; + _temperature = 0.0; _lastRead = 0; _disableIRQ = true; _waitForRead = false; @@ -74,18 +74,22 @@ uint8_t DHTNEW::getType() void DHTNEW::setType(uint8_t type) { - if ((type == 0) || (type == 11)) + // default type == 0 or unsupported + _type = 0; + _wakeupDelay = DHTLIB_DHT11_WAKEUP; + + if ((type == 22) || (type == 23)) { _type = type; - _wakeupDelay = DHTLIB_DHT11_WAKEUP; + _wakeupDelay = DHTLIB_DHT_WAKEUP; } - if ((type == 22) || (type == 23)) + else if (type == 11) { _type = type; - _wakeupDelay = DHTLIB_DHT_WAKEUP; + _wakeupDelay = DHTLIB_DHT11_WAKEUP; } // experimental 0.4.14 - if (type == 70) + else if (type == 70) { _type = type; _wakeupDelay = DHTLIB_SI7021_WAKEUP; @@ -192,7 +196,7 @@ int DHTNEW::_read() int16_t t = ((_bits[2] & 0x7F) * 256 + _bits[3]); if (t == 0) { - _temperature = (float)0.0; // prevent -0.0; + _temperature = 0.0; // prevent -0.0; } else { @@ -205,14 +209,14 @@ int DHTNEW::_read() } - // HEXDUMP DEBUG + // HEXDUMP DEBUG /* Serial.println(); - // CHECKSUM + // CHECKSUM if (_bits[4] < 0x10) Serial.print(0); Serial.print(_bits[4], HEX); Serial.print(" "); - // TEMPERATURE + // TEMPERATURE if (_bits[2] < 0x10) Serial.print(0); Serial.print(_bits[2], HEX); if (_bits[3] < 0x10) Serial.print(0); @@ -220,7 +224,7 @@ int DHTNEW::_read() Serial.print(" "); Serial.print(_temperature, 1); Serial.print(" "); - // HUMIDITY + // HUMIDITY if (_bits[0] < 0x10) Serial.print(0); Serial.print(_bits[0], HEX); if (_bits[1] < 0x10) Serial.print(0); @@ -241,13 +245,13 @@ int DHTNEW::_read() } #endif - if (_humOffset != (float)0.0) + if (_humOffset != 0.0) { _humidity += _humOffset; - if (_humidity < 0) _humidity = 0; - if (_humidity > 100) _humidity = 100; + if (_humidity > 100) _humidity = 100; + else if (_humidity < 0) _humidity = 0; } - if (_tempOffset != (float)0.0) + if (_tempOffset != 0.0) { _temperature += _tempOffset; } @@ -266,7 +270,7 @@ int DHTNEW::_read() void DHTNEW::powerUp() { digitalWrite(_dataPin, HIGH); - // do a dummy read to sync the sensor + // do a dummy read to synchronise the sensor read(); }; @@ -298,7 +302,10 @@ int DHTNEW::_readSensor() uint8_t idx = 0; // EMPTY BUFFER - for (uint8_t i = 0; i < 5; i++) _bits[i] = 0; + for (uint8_t i = 0; i < 5; i++) + { + _bits[i] = 0; + } // REQUEST SAMPLE - SEND WAKEUP TO SENSOR pinMode(_dataPin, OUTPUT); @@ -412,7 +419,7 @@ bool DHTNEW::_waitFor(uint8_t state, uint32_t timeout) uint8_t count = 2; while ((micros() - start) < timeout) { - // d elayMicroseconds(1); // less # reads ==> minimizes # glitch reads + // delayMicroseconds(1); // less # reads ==> minimizes # glitch reads if (digitalRead(_dataPin) == state) { count--; diff --git a/dhtnew.h b/dhtnew.h index bbaa708..5d50dd5 100644 --- a/dhtnew.h +++ b/dhtnew.h @@ -59,12 +59,13 @@ class DHTNEW DHTNEW(uint8_t pin); // resets all internals to construction time - // might help to reset a sensor behaving badly.. + // might help to reset a sensor behaving badly void reset(); // 0 = unknown // 11 = DHT11 and compatibles // 22 = DHT22 and compatibles + // 23 = mapped to 22 for now (AM23xx) // 70 = Sonoff Si7021 uint8_t getType(); void setType(uint8_t type = 0); @@ -108,10 +109,10 @@ class DHTNEW uint8_t _dataPin = 0; uint32_t _wakeupDelay = 0; uint8_t _type = 0; - float _humOffset = (float)0.0; - float _tempOffset = (float)0.0; - float _humidity = (float)0.0; - float _temperature = (float)0.0; + float _humOffset = 0.0; + float _tempOffset = 0.0; + float _humidity = 0.0; + float _temperature = 0.0; uint32_t _lastRead = 0; bool _disableIRQ = true; bool _waitForRead = false; diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index d204e17..f5f2d74 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -21,7 +21,6 @@ #include -#include "Arduino.h" #include "dhtnew.h" @@ -46,10 +45,12 @@ unittest(test_constants) assertEqual(-6, DHTLIB_ERROR_TIMEOUT_D ); assertEqual(-7, DHTLIB_ERROR_TIMEOUT_B ); assertEqual(-8, DHTLIB_WAITING_FOR_READ ); - + assertEqual(-100, DHTLIB_HUMIDITY_OUT_OF_RANGE ); assertEqual(-101, DHTLIB_TEMPERATURE_OUT_OF_RANGE); - + + assertEqual(50, DHTLIB_BIT_THRESHOLD); + assertEqual(-999, DHTLIB_INVALID_VALUE); } @@ -58,17 +59,24 @@ unittest(test_constructor) { DHTNEW dht(4); - // verify default flags - // assertEqual(0, dht.getType()); // calls read which blocks. + // verify default flags + // assertEqual(0, dht.getType()); // calls read which blocks. + assertEqual(0.0, dht.getHumidity()); + assertEqual(0.0, dht.getTemperature()); + assertEqual(0, dht.getHumOffset()); assertEqual(0, dht.getTempOffset()); - #if defined(__AVR__) + + assertEqual(0, lastRead(); + +#if defined(__AVR__) fprintf(stderr, "__AVR__ defined."); assertFalse(dht.getDisableIRQ()); - #else +#else fprintf(stderr, "__AVR__ not defined."); assertTrue(dht.getDisableIRQ()); - #endif +#endif + assertFalse(dht.getWaitForReading()); assertEqual(0, dht.getReadDelay()); assertFalse(dht.getSuppressError()); @@ -83,7 +91,7 @@ unittest(test_hum_temp) assertEqual(0, dht.getHumOffset()); dht.setHumOffset(1.5); assertEqual(1.5, dht.getHumOffset()); - + assertEqual(0, dht.getTemperature()); assertEqual(0, dht.getTempOffset()); dht.setTempOffset(-1.5); @@ -91,7 +99,7 @@ unittest(test_hum_temp) } -unittest(test_process_flags) +unittest(test_setType) { DHTNEW dht(4); @@ -99,17 +107,27 @@ unittest(test_process_flags) assertEqual(11, dht.getType()); dht.setType(22); assertEqual(22, dht.getType()); - + dht.setType(23); + assertEqual(23, dht.getType()); + dht.setType(70); + assertEqual(70, dht.getType()); +} + + +unittest(test_process_flags) +{ + DHTNEW dht(4); + dht.setDisableIRQ(true); assertTrue(dht.getDisableIRQ()); dht.setDisableIRQ(false); assertFalse(dht.getDisableIRQ()); - + dht.setWaitForReading(true); assertTrue(dht.getWaitForReading()); dht.setWaitForReading(false); assertFalse(dht.getWaitForReading()); - + dht.setReadDelay(1500); assertEqual(1500, dht.getReadDelay()); dht.setType(11); @@ -118,7 +136,7 @@ unittest(test_process_flags) dht.setType(22); dht.setReadDelay(); assertEqual(0, dht.getReadDelay()); - + dht.setSuppressError(true); assertTrue(dht.getSuppressError()); dht.setSuppressError(false); @@ -131,13 +149,15 @@ unittest(test_read) DHTNEW dht(4); fprintf(stderr, "\tread() cannot be tested GODMODE?\n"); - // int rc = dht.read(); - // fprintf(stderr, "%d\n", rc); - + // int rc = dht.read(); + // fprintf(stderr, "%d\n", rc); + long lr = dht.lastRead(); fprintf(stderr, "\ttime since lastRead %ld\n", lr); } + unittest_main() -// -------- + +// -- END OF FILE -- From fb72437af337d158347053227816cc8ef6e6036f Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Sun, 24 Mar 2024 15:49:16 +0100 Subject: [PATCH 3/4] refactor --- test/unit_test_001.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index f5f2d74..f02877f 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -67,7 +67,7 @@ unittest(test_constructor) assertEqual(0, dht.getHumOffset()); assertEqual(0, dht.getTempOffset()); - assertEqual(0, lastRead(); + assertEqual(0, dht.lastRead(); #if defined(__AVR__) fprintf(stderr, "__AVR__ defined."); From 482e24630aac1c5c41fc0772704e0f7096ee1d44 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Sun, 24 Mar 2024 16:05:40 +0100 Subject: [PATCH 4/4] fix --- test/unit_test_001.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index f02877f..0cc722a 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -67,7 +67,7 @@ unittest(test_constructor) assertEqual(0, dht.getHumOffset()); assertEqual(0, dht.getTempOffset()); - assertEqual(0, dht.lastRead(); + assertEqual(0, dht.lastRead()); #if defined(__AVR__) fprintf(stderr, "__AVR__ defined.");