From 2ee0388db810c25eae22336ac7ead8464deee227 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Fri, 12 May 2023 17:01:00 +0200 Subject: [PATCH] 0.1.1 SHT31_SW --- libraries/SHT31_SW/CHANGELOG.md | 9 ++ libraries/SHT31_SW/README.md | 43 +++++-- libraries/SHT31_SW/SHT31_SW.cpp | 109 +++++++++++++++--- libraries/SHT31_SW/SHT31_SW.h | 46 +++++++- .../examples/SHT31_demo/SHT31_demo.ino | 9 +- libraries/SHT31_SW/keywords.txt | 34 +----- libraries/SHT31_SW/library.json | 4 +- libraries/SHT31_SW/library.properties | 4 +- libraries/SHT31_SW/test/unit_test_001.cpp | 4 +- 9 files changed, 190 insertions(+), 72 deletions(-) diff --git a/libraries/SHT31_SW/CHANGELOG.md b/libraries/SHT31_SW/CHANGELOG.md index eb64a5bd..565f9cf7 100644 --- a/libraries/SHT31_SW/CHANGELOG.md +++ b/libraries/SHT31_SW/CHANGELOG.md @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.1.1] - 2023-05-12 +- add **commented** version with **SoftwareWire** to "fix" issue #5 +- clean up SHT31_SW.cpp +- update keywords.txt +- update readme.md + + ## [0.1.0] - 2023-03-23 - initial version, derives from SHT31 0.3.8 +- uses **SoftWire** I2C library. + diff --git a/libraries/SHT31_SW/README.md b/libraries/SHT31_SW/README.md index 5c4c1b4d..5ccc2d62 100644 --- a/libraries/SHT31_SW/README.md +++ b/libraries/SHT31_SW/README.md @@ -13,12 +13,13 @@ Arduino library for the SHT31 temperature and humidity sensor - using **SoftWire ## Description -This library features the class SHT31_SW derived from - https://github.com/RobTillaart/SHT31 +This experimental library features the class SHT31_SW derived from - https://github.com/RobTillaart/SHT31 It has the same interface as the SHT31 class so please use that documentation as it will be the "leading class". The main difference is that this SHT31_SW class uses the **SoftWire** library for communication instead of the **TwoWire** based **Wire** class. +See SHT31 PR #35. **SoftWire** is a software I2C bus library to be able to select other pins than the default (AVR) hardware I2C pins (SDA and SCL). @@ -26,10 +27,18 @@ An important reason to use this version is when you want more than two devices on one Arduino. -#### Status +#### SoftwareWire (AVR only) -- experimental -- created as alternative for SHT31 PR #35. +The **SoftWire** library is portable, but seems less stable as it could not read +the SHT85 sensor which is command compatible with the SHT3x. +The cause is not found yet, so until then an alternative **SoftwareWire** is included. +(If you know a solution, please let me know). + +The **SoftwareWire** library is an AVR specific and worked for the SHT85. +See https://github.com/RobTillaart/SHT31_SW/issues/5 + +You need to comment/uncomment the .h and .cpp file and of course use **SoftwareWire** +instead of **SoftWire** in your project. #### SHT sensors @@ -40,21 +49,22 @@ This library should also work for SHT30/35/85 but these are not tested yet. Accuracy table -| SENSOR | Temperature | Humidity | -|:--------:|:-------------:|:----------:| -| SHT30 | ~0.3 | 2.0 | -| SHT31 | ~0.3 | 1.5 | -| SHT35 | ~0.2 | 1.5 | -| SHT85 | ~0.2 | 1.5 | +| SENSOR | Temperature | Humidity | works with | +|:--------:|:-------------:|:----------:|:---------------| +| SHT30 | ~0.3 | 2.0 | (not tested) | +| SHT31 | ~0.3 | 1.5 | SoftWire | +| SHT35 | ~0.2 | 1.5 | (not tested) | +| SHT85 | ~0.2 | 1.5 | SoftwareWire | #### Links These libraries need to be installed to get SHT31_SW working: +- https://github.com/RobTillaart/SHT31 - https://github.com/stevemarple/SoftWire - https://github.com/stevemarple/AsyncDelay -- https://github.com/RobTillaart/SHT31 +- https://github.com/Testato/SoftwareWire // AVR only ## Interface @@ -169,6 +179,15 @@ Returns false if reading fails or in case of a CRC failure. ## Future -- keep in sync with (leading) SHT31 library +#### Must +- keep in sync with (leading) SHT31 library. +- remove script for atomic if not needed any more. +- investigate why SHT85 does not work with SoftWire. + +#### Should + +#### Could + +#### Wont diff --git a/libraries/SHT31_SW/SHT31_SW.cpp b/libraries/SHT31_SW/SHT31_SW.cpp index 1f71ad54..0f7c21cb 100644 --- a/libraries/SHT31_SW/SHT31_SW.cpp +++ b/libraries/SHT31_SW/SHT31_SW.cpp @@ -1,7 +1,7 @@ // // FILE: SHT31_SW.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 +// VERSION: 0.1.1 // DATE: 2019-02-08 (base SHT31 lib) // PURPOSE: Arduino library for the SHT31 temperature and humidity sensor // to be used with the SoftWire library instead of (hardware) Wire. @@ -14,21 +14,6 @@ #include "SHT31_SW.h" -// SUPPORTED COMMANDS - single shot mode only -#define SHT31_READ_STATUS 0xF32D -#define SHT31_CLEAR_STATUS 0x3041 - -#define SHT31_SOFT_RESET 0x30A2 -#define SHT31_HARD_RESET 0x0006 - -#define SHT31_MEASUREMENT_FAST 0x2416 // page 10 datasheet -#define SHT31_MEASUREMENT_SLOW 0x2400 // no clock stretching - -#define SHT31_HEAT_ON 0x306D -#define SHT31_HEAT_OFF 0x3066 -#define SHT31_HEATER_TIMEOUT 180000UL // milliseconds - - SHT31_SW::SHT31_SW() { _softWire = NULL; @@ -79,7 +64,7 @@ bool SHT31_SW::isConnected() bool SHT31_SW::writeCmd(uint16_t cmd) { _softWire->beginTransmission(_address); - _softWire->write(cmd >> 8 ); + _softWire->write(cmd >> 8); _softWire->write(cmd & 0xFF); if (_softWire->endTransmission() != 0) { @@ -106,5 +91,95 @@ bool SHT31_SW::readBytes(uint8_t n, uint8_t *val) } + +///////////////////////////////////////////////////////// +// +// This version is using an AVR specific SoftwareWire +// it can read the SHT85 where the above can't. +// See https://github.com/RobTillaart/SHT31_SW/issues/5 +// +// you need to comment the above version (also in .h file) +// and use SoftwareWire instead of SoftWire in your project. +// + +/* +SHT31_SW::SHT31_SW() +{ + _softWire = NULL; + _address = 0; + _lastRead = 0; + _rawTemperature = 0; + _rawHumidity = 0; + _heatTimeout = 0; + _heaterStart = 0; + _heaterStop = 0; + _heaterOn = false; + _error = SHT31_OK; +} + + +bool SHT31_SW::begin(const uint8_t address, SoftwareWire *softWire) +{ + if ((address != 0x44) && (address != 0x45)) + { + return false; + } + _address = address; + _softWire = softWire; + _softWire->begin(); + return reset(); +} + + +bool SHT31_SW::begin(SoftwareWire *softWire) +{ + return begin(SHT_DEFAULT_ADDRESS, softWire); +} + + +bool SHT31_SW::isConnected() +{ + _softWire->beginTransmission(_address); + int rv = _softWire->endTransmission(); + if (rv != 0) _error = SHT31_ERR_NOT_CONNECT; + return (rv == 0); +} + + +//////////////////////////////////////////////////// +// +// PRIVATE +// +bool SHT31_SW::writeCmd(uint16_t cmd) +{ + _softWire->beginTransmission(_address); + _softWire->write(cmd >> 8 ); + _softWire->write(cmd & 0xFF); + if (_softWire->endTransmission() != 0) + { + _error = SHT31_ERR_WRITECMD; + return false; + } + return true; +} + + +bool SHT31_SW::readBytes(uint8_t n, uint8_t *val) +{ + int rv = _softWire->requestFrom(_address, (uint8_t) n); + if (rv == n) + { + for (uint8_t i = 0; i < n; i++) + { + val[i] = _softWire->read(); + } + return true; + } + _error = SHT31_ERR_READBYTES; + return false; +} +*/ + + // -- END OF FILE -- diff --git a/libraries/SHT31_SW/SHT31_SW.h b/libraries/SHT31_SW/SHT31_SW.h index 4bc33b05..80f850a2 100644 --- a/libraries/SHT31_SW/SHT31_SW.h +++ b/libraries/SHT31_SW/SHT31_SW.h @@ -2,7 +2,7 @@ // // FILE: SHT31_SW.h // AUTHOR: Rob Tillaart, Gunter Haug -// VERSION: 0.1.0 +// VERSION: 0.1.1 // DATE: 2019-02-08 (base SHT31 lib) // PURPOSE: Arduino library for the SHT31 temperature and humidity sensor // to be used with the SoftWire library instead of (hardware) Wire. @@ -12,14 +12,14 @@ // URL: https://github.com/RobTillaart/SHT31 +#define SHT31_SW_LIB_VERSION (F("0.1.1")) + + #include "Arduino.h" #include "SoftWire.h" #include "SHT31.h" -#define SHT31_SW_LIB_VERSION (F("0.1.0")) - - class SHT31_SW : public SHT31 { public: @@ -39,5 +39,43 @@ private: }; + +///////////////////////////////////////////////////////// +// +// This version is using an AVR specific SoftwareWire +// it can read the SHT85 where the above can't. +// See https://github.com/RobTillaart/SHT31_SW/issues/5 +// +// you need to comment the above version (also in .cpp file) +// and use SoftwareWire instead of SoftWire in your project. +// + + +/* +#include "Arduino.h" +#include "SoftwareWire.h" +#include "SHT31.h" + + +class SHT31_SW : public SHT31 +{ +public: + SHT31_SW(); + + // use SHT_DEFAULT_ADDRESS + bool begin(const uint8_t address, SoftwareWire *wire); + bool begin(SoftwareWire *wire); + + // check sensor is reachable over I2C + bool isConnected(); + +private: + bool writeCmd(uint16_t cmd); + bool readBytes(uint8_t n, uint8_t *val); + SoftwareWire* _softWire; +}; +*/ + + // -- END OF FILE -- diff --git a/libraries/SHT31_SW/examples/SHT31_demo/SHT31_demo.ino b/libraries/SHT31_SW/examples/SHT31_demo/SHT31_demo.ino index cba7f9a7..52e3698c 100644 --- a/libraries/SHT31_SW/examples/SHT31_demo/SHT31_demo.ino +++ b/libraries/SHT31_SW/examples/SHT31_demo/SHT31_demo.ino @@ -27,8 +27,13 @@ void setup() Serial.println(SHT31_SW_LIB_VERSION); sw.begin(); - sht.begin(SHT31_ADDRESS, &sw); sw.setClock(100000); + sht.begin(SHT31_ADDRESS, &sw); + + Serial.print("CON:\t"); + Serial.println(sht.isConnected()); + delay(100); + uint16_t stat = sht.readStatus(); Serial.print(stat, HEX); @@ -48,7 +53,7 @@ void loop() Serial.print(sht.getTemperature(), 1); Serial.print("\t"); Serial.println(sht.getHumidity(), 1); - delay(100); + delay(3000); } diff --git a/libraries/SHT31_SW/keywords.txt b/libraries/SHT31_SW/keywords.txt index 2da583b7..080a3194 100644 --- a/libraries/SHT31_SW/keywords.txt +++ b/libraries/SHT31_SW/keywords.txt @@ -1,4 +1,4 @@ -# Syntax Colouring Map For SHT31 temperature and humidity sensor +# Syntax Colouring Map For SHT31_SW temperature and humidity sensor # Data types (KEYWORD1) @@ -6,42 +6,12 @@ SHT31_SW KEYWORD1 # Methods and Functions (KEYWORD2) -begin KEYWORD2 -read KEYWORD2 -isConnected KEYWORD2 -readStatus KEYWORD2 -lastRead KEYWORD2 -reset KEYWORD2 - -setHeatTimeout KEYWORD2 -heatOn KEYWORD2 -heatOff KEYWORD2 -isHeaterOn KEYWORD2 - -getHumidity KEYWORD2 -getTemperature KEYWORD2 - -requestData KEYWORD2 -dataReady KEYWORD2 -readData KEYWORD2 - -getRawHumidity KEYWORD2 -getRawTemperature KEYWORD2 # Instances (KEYWORD2) # Constants (LITERAL1) -SHT31_LIB_VERSION LITERAL1 +SHT31_SW_LIB_VERSION LITERAL1 -SHT31_STATUS_ALERT_PENDING LITERAL1 -SHT31_STATUS_HEATER_ON LITERAL1 -SHT31_STATUS_HUM_TRACK_ALERT LITERAL1 -SHT31_STATUS_TEMP_TRACK_ALERT LITERAL1 -SHT31_STATUS_SYSTEM_RESET LITERAL1 -SHT31_STATUS_COMMAND_STATUS LITERAL1 -SHT31_STATUS_WRITE_CRC_STATUS LITERAL1 -SHT31_ERR_HEATER_COOLDOWN LITERAL1 -SHT31_ERR_HEATER_ON LITERAL1 diff --git a/libraries/SHT31_SW/library.json b/libraries/SHT31_SW/library.json index d5850620..c6a8eb1e 100644 --- a/libraries/SHT31_SW/library.json +++ b/libraries/SHT31_SW/library.json @@ -1,6 +1,6 @@ { "name": "SHT31_SW", - "keywords": "SoftWire,I2C,SHT30,SHT35,SHT85", + "keywords": "SoftWire,I2C,SHT30,SHT31,SHT35,SHT85", "description": "Arduino library for the I2C SHT31 temperature and humidity sensor.", "authors": [ @@ -18,7 +18,7 @@ "type": "git", "url": "https://github.com/RobTillaart/SHT31_SW" }, - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/libraries/SHT31_SW/library.properties b/libraries/SHT31_SW/library.properties index 8bf1d178..54cecb49 100644 --- a/libraries/SHT31_SW/library.properties +++ b/libraries/SHT31_SW/library.properties @@ -1,9 +1,9 @@ name=SHT31_SW -version=0.1.0 +version=0.1.1 author=Rob Tillaart , Gunter Haug maintainer=Rob Tillaart sentence=Arduino library for the I2C SHT31 temperature and humidity sensor -paragraph=SoftWire,I2C,SHT30,SHT35,SHT85 +paragraph=SoftWire,I2C,SHT30,SHT31,SHT35,SHT85 category=Sensors url=https://github.com/RobTillaart/SHT31_SW architectures=* diff --git a/libraries/SHT31_SW/test/unit_test_001.cpp b/libraries/SHT31_SW/test/unit_test_001.cpp index 1a6727a2..ec4006fe 100644 --- a/libraries/SHT31_SW/test/unit_test_001.cpp +++ b/libraries/SHT31_SW/test/unit_test_001.cpp @@ -198,4 +198,6 @@ unittest(test_async) unittest_main() -// -------- \ No newline at end of file + +// -- END OF FILE -- +