0.5.0 SHT2x

This commit is contained in:
Rob Tillaart 2023-12-07 17:29:13 +01:00
parent 7237551782
commit 95adf18cb4
23 changed files with 103 additions and 96 deletions

View File

@ -6,17 +6,25 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.5.0] - 2023-12-07
- update API, begin()
- update readme.md
- update examples
- minor edits
----
## [0.4.2] - 2023-11-25 ## [0.4.2] - 2023-11-25
- add **readCachedTemperature()** to async interface - add **bool readCachedTemperature()** to async interface
- update readme.md - update readme.md
## [0.4.1] - 2023-11-25 ## [0.4.1] - 2023-11-25
- fix **reset()**: clear state of resolution, heater and error - fix **bool reset()** clear state of resolution, heater and error
- update readme.md - update readme.md
- minor edits - minor edits
## [0.4.0] - 2023-09-21 ## [0.4.0] - 2023-09-21
- moved TwoWire param from begin() to Constructor - moved TwoWire parameter from begin() to Constructor
- FIx #23 support for Wire1 for ESP32 - FIx #23 support for Wire1 for ESP32
- update readme.md - update readme.md
- minor edits - minor edits

View File

@ -44,6 +44,15 @@ If you want to use more on one I2C bus one needs either an I2C multiplexer
or one should switch sensors on/off like the select in SPI communication. or one should switch sensors on/off like the select in SPI communication.
#### 0.5.0 Breaking change
Version 0.5.0 introduced a breaking change.
You cannot set the pins in **begin()** any more.
This reduces the dependency of processor dependent Wire implementations.
The user has to call **Wire.begin()** and can optionally set the Wire pins
before calling **begin()**.
#### Related #### Related
- https://github.com/RobTillaart/SHT31 - https://github.com/RobTillaart/SHT31
@ -75,15 +84,17 @@ Optional set the wire interface for platforms with multiple I2C buses.
#### Base interface #### Base interface
- **bool begin(int dataPin, int clockPin)** begin function for ESP8266 & ESP32; - **bool begin()** calls **reset()** which can take up to 15 ms.
returns false if device address is incorrect or device cannot be reset. Returns false if device address is nor reachable or device cannot be reset.
- **bool begin()** calls **reset()** which can take up to 15 ms.
- **bool read()** Reads both the temperature and humidity. - **bool read()** Reads both the temperature and humidity.
Initial release has a blocking delay. Initial release has a blocking delay.
- **bool isConnected()** check if sensor is reachable over I2C. Returns false if not connected. - **bool isConnected()** check if sensor is reachable over I2C. Returns false if not connected.
- **uint16_t getStatus()** returns a 2 bit status. See Status fields below. - **uint16_t getStatus()** returns a 2 bit status. See Status fields below.
- **uint32_t lastRead()** in milliSeconds since start of program. - **uint32_t lastRead()** in milliSeconds since start of program.
- **bool reset()** resets the sensor, soft reset, no hard reset supported. - **bool reset()** resets the sensor, soft reset, no hard reset supported.
#### Temperature and humidity
- **float getHumidity()** computes the relative humidity in % based off the latest raw reading, and returns it. - **float getHumidity()** computes the relative humidity in % based off the latest raw reading, and returns it.
- **float getTemperature()** computes the temperature in °C based off the latest raw reading, and returns it. - **float getTemperature()** computes the temperature in °C based off the latest raw reading, and returns it.
- **uint16_t getRawHumidity()** returns the raw two-byte representation of humidity directly from the sensor. - **uint16_t getRawHumidity()** returns the raw two-byte representation of humidity directly from the sensor.
@ -250,19 +261,6 @@ Timing in milliseconds.
- async documentation - async documentation
- clean up code. - clean up code.
#### 0.4.0
- add crc8 check (need sensor to test)
- improve error handling (all code paths)
- investigate blocking delay() in read
- add offset for temperature and humidity
#### 0.4.1
- fix reset(): clear state of resolution, heater and error
#### 0.4.2
- add readCachedTemperature()
#### Should #### Should
- test test test - test test test

