From c8c9f1177a861939c0298b055b58f121d26dda14 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Sat, 29 Oct 2022 18:55:26 +0200 Subject: [PATCH] 0.1.5 CHT8305 --- libraries/CHT8305/.arduino-ci.yml | 3 +- libraries/CHT8305/CHANGELOG.md | 42 +++++++++++++++++++++++ libraries/CHT8305/CHT8305.cpp | 50 +++++++++++++++++++++++++++- libraries/CHT8305/CHT8305.h | 29 +++++++++++----- libraries/CHT8305/README.md | 11 +++--- libraries/CHT8305/library.json | 2 +- libraries/CHT8305/library.properties | 2 +- 7 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 libraries/CHT8305/CHANGELOG.md diff --git a/libraries/CHT8305/.arduino-ci.yml b/libraries/CHT8305/.arduino-ci.yml index fa8c315b..77a333f9 100644 --- a/libraries/CHT8305/.arduino-ci.yml +++ b/libraries/CHT8305/.arduino-ci.yml @@ -24,4 +24,5 @@ compile: - esp32 - esp8266 # - mega2560 - - rpipico \ No newline at end of file + - rpipico + diff --git a/libraries/CHT8305/CHANGELOG.md b/libraries/CHT8305/CHANGELOG.md new file mode 100644 index 00000000..e7a73829 --- /dev/null +++ b/libraries/CHT8305/CHANGELOG.md @@ -0,0 +1,42 @@ +# Change Log CHT8305 + +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.5] - 2022-10-29 +- fix changelog.md +- add readTemperature() +- add readHumidity() + + +## [0.1.4] - 2022-10-15 +- add CHT8305_DEFAULT_ADDRESS (0x40). +- add CHANGELOG.md. +- minor refactor. +- update readme.md. + +## [0.1.3] - 2022-10-13 +- adjust getVoltage() formula. (still unclear). +- update readme.md (some AVR test results). +- add get- setConversionDelay(). + +## [0.1.2] - 2022-10-09 +- update unit tests. +- fix humidity resolution parameter. +- fix setAlertLevels(). +- refactor, move code to .cpp file. + +## [0.1.1] - 2022-10-08 +- add configuration specific functions. +- fix ESP32 begin() address check. +- add configuration ALERT functions. +- add constants for registers. +- fix getVoltage() register. + +## [0.1.0] - 2022-10-06 +- initial version. + diff --git a/libraries/CHT8305/CHT8305.cpp b/libraries/CHT8305/CHT8305.cpp index 40f528a1..9ad3c132 100644 --- a/libraries/CHT8305/CHT8305.cpp +++ b/libraries/CHT8305/CHT8305.cpp @@ -1,7 +1,7 @@ // // FILE: CHT8305.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.4 +// VERSION: 0.1.5 // PURPOSE: Arduino library for CHT8305 temperature and humidity sensor // URL: https://github.com/RobTillaart/CH8305 // @@ -60,6 +60,12 @@ bool CHT8305::isConnected() } +uint8_t CHT8305::getAddress() +{ + return _address; +} + + //////////////////////////////////////////////// // // READ THE SENSOR @@ -88,6 +94,48 @@ int CHT8305::read() } +int CHT8305::readTemperature() +{ + // do not read too fast + if (millis() - _lastRead < 1000) + { + return CHT8305_ERROR_LASTREAD; + } + _lastRead = millis(); + + uint8_t data[2] = {0, 0}; + _readRegister(CHT8305_REG_TEMPERATURE, &data[0], 2); + + uint16_t tmp = data[0] << 8 | data[1]; + _temperature = tmp * (165.0 / 65535.0) - 40.0; + + if (_tempOffset != 0.0) _temperature += _tempOffset; + + return CHT8305_OK; +} + + +int CHT8305::readHumidity() +{ + // do not read too fast + if (millis() - _lastRead < 1000) + { + return CHT8305_ERROR_LASTREAD; + } + _lastRead = millis(); + + uint8_t data[2] = {0, 0}; + _readRegister(CHT8305_REG_HUMIDITY, &data[0], 2); + + uint16_t tmp = data[0] << 8 | data[1]; + _humidity = tmp / 655.35; // == / 65535 * 100% + + if (_humOffset != 0.0) _humidity += _humOffset; + + return CHT8305_OK; +} + + // MilliSeconds since start sketch uint32_t CHT8305::lastRead() { diff --git a/libraries/CHT8305/CHT8305.h b/libraries/CHT8305/CHT8305.h index 1809b393..d826aa38 100644 --- a/libraries/CHT8305/CHT8305.h +++ b/libraries/CHT8305/CHT8305.h @@ -2,7 +2,7 @@ // // FILE: CHT8305.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.4 +// VERSION: 0.1.5 // PURPOSE: Arduino library for CHT8305 temperature and humidity sensor // URL: https://github.com/RobTillaart/CH8305 // @@ -12,7 +12,7 @@ #include "Wire.h" -#define CHT8305_LIB_VERSION (F("0.1.4")) +#define CHT8305_LIB_VERSION (F("0.1.5")) // DEFAULT ADDRESS #ifndef CHT8305_DEFAULT_ADDRESS @@ -63,9 +63,16 @@ public: // address and writeProtectPin is optional int begin(const uint8_t address = CHT8305_DEFAULT_ADDRESS); bool isConnected(); + uint8_t getAddress(); - // read the temperature and humidity. + + // read both the temperature and humidity. int read(); + // read only temperature (slightly faster) + int readTemperature(); + // read only humidity (slightly faster) + int readHumidity(); + // lastRead is in MilliSeconds since start sketch uint32_t lastRead(); float getHumidity(); @@ -73,6 +80,7 @@ public: void setConversionDelay(uint8_t cd = 14); uint8_t getConversionDelay(); + // adding offsets works well in normal range // might introduce under- or overflow at the ends of the sensor range void setHumOffset(float offset); @@ -80,6 +88,7 @@ public: float getHumOffset(); float getTempOffset(); + // CONFIG REGISTER void setConfigRegister(uint16_t bitmask); uint16_t getConfigRegister(); @@ -107,7 +116,8 @@ public: void setI2CClockStretch(bool on = false); bool getI2CClockStretch(); - void setHeaterOn(bool on = false); // WARNING: user is responsible for timing! + // WARNING: user is responsible for timing! WARNING! + void setHeaterOn(bool on = false); bool getHeater(); void setMeasurementMode(bool both = true); @@ -115,11 +125,13 @@ public: bool getVCCstatus(); - void setTemperatureResolution(uint8_t res = 0); // 1 = 11 bit, other = 14 bit + // 1 = 11 bit, other = 14 bit + void setTemperatureResolution(uint8_t res = 0); uint8_t getTemperatureResolution(); - void setHumidityResolution(uint8_t res = 0); // 2 = 8 bit, 1 = 11 bit, other = 14 bit - uint8_t getHumidityResolution(); // idem + // 2 = 8 bit, 1 = 11 bit, other = 14 bit + void setHumidityResolution(uint8_t res = 0); + uint8_t getHumidityResolution(); void setVCCenable(bool enable = true); bool getVCCenable(); @@ -130,7 +142,7 @@ public: // 1 T // 2 H // 3 T and H - bool setAlertTriggerMode(uint8_t mode = 0); // 0, 1, 2, 3 + bool setAlertTriggerMode(uint8_t mode = 0); uint8_t getAlertTriggerMode(); bool getAlertPendingStatus(); bool getAlertHumidityStatus(); @@ -149,6 +161,7 @@ public: uint16_t getManufacturer(); // expect 0x5959 uint16_t getVersionID(); // may vary + private: float _humOffset = 0.0; float _tempOffset = 0.0; diff --git a/libraries/CHT8305/README.md b/libraries/CHT8305/README.md index 29ec8309..e96d4e5d 100644 --- a/libraries/CHT8305/README.md +++ b/libraries/CHT8305/README.md @@ -110,12 +110,18 @@ Pull ups are needed on SDA, SCL and optional to ALERT. - **int begin(int sda, int scl, const uint8_t address = CHT8305_DEFAULT_ADDRESS)** idem ESP32 et. al. - **bool isConnected()** checks if address can be seen on the I2C bus. - **int read()** reads both the temperature and humidity. +Can be called once per second. +- **int readTemperature()** read only temperature (slightly faster than read) +- **int readHumidity()** read only humidity (slightly faster than read) - **uint32_t lastRead()** returns lastRead in MilliSeconds since start sketch. - **float getHumidity()** returns last humidity read. Will return the same value until **read()** is called again. - **float getTemperature()** returns last temperature read. Will return the same value until **read()** is called again. +Note: read(), readTemperature() and readHumidity() blocks each other, +so you can call one of them every second. + ### Conversion delay @@ -252,9 +258,6 @@ Test returned 0x8305. - test ALERT functions. - test write / readRegister with a single uint16_t to simplify code. -### Could +#### Could -- add **uint8_t getAddress()** -- add **void readTemperature()** does single acquisition. -- add **void readHumidity()** does single acquisition. diff --git a/libraries/CHT8305/library.json b/libraries/CHT8305/library.json index 698e23e1..b402cdb0 100644 --- a/libraries/CHT8305/library.json +++ b/libraries/CHT8305/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/CHT8305.git" }, - "version": "0.1.4", + "version": "0.1.5", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/libraries/CHT8305/library.properties b/libraries/CHT8305/library.properties index 9d03d5ab..c0e3155a 100644 --- a/libraries/CHT8305/library.properties +++ b/libraries/CHT8305/library.properties @@ -1,5 +1,5 @@ name=CHT8305 -version=0.1.4 +version=0.1.5 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for CHT8305 temperature and humidity sensor.