From b1ed8ae10ca273639e1252ee62e56cce4a77c9ee Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Wed, 25 Oct 2023 13:33:10 +0200 Subject: [PATCH] simplify begin (#15) - simplify begin() - updated all examples - update readme.md --- CHANGELOG.md | 8 +++++++- DHT20.cpp | 18 +---------------- DHT20.h | 8 ++------ README.md | 18 ++++++++++++++++- examples/DHT20/DHT20.ino | 13 ++++++------ examples/DHT20_I2C_speed/DHT20_I2C_speed.ino | 4 ++-- examples/DHT20_async/DHT20_async.ino | 13 ++++++------ examples/DHT20_lcd/DHT20_lcd.ino | 20 +++++++++---------- examples/DHT20_offset/DHT20_offset.ino | 16 +++++++-------- examples/DHT20_plotter/DHT20_plotter.ino | 8 +++++--- .../DHT20_read_status/DHT20_read_status.ino | 12 +++++------ examples/DHT20_test_esp/.arduino-ci.yml | 2 +- examples/DHT20_test_esp/DHT20_test_esp.ino | 13 +++++------- library.json | 4 ++-- library.properties | 2 +- test/unit_test_001.cpp | 3 +++ 16 files changed, 83 insertions(+), 79 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1d94fb..123f9f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,10 +6,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.3.0] - 2023-10-25 +- simplify begin() +- updated all examples +- update readme.md + +---- + ## [0.2.3] - 2023-07-27 - fix #12 add time out to read function - ## [0.2.2] - 2022-12-21 - update keywords.txt - add defaults to offset functions. diff --git a/DHT20.cpp b/DHT20.cpp index d706ed6..e953918 100644 --- a/DHT20.cpp +++ b/DHT20.cpp @@ -1,7 +1,7 @@ // // FILE: DHT20.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.2.3 +// VERSION: 0.3.0 // PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor. @@ -31,27 +31,11 @@ DHT20::DHT20(TwoWire *wire) bool DHT20::begin() { - _wire->begin(); // _wire->setWireTimeout(DHT20_WIRE_TIME_OUT, true); return isConnected(); } -#if defined(ESP8266) || defined(ESP32) -bool DHT20::begin(const uint8_t dataPin, const uint8_t clockPin) -{ - if ((dataPin < 255) && (clockPin < 255)) - { - _wire->begin(dataPin, clockPin); - } else { - _wire->begin(); - } - // _wire->setWireTimeout(DHT20_WIRE_TIME_OUT, true); - return isConnected(); -} -#endif - - bool DHT20::isConnected() { _wire->beginTransmission(DHT20_ADDRESS); diff --git a/DHT20.h b/DHT20.h index 6d000f0..baa5f7f 100644 --- a/DHT20.h +++ b/DHT20.h @@ -3,7 +3,7 @@ // FILE: DHT20.h // AUTHOR: Rob Tillaart // PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor. -// VERSION: 0.2.3 +// VERSION: 0.3.0 // URL: https://github.com/RobTillaart/DHT20 // @@ -20,7 +20,7 @@ #include "Arduino.h" #include "Wire.h" -#define DHT20_LIB_VERSION (F("0.2.3")) +#define DHT20_LIB_VERSION (F("0.3.0")) #define DHT20_OK 0 #define DHT20_ERROR_CHECKSUM -10 @@ -38,10 +38,6 @@ class DHT20 // fixed address 0x38 DHT20(TwoWire *wire = &Wire); - // start the I2C -#if defined(ESP8266) || defined(ESP32) - bool begin(const uint8_t dataPin, const uint8_t clockPin); -#endif bool begin(); bool isConnected(); uint8_t getAddress(); diff --git a/README.md b/README.md index 7dffe28..a3516f7 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,11 @@ [![Arduino CI](https://github.com/RobTillaart/DHT20/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) [![Arduino-lint](https://github.com/RobTillaart/DHT20/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/DHT20/actions/workflows/arduino-lint.yml) [![JSON check](https://github.com/RobTillaart/DHT20/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/DHT20/actions/workflows/jsoncheck.yml) +[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/DHT20.svg)](https://github.com/RobTillaart/DHT20/issues) + [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/DHT20/blob/master/LICENSE) [![GitHub release](https://img.shields.io/github/release/RobTillaart/DHT20.svg?maxAge=3600)](https://github.com/RobTillaart/DHT20/releases) +[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/DHT20.svg)](https://registry.platformio.org/libraries/robtillaart/DHT20) # DHT20 @@ -52,6 +55,11 @@ reset the sensor if needed in both synchronous and asynchronous calls. This keeps the API simple. The reads are 1-2 ms slower than 0.1.4. (< 50 ms). Still far below the 80 ms mentioned in the datasheet. +### 0.3.0 + +User should call Wire.begin(), and setting I2C pins himself. +It is removed from the specific ESP32 begin() call to be more generic. + #### Tested @@ -140,7 +148,6 @@ and should only be used if you are in a need for speed. #### Constructor - **DHT20(TwoWire \*wire = &Wire)** constructor, using a specific Wire (I2C bus). -- **bool begin(uint8_t dataPin, uint8_t clockPin)** begin for ESP32 et al, to set I2C bus pins. - **bool begin()** initializer for non ESP32. Returns true if connected. - **bool isConnected()** returns true if the address of the DHT20 can be seen on the I2C bus. - **uint8_t getAddress()** returns the (fixed) address - convenience. @@ -269,3 +276,12 @@ the read calls. (0.2.0) - **bool getIgnoreChecksum()** get checksum flag. for completeness. +## 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/examples/DHT20/DHT20.ino b/examples/DHT20/DHT20.ino index ff5c58f..28388a8 100644 --- a/examples/DHT20/DHT20.ino +++ b/examples/DHT20/DHT20.ino @@ -3,7 +3,6 @@ // AUTHOR: Rob Tillaart // PURPOSE: Demo for DHT20 I2C humidity & temperature sensor // - // Always check datasheet - front view // // +--------------+ @@ -22,14 +21,16 @@ uint8_t count = 0; void setup() { - DHT.begin(); // ESP32 default pins 21 22 - Serial.begin(115200); Serial.println(__FILE__); Serial.print("DHT20 LIBRARY VERSION: "); Serial.println(DHT20_LIB_VERSION); Serial.println(); + Wire.begin(); + DHT.begin(); // ESP32 default pins 21 22 + + delay(1000); } @@ -38,7 +39,7 @@ void loop() { if (millis() - DHT.lastRead() >= 1000) { - // READ DATA + // READ DATA uint32_t start = micros(); int status = DHT.read(); uint32_t stop = micros(); @@ -52,7 +53,7 @@ void loop() count++; Serial.print("DHT20 \t"); - // DISPLAY DATA, sensor has only one decimal. + // DISPLAY DATA, sensor has only one decimal. Serial.print(DHT.getHumidity(), 1); Serial.print("\t\t"); Serial.print(DHT.getTemperature(), 1); @@ -91,4 +92,4 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/DHT20_I2C_speed/DHT20_I2C_speed.ino b/examples/DHT20_I2C_speed/DHT20_I2C_speed.ino index 5925d9c..26cd077 100644 --- a/examples/DHT20_I2C_speed/DHT20_I2C_speed.ino +++ b/examples/DHT20_I2C_speed/DHT20_I2C_speed.ino @@ -3,7 +3,6 @@ // AUTHOR: Rob Tillaart // PURPOSE: Demo for DHT20 I2C humidity & temperature sensor // - // Always check datasheet - front view // // +--------------+ @@ -31,6 +30,7 @@ void setup() Serial.println("\nNOTE: datasheet states 400 KHz as maximum.\n"); + Wire.begin(); DHT.begin(); // ESP32 default pins 21, 22 delay(2000); @@ -98,4 +98,4 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/DHT20_async/DHT20_async.ino b/examples/DHT20_async/DHT20_async.ino index 851cdad..c197c43 100644 --- a/examples/DHT20_async/DHT20_async.ino +++ b/examples/DHT20_async/DHT20_async.ino @@ -3,7 +3,6 @@ // AUTHOR: Rob Tillaart // PURPOSE: Demo for DHT20 I2C humidity & temperature sensor // - // Always check datasheet - front view // // +--------------+ @@ -22,21 +21,21 @@ uint32_t counter = 0; void setup() { - DHT.begin(); // ESP32 default 21, 22 - - Wire.setClock(400000); - Serial.begin(115200); Serial.println(__FILE__); Serial.print("DHT20 LIBRARY VERSION: "); Serial.println(DHT20_LIB_VERSION); Serial.println(); + Wire.begin(); + Wire.setClock(400000); + + DHT.begin(); // ESP32 default 21, 22 + delay(2000); // start with initial request Serial.println(DHT.requestData()); - } @@ -65,4 +64,4 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/DHT20_lcd/DHT20_lcd.ino b/examples/DHT20_lcd/DHT20_lcd.ino index aa66791..0cc712d 100644 --- a/examples/DHT20_lcd/DHT20_lcd.ino +++ b/examples/DHT20_lcd/DHT20_lcd.ino @@ -3,7 +3,6 @@ // AUTHOR: Rob Tillaart // PURPOSE: Demo for DHT20 I2C humidity & temperature sensor // - // Always check datasheet - front view // // +--------------+ @@ -57,16 +56,16 @@ uint32_t stop, start; int displayAddress = 0x27; -// lcd object is created as a placeholder as the actual address is determined in setupDisplay() -// constructor needs an I2C address. Better solution would be a function to set the address -// runtime in the class. +// LCD object is created as a placeholder as the actual address is determined in setupDisplay() +// constructor needs an I2C address. Better solution would be a function to set the address +// runtime in the class. LiquidCrystal_I2C lcd(displayAddress); void setupDisplay() { lcd = LiquidCrystal_I2C(displayAddress, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin); lcd.begin(20, 4); - // lcd.setBacklightPin(BACKLIGHT_PIN, NEGATIVE); + // lcd.setBacklightPin(BACKLIGHT_PIN, NEGATIVE); lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE); lcd.setBacklight(BL_ON); } @@ -97,14 +96,15 @@ void display() void setup() { - DHT.begin(); // ESP32 default pins 21 22 - Serial.begin(115200); Serial.println(__FILE__); Serial.print("DHT20 LIBRARY VERSION: "); Serial.println(DHT20_LIB_VERSION); Serial.println(); + Wire.begin(); + DHT.begin(); // ESP32 default pins 21 22 + setupDisplay(); delay(1000); @@ -115,7 +115,7 @@ void loop() { if (millis() - DHT.lastRead() >= 1000) { - // READ DATA + // READ DATA start = micros(); int status = DHT.read(); stop = micros(); @@ -131,7 +131,7 @@ void loop() count++; Serial.print("DHT20 \t"); - // DISPLAY DATA, sensor has only one decimal. + // DISPLAY DATA, sensor has only one decimal. Serial.print(DHT.getHumidity(), 1); Serial.print("\t\t"); Serial.print(DHT.getTemperature(), 1); @@ -170,4 +170,4 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/DHT20_offset/DHT20_offset.ino b/examples/DHT20_offset/DHT20_offset.ino index 964990c..3d30ca5 100644 --- a/examples/DHT20_offset/DHT20_offset.ino +++ b/examples/DHT20_offset/DHT20_offset.ino @@ -3,7 +3,6 @@ // AUTHOR: Rob Tillaart // PURPOSE: Demo for DHT20 I2C humidity & temperature sensor // - // Always check datasheet - front view // // +--------------+ @@ -22,16 +21,17 @@ uint8_t count = 0; void setup() { - DHT.begin(); // ESP32 default pins 21 22 - - Wire.setClock(400000); - Serial.begin(115200); Serial.println(__FILE__); Serial.print("DHT20 LIBRARY VERSION: "); Serial.println(DHT20_LIB_VERSION); Serial.println(); + Wire.begin(); + Wire.setClock(400000); + + DHT.begin(); // ESP32 default pins 21 22 + delay(1000); } @@ -48,7 +48,7 @@ void loop() // make a measurement every 2 seconds if (millis() - DHT.lastRead() >= 2000) { - // READ DATA + // READ DATA uint32_t start = micros(); int status = DHT.read(); uint32_t stop = micros(); @@ -62,7 +62,7 @@ void loop() count++; Serial.print("DHT20 \t"); - // DISPLAY DATA, sensor has only one decimal. + // DISPLAY DATA, sensor has only one decimal. Serial.print(DHT.getHumidity(), 1); Serial.print("\t\t"); Serial.print(DHT.getTemperature(), 1); @@ -105,4 +105,4 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/DHT20_plotter/DHT20_plotter.ino b/examples/DHT20_plotter/DHT20_plotter.ino index 82f33b3..c0adbfe 100644 --- a/examples/DHT20_plotter/DHT20_plotter.ino +++ b/examples/DHT20_plotter/DHT20_plotter.ino @@ -3,7 +3,6 @@ // AUTHOR: Rob Tillaart // PURPOSE: Demo for DHT20 I2C humidity & temperature sensor // - // Always check datasheet - front view // // +--------------+ @@ -13,6 +12,7 @@ // SCL ----| 4 | // +--------------+ + #include "DHT20.h" DHT20 DHT(&Wire); @@ -20,7 +20,9 @@ DHT20 DHT(&Wire); void setup() { + Wire.begin(); DHT.begin(); // ESP32 default pins 21 22 + Serial.begin(115200); Serial.println("Humidity, Temperature"); } @@ -30,7 +32,7 @@ void loop() { if (millis() - DHT.lastRead() >= 1000) { - // note no error checking + // note no error checking DHT.read(); Serial.print(DHT.getHumidity(), 1); Serial.print(", "); @@ -39,4 +41,4 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/DHT20_read_status/DHT20_read_status.ino b/examples/DHT20_read_status/DHT20_read_status.ino index 1fcda8e..f751fd8 100644 --- a/examples/DHT20_read_status/DHT20_read_status.ino +++ b/examples/DHT20_read_status/DHT20_read_status.ino @@ -3,7 +3,6 @@ // AUTHOR: Rob Tillaart // PURPOSE: Demo for DHT20 I2C humidity & temperature sensor // - // Always check datasheet - front view // // +--------------+ @@ -22,16 +21,17 @@ uint8_t count = 0; void setup() { - DHT.begin(); // ESP32 default pins 21 22 - - Wire.setClock(400000); - Serial.begin(115200); Serial.println(__FILE__); Serial.print("DHT20 LIBRARY VERSION: "); Serial.println(DHT20_LIB_VERSION); Serial.println(); + Wire.begin(); + Wire.setClock(400000); + + DHT.begin(); // ESP32 default pins 21 22 + delay(2000); } @@ -44,4 +44,4 @@ void loop() } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/examples/DHT20_test_esp/.arduino-ci.yml b/examples/DHT20_test_esp/.arduino-ci.yml index b301dfc..2c4a7db 100644 --- a/examples/DHT20_test_esp/.arduino-ci.yml +++ b/examples/DHT20_test_esp/.arduino-ci.yml @@ -7,5 +7,5 @@ compile: # - leonardo # - m4 - esp32 - - esp8266 + # - esp8266 # - mega2560 \ No newline at end of file diff --git a/examples/DHT20_test_esp/DHT20_test_esp.ino b/examples/DHT20_test_esp/DHT20_test_esp.ino index b4eec7c..d49d19d 100644 --- a/examples/DHT20_test_esp/DHT20_test_esp.ino +++ b/examples/DHT20_test_esp/DHT20_test_esp.ino @@ -13,25 +13,22 @@ // SCL ----| 4 | // +--------------+ + #include "DHT20.h" -DHT20 DHT(&Wire); +DHT20 DHT(&Wire1); // 2nd I2C interface void setup() { - -#if defined(ESP8266) || defined(ESP32) - DHT.begin(12, 13); // select your pin numbers here -#else - DHT.begin(); -#endif - Serial.begin(115200); Serial.println(__FILE__); Serial.print("DHT20 LIBRARY VERSION: "); Serial.println(DHT20_LIB_VERSION); Serial.println(); + + Wire1.begin(12, 13); // select your pin numbers here + delay(2000); Serial.println("Type,\tStatus,\tHumidity (%),\tTemperature (C)"); diff --git a/library.json b/library.json index 1f61619..2fce220 100644 --- a/library.json +++ b/library.json @@ -15,9 +15,9 @@ "type": "git", "url": "https://github.com/RobTillaart/DHT20.git" }, - "version": "0.2.3", + "version": "0.3.0", "license": "MIT", - "frameworks": "arduino", + "frameworks": "*", "platforms": "*", "headers": "DHT20.h" } diff --git a/library.properties b/library.properties index afc5749..6ea1930 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=DHT20 -version=0.2.3 +version=0.3.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for I2C DHT20 temperature and humidity sensor. diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index fb3acde..c6f8782 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -61,6 +61,7 @@ unittest(test_constructor) assertEqualFloat(0, DHT.getTempOffset(), 0.001); assertEqualFloat(0, DHT.getHumOffset(), 0.001); + Wire.begin(); DHT.begin(); assertEqual(DHT20_ERROR_LASTREAD, DHT.read()); @@ -74,6 +75,8 @@ unittest(test_constructor) unittest(test_offset) { DHT20 DHT(&Wire); + + Wire.begin(); DHT.begin(); assertEqualFloat(0, DHT.getTempOffset(), 0.001);