View File

@ -1,7 +1,7 @@
// //
// FILE: SHT2x.cpp // FILE: SHT2x.cpp
// AUTHOR: Rob Tillaart, Viktor Balint, JensB // AUTHOR: Rob Tillaart, Viktor Balint, JensB
// VERSION: 0.4.2 // VERSION: 0.5.0
// DATE: 2023-11-25 // DATE: 2023-11-25
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor // PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
// URL: https://github.com/RobTillaart/SHT2x // URL: https://github.com/RobTillaart/SHT2x
@ -50,23 +50,9 @@ SHT2x::SHT2x(TwoWire *wire)
} }
#if defined(ESP8266) || defined(ESP32)
bool SHT2x::begin(const int dataPin, const int clockPin)
{
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
return reset();
}
#endif
bool SHT2x::begin() bool SHT2x::begin()
{ {
_wire->begin(); if (! isConnected()) return false;
return reset(); return reset();
} }
@ -187,8 +173,8 @@ bool SHT2x::readTemperature()
// clear requestType // clear requestType
_requestType = SHT2x_REQ_NONE; _requestType = SHT2x_REQ_NONE;
_status = buffer[1] & 0x0003; _status = buffer[1] & 0x03;
if (_status == 0xFF) // TODO != 0x01 (need HW to test) if (_status == 0xFF) // TODO != 0x01 (need HW to test)
{ {
_error = SHT2x_ERR_READBYTES; _error = SHT2x_ERR_READBYTES;
return false; return false;
@ -212,12 +198,12 @@ bool SHT2x::readHumidity()
} }
_rawHumidity = buffer[0] << 8; _rawHumidity = buffer[0] << 8;
_rawHumidity += buffer[1]; _rawHumidity += buffer[1];
_rawHumidity &= 0xFFFC; // TODO is this mask OK? as humidity is max 12 bit.. _rawHumidity &= 0xFFFC;
// clear requestType // clear requestType
_requestType = SHT2x_REQ_NONE; _requestType = SHT2x_REQ_NONE;
_status = buffer[1] & 0x0003; _status = buffer[1] & 0x03;
if (_status == 0xFF) // TODO != 0x02 (need HW to test) if (_status == 0xFF) // TODO != 0x02 (need HW to test)
{ {
_error = SHT2x_ERR_READBYTES; _error = SHT2x_ERR_READBYTES;

View File

@ -2,7 +2,7 @@
// //
// FILE: SHT2x.h // FILE: SHT2x.h
// AUTHOR: Rob Tillaart, Viktor Balint, JensB // AUTHOR: Rob Tillaart, Viktor Balint, JensB
// VERSION: 0.4.2 // VERSION: 0.5.0
// DATE: 2023-11-25 // DATE: 2023-11-25
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor // PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
// URL: https://github.com/RobTillaart/SHT2x // URL: https://github.com/RobTillaart/SHT2x
@ -13,7 +13,7 @@
#include "Wire.h" #include "Wire.h"
#define SHT2x_LIB_VERSION (F("0.4.2")) #define SHT2x_LIB_VERSION (F("0.5.0"))
// fields getStatus // fields getStatus
@ -50,11 +50,7 @@ class SHT2x
public: public:
SHT2x(TwoWire *wire = &Wire); SHT2x(TwoWire *wire = &Wire);
#if defined(ESP8266) || defined(ESP32)
bool begin(const int dataPin, const int clockPin);
#endif
bool begin(); bool begin();
// check sensor is reachable over I2C // check sensor is reachable over I2C
bool isConnected(); bool isConnected();

View File

@ -8,8 +8,7 @@
#include "Wire.h" #include "Wire.h"
#include "SHT2x.h" #include "SHT2x.h"
uint32_t start; uint32_t start, stop;
uint32_t stop;
SHT2x sht; SHT2x sht;
@ -21,6 +20,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
uint32_t EIDA = sht.getEIDA(); uint32_t EIDA = sht.getEIDA();
@ -44,5 +44,5 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -8,8 +8,7 @@
#include "Wire.h" #include "Wire.h"
#include "SHT2x.h" #include "SHT2x.h"
uint32_t start; uint32_t start, stop;
uint32_t stop;
SHT2x sht; SHT2x sht;
@ -21,6 +20,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
uint8_t stat = sht.getStatus(); uint8_t stat = sht.getStatus();
@ -55,5 +55,5 @@ void test()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -21,6 +21,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
uint8_t stat = sht.getStatus(); uint8_t stat = sht.getStatus();
@ -45,5 +46,5 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -21,7 +21,8 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
sht.begin(12, 13); Wire.begin(12, 13);
sht.begin();
uint8_t stat = sht.getStatus(); uint8_t stat = sht.getStatus();
Serial.print(stat, HEX); Serial.print(stat, HEX);
@ -45,5 +46,5 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -6,9 +6,11 @@
// //
// ESP32 specific - see issue #7 // ESP32 specific - see issue #7
#include "Wire.h" #include "Wire.h"
#include "SHT2x.h" #include "SHT2x.h"
#define SDA_1 21 #define SDA_1 21
#define SCL_1 22 #define SCL_1 22
#define SDA_2 33 #define SDA_2 33
@ -17,8 +19,7 @@
SHT2x internal(&Wire); SHT2x internal(&Wire);
SHT2x external(&Wire1); SHT2x external(&Wire1);
uint32_t start; uint32_t start, stop;
uint32_t stop;
void setup() void setup()
@ -65,4 +66,4 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -18,6 +18,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
uint8_t stat = sht.getStatus(); uint8_t stat = sht.getStatus();
@ -38,9 +39,9 @@ void loop()
sht.requestTemperature(); sht.requestTemperature();
} }
// do other things here // do other things here
delay(1000); delay(1000);
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -27,6 +27,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
uint8_t stat = sht.getStatus(); uint8_t stat = sht.getStatus();
@ -67,7 +68,7 @@ void loop()
Serial.println(sht.getHumidity(), 1); Serial.println(sht.getHumidity(), 1);
} }
// do other things here // do other things here
delay(1000); delay(1000);
} }

View File

@ -22,9 +22,11 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
sht.setHeatTimeout(30); // heater timeout 30 seconds, just for demo. // heater timeout 30 seconds, just for demo.
sht.setHeatTimeout(30);
status = sht.getStatus(); status = sht.getStatus();
printHeaterStatus(status); printHeaterStatus(status);
@ -46,7 +48,7 @@ void setup()
void loop() void loop()
{ {
// forced switch off // forced switch off
sht.heatOff(); sht.heatOff();
delay(1000); delay(1000);
} }
@ -56,7 +58,7 @@ void printHeaterStatus(uint8_t status)
{ {
Serial.print(millis()); Serial.print(millis());
Serial.print("\tHEATER: "); Serial.print("\tHEATER: ");
if (status == 0x00) // TODO - elaborate if (status == 0x00) // TODO - elaborate
{ {
Serial.println("ON"); Serial.println("ON");
} else { } else {
@ -65,4 +67,4 @@ void printHeaterStatus(uint8_t status)
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -18,6 +18,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
for (int i = 0 ; i < 16; i++) for (int i = 0 ; i < 16; i++)
@ -37,4 +38,4 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -8,8 +8,7 @@
#include "Wire.h" #include "Wire.h"
#include "SHT2x.h" #include "SHT2x.h"
uint32_t start; uint32_t start, stop;
uint32_t stop;
SHT2x sht; SHT2x sht;
uint32_t connectionFails = 0; uint32_t connectionFails = 0;
@ -22,6 +21,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
uint8_t stat = sht.getStatus(); uint8_t stat = sht.getStatus();
@ -61,11 +61,11 @@ void loop()
Serial.print(millis()); Serial.print(millis());
Serial.print("\tNot connected:\t"); Serial.print("\tNot connected:\t");
Serial.print(connectionFails); Serial.print(connectionFails);
// sht.reset(); // sht.reset();
} }
Serial.println(); Serial.println();
delay(1000); delay(1000);
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -8,8 +8,7 @@
#include "Wire.h" #include "Wire.h"
#include "SHT2x.h" #include "SHT2x.h"
uint32_t start; uint32_t start, stop;
uint32_t stop;
SHT2x sht; SHT2x sht;
@ -21,6 +20,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
uint8_t stat = sht.getStatus(); uint8_t stat = sht.getStatus();
@ -42,5 +42,5 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -119,5 +119,5 @@ float getHumidity()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -8,8 +8,7 @@
#include "Wire.h" #include "Wire.h"
#include "SHT2x.h" #include "SHT2x.h"
uint32_t start; uint32_t start, stop;
uint32_t stop;
SHT2x sht; SHT2x sht;
@ -21,6 +20,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
uint8_t stat = sht.getStatus(); uint8_t stat = sht.getStatus();
@ -63,4 +63,4 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -8,8 +8,7 @@
#include "Wire.h" #include "Wire.h"
#include "SHT2x.h" #include "SHT2x.h"
uint32_t start; uint32_t start, stop;
uint32_t stop;
SHT2x sht; SHT2x sht;
@ -21,6 +20,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
uint8_t stat = sht.getStatus(); uint8_t stat = sht.getStatus();
@ -64,4 +64,4 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -1,5 +1,5 @@
// //
// FILE: SHT2x_demo.ino // FILE: SHT2x_test_CRC.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: demo // PURPOSE: demo
// URL: https://github.com/RobTillaart/SHT2x // URL: https://github.com/RobTillaart/SHT2x
@ -8,8 +8,7 @@
#include "Wire.h" #include "Wire.h"
#include "SHT2x.h" #include "SHT2x.h"
uint32_t start; uint32_t start, stop;
uint32_t stop;
SHT2x sht; SHT2x sht;
@ -21,6 +20,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
Wire.begin();
sht.begin(); sht.begin();
// uint8_t buf[3] = { 0x7C, 0x82, 0 }; // uint8_t buf[3] = { 0x7C, 0x82, 0 };
@ -38,4 +38,4 @@ void loop()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -24,7 +24,7 @@ void setup()
Serial.print("SHT2x_LIB_VERSION: \t"); Serial.print("SHT2x_LIB_VERSION: \t");
Serial.println(SHT2x_LIB_VERSION); Serial.println(SHT2x_LIB_VERSION);
// connect to sensor // connect to sensor
Wire.begin(); Wire.begin();
while (!dhtSensor.isConnected()) while (!dhtSensor.isConnected())
{ {
@ -33,9 +33,10 @@ void setup()
} }
Serial.println(); Serial.println();
// soft reset sensor // soft reset sensor
dhtSensor.reset(); dhtSensor.reset();
delay(5); // ~5 ms for soft reset to complete // ~5 ms for soft reset to complete
delay(5);
while (!dhtSensor.isConnected()) while (!dhtSensor.isConnected())
{ {
Serial.print("*"); Serial.print("*");
@ -43,8 +44,8 @@ void setup()
} }
Serial.println(); Serial.println();
// change resolution to 11 bits // change resolution to 11 bits
if (!dhtSensor.setResolution(3)) // 3: 11 bits / 0.08 °C / 0.05 % / ~18 ms - see datasheet if (!dhtSensor.setResolution(3)) // 3: 11 bits / 0.08 °C / 0.05 % / ~18 ms - see datasheet
{ {
Serial.print("set resolution error:\t"); Serial.print("set resolution error:\t");
Serial.println(dhtSensor.getError()); Serial.println(dhtSensor.getError());
@ -58,15 +59,15 @@ void loop()
if (dhtSensor.isConnected()) if (dhtSensor.isConnected())
{ {
// async request humidity and wait for completion // async request humidity and wait for completion
dhtSensor.requestHumidity(); dhtSensor.requestHumidity();
int available = 20; // [ms] choose a value that is at least 10 % higher that the measurement time from the datasheet int available = 20; // [ms] choose a value that is at least 10 % higher that the measurement time from the datasheet
while (!dhtSensor.reqHumReady() && (available-- > 0)) while (!dhtSensor.reqHumReady() && (available-- > 0))
{ {
delay(1); // or do something else here delay(1); // or do something else here
} }
// retrieve humidity and temperature values // retrieve humidity and temperature values
if (available) if (available)
{ {
if (!dhtSensor.readHumidity()) if (!dhtSensor.readHumidity())
@ -103,7 +104,7 @@ void loop()
Serial.println("sensor not connected"); Serial.println("sensor not connected");
} }
// do other things here // do other things here
delay(1000); delay(1000);
} }

View File

@ -21,7 +21,7 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/SHT2x.git" "url": "https://github.com/RobTillaart/SHT2x.git"
}, },
"version": "0.4.2", "version": "0.5.0",
"license": "MIT", "license": "MIT",
"frameworks": "*", "frameworks": "*",
"platforms": "*", "platforms": "*",

View File

@ -1,5 +1,5 @@
name=SHT2x name=SHT2x
version=0.4.2 version=0.5.0
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com> maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the I2C SHT20 SHT21 SHT25 series temperature and humidity sensor. sentence=Arduino library for the I2C SHT20 SHT21 SHT25 series temperature and humidity sensor.

View File

@ -39,7 +39,7 @@
#include "Arduino.h" #include "Arduino.h"
#include "SHT2x.h" #include "SHT2x.h"
int expect; // TODO needed as there seems a problem with 8 bit comparisons (char?) int expect; // TODO needed as there seems a problem with 8 bit comparisons (char?)
uint32_t start, stop; uint32_t start, stop;
@ -86,6 +86,7 @@ unittest(test_constructor)
{ {
SHT2x sht; SHT2x sht;
Wire.begin();
bool b = sht.begin(); bool b = sht.begin();
assertEqual(b, true); assertEqual(b, true);
} }
@ -95,6 +96,7 @@ unittest(test_begin)
{ {
SHT2x sht; SHT2x sht;
Wire.begin();
bool b = sht.begin(); bool b = sht.begin();
assertEqual(b, true); assertEqual(b, true);
@ -118,6 +120,8 @@ unittest(test_begin)
unittest(test_read) unittest(test_read)
{ {
SHT2x sht; SHT2x sht;
Wire.begin();
bool b = sht.begin(); bool b = sht.begin();
assertEqual(b, true); assertEqual(b, true);
@ -149,6 +153,8 @@ unittest(test_read)
unittest(test_getStatus) unittest(test_getStatus)
{ {
SHT2x sht; SHT2x sht;
Wire.begin();
bool b = sht.begin(); bool b = sht.begin();
assertEqual(b, true); assertEqual(b, true);
@ -161,6 +167,8 @@ unittest(test_getStatus)
unittest(test_heater) unittest(test_heater)
{ {
SHT2x sht; SHT2x sht;
Wire.begin();
bool b = sht.begin(); bool b = sht.begin();
assertEqual(b, true); assertEqual(b, true);
@ -182,6 +190,8 @@ unittest(test_heater)
unittest(test_resolution) unittest(test_resolution)
{ {
SHT2x sht; SHT2x sht;
Wire.begin();
bool b = sht.begin(); bool b = sht.begin();
assertEqual(b, true); assertEqual(b, true);