0.5.0 SHT31

This commit is contained in:
Rob Tillaart 2023-12-09 17:38:29 +01:00
parent 68fad4c223
commit 2d93065d5e
17 changed files with 162 additions and 140 deletions

View File

@ -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.5.0] - 2023-12-09
- refactor API, constructor and begin()
- add **getAddress()**
- update readme.md
- update examples
- minor edits
----
## [0.4.0] - 2023-09-21
- fix #38 support for Wire1 for ESP32
- update examples

View File

@ -40,24 +40,43 @@ A derived class for using the SHT31 sensor with SoftWire (soft I2C) can be found
- https://github.com/RobTillaart/SHT31_SW
#### 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
- https://github.com/RobTillaart/SHT31
- https://github.com/RobTillaart/SHT31_SW
- https://github.com/RobTillaart/SHT31_SWW
- https://github.com/RobTillaart/SHT85
## Interface
```cpp
#include "SHT31.h"
```
#### Constructor
#### Base interface
- **SHT31(TwoWire \*wire = &Wire)** constructor. Optional select the I2C bus (Wire, Wire1 etc).
- **bool begin(uint8_t address, uint8_t dataPin, uint8_t clockPin)** begin function for ESP8266 & ESP32;
returns false if device address is incorrect or device cannot be reset.
- **bool begin(uint8_t dataPin, uint8_t clockPin)** same as above. With default SHT_DEFAULT_ADDRESS.
- **bool begin(uint8_t address = SHT_DEFAULT_ADDRESS)**
- **SHT31(uint8_t address = SHT_DEFAULT_ADDRESS, TwoWire \*wire = &Wire)** constructor.
Optional select address and the I2C bus (Wire, Wire1 etc).
- **bool begin()**
Returns false if device address is incorrect or device cannot be reset.
- **bool isConnected()** check sensor is reachable over I2C. Returns false if not connected.
- **uint8_t getAddress()** returns address set in the constructor.
#### Read
- **bool read(bool fast = true)** blocks 4 (fast) or 15 (slow) milliseconds + actual read + math.
Does read both the temperature and humidity.
- **bool isConnected()** check sensor is reachable over I2C. Returns false if not connected.
- **uint16_t readStatus()** details see datasheet and **Status fields** below.
- **uint32_t lastRead()** in milliSeconds since start of program.
- **bool reset(bool hard = false)** resets the sensor, soft reset by default. Returns false if it fails.

View File

@ -1,7 +1,7 @@
//
// FILE: SHT31.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.4.0
// VERSION: 0.5.0
// DATE: 2019-02-08
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
// https://www.adafruit.com/product/2857
@ -26,10 +26,10 @@
#define SHT31_HEATER_TIMEOUT 180000UL // milliseconds
SHT31::SHT31(TwoWire *wire)
SHT31::SHT31(uint8_t address, TwoWire *wire)
{
_wire = wire;
_address = 0;
_address = address;
_lastRead = 0;
_rawTemperature = 0;
_rawHumidity = 0;
@ -41,41 +41,28 @@ SHT31::SHT31(TwoWire *wire)
}
#if defined(ESP8266) || defined(ESP32)
bool SHT31::begin(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
bool SHT31::begin()
{
if ((address != 0x44) && (address != 0x45))
if ((_address != 0x44) && (_address != 0x45))
{
return false;
}
_address = address;
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
return reset();
}
bool SHT31::begin(const uint8_t dataPin, const uint8_t clockPin)
bool SHT31::isConnected()
{
return begin(SHT_DEFAULT_ADDRESS, dataPin, clockPin);
_wire->beginTransmission(_address);
int rv = _wire->endTransmission();
if (rv != 0) _error = SHT31_ERR_NOT_CONNECT;
return (rv == 0);
}
#endif
bool SHT31::begin(const uint8_t address)
uint8_t SHT31::getAddress()
{
if ((address != 0x44) && (address != 0x45))
{
return false;
}
_address = address;
_wire->begin();
return reset();
return _address;
}
@ -90,14 +77,6 @@ bool SHT31::read(bool fast)
}
bool SHT31::isConnected()
{
_wire->beginTransmission(_address);
int rv = _wire->endTransmission();
if (rv != 0) _error = SHT31_ERR_NOT_CONNECT;
return (rv == 0);
}
#ifdef doc
// bit - description
// ==================

View File

@ -2,7 +2,7 @@
//
// FILE: SHT31.h
// AUTHOR: Rob Tillaart
// VERSION: 0.4.0
// VERSION: 0.5.0
// DATE: 2019-02-08
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
// https://www.adafruit.com/product/2857
@ -13,7 +13,7 @@
#include "Wire.h"
#define SHT31_LIB_VERSION (F("0.4.0"))
#define SHT31_LIB_VERSION (F("0.5.0"))
#ifndef SHT_DEFAULT_ADDRESS
#define SHT_DEFAULT_ADDRESS 0x44
@ -44,21 +44,17 @@
class SHT31
{
public:
SHT31(TwoWire *wire = &Wire);
SHT31(uint8_t address = SHT_DEFAULT_ADDRESS, TwoWire *wire = &Wire);
#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
// use SHT_DEFAULT_ADDRESS
bool begin(const uint8_t dataPin, const uint8_t clockPin);
#endif
bool begin(const uint8_t address = SHT_DEFAULT_ADDRESS);
// blocks 15 milliseconds + actual read + math
bool read(bool fast = true);
bool begin();
uint8_t getAddress();
// check sensor is reachable over I2C
virtual bool isConnected();
// blocks 15 milliseconds + actual read + math
bool read(bool fast = true);
// details see datasheet; summary in SHT31.cpp file
uint16_t readStatus();
@ -92,18 +88,16 @@ public:
int getError(); // clears error flag
protected:
uint8_t _address;
uint8_t _heatTimeout; // seconds
uint32_t _lastRead;
uint32_t _lastRequest; // for async interface
uint32_t _heaterStart;
uint32_t _heaterStop;
bool _heaterOn;
uint8_t _address;
uint8_t _heatTimeout; // seconds
uint32_t _lastRead;
uint32_t _lastRequest; // for async interface
uint32_t _heaterStart;
uint32_t _heaterStop;
bool _heaterOn;
uint16_t _rawHumidity;
uint16_t _rawTemperature;
uint8_t _error;
uint8_t _error;
private:
uint8_t crc8(const uint8_t *data, uint8_t len);

View File

@ -13,7 +13,7 @@
uint32_t start;
uint32_t stop;
SHT31 sht;
SHT31 sht(SHT31_ADDRESS);
void setup()
@ -24,8 +24,8 @@ void setup()
Serial.println(SHT31_LIB_VERSION);
Wire.begin();
sht.begin(SHT31_ADDRESS);
Wire.setClock(100000);
sht.begin();
uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
@ -41,13 +41,14 @@ void loop()
Wire.setClock(I2Cfreq);
test();
}
Serial.println();
}
void test()
{
start = micros();
sht.read(true); // default = true/fast slow = false
sht.read(true); // default = true/fast slow = false
stop = micros();
Serial.print("\t");
Serial.print(stop - start);
@ -55,9 +56,9 @@ void test()
Serial.print(sht.getTemperature(), 1);
Serial.print("\t");
Serial.println(sht.getHumidity(), 1);
delay(100);
delay(1000);
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -14,7 +14,7 @@ uint32_t start;
uint32_t stop;
uint32_t cnt;
SHT31 sht;
SHT31 sht; // use default address and Wire
void setup()
@ -25,8 +25,8 @@ void setup()
Serial.println(SHT31_LIB_VERSION);
Wire.begin();
sht.begin(SHT31_ADDRESS);
Wire.setClock(100000);
sht.begin();
uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
@ -42,9 +42,9 @@ void loop()
if (sht.dataReady())
{
start = micros();
bool success = sht.readData(); // default = true = fast
bool success = sht.readData(); // default = true = fast
stop = micros();
sht.requestData(); // request for next sample
sht.requestData(); // request for next sample
Serial.print("\t");
Serial.print(stop - start);
@ -63,9 +63,9 @@ void loop()
cnt = 0;
}
}
cnt++; // simulate other activity
cnt++; // simulate other activity
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -24,8 +24,8 @@ void setup()
Serial.println(SHT31_LIB_VERSION);
Wire.begin();
sht.begin(SHT31_ADDRESS);
Wire.setClock(100000);
sht.begin();
uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
@ -36,7 +36,7 @@ void setup()
void loop()
{
start = micros();
sht.read(); // default = true/fast slow = false
sht.read(); // default = true/fast slow = false
stop = micros();
Serial.print("\t");
@ -49,5 +49,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -10,7 +10,7 @@
#define SHT31_ADDRESS 0x44
SHT31 sht;
SHT31 sht(SHT31_ADDRESS);
uint16_t status;
@ -22,10 +22,10 @@ void setup()
Serial.println(SHT31_LIB_VERSION);
Wire.begin();
sht.begin(SHT31_ADDRESS);
Wire.setClock(100000);
sht.begin();
sht.setHeatTimeout(30); // heater timeout 30 seconds, just for demo.
sht.setHeatTimeout(30); // heater timeout 30 seconds, just for demo.
status = sht.readStatus();
printHeaterStatus(status);
@ -47,7 +47,7 @@ void setup()
void loop()
{
// forced switch off
// forced switch off
if (status & SHT31_STATUS_HEATER_ON) sht.heatOff();
}
@ -65,5 +65,5 @@ void printHeaterStatus(uint16_t status)
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -13,7 +13,8 @@
uint32_t start;
uint32_t stop;
SHT31 sht;
SHT31 sht; // use default address and Wire
uint32_t connectionFails = 0;
@ -25,8 +26,8 @@ void setup()
Serial.println(SHT31_LIB_VERSION);
Wire.begin();
sht.begin(SHT31_ADDRESS);
Wire.setClock(100000);
sht.begin();
uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
@ -39,7 +40,7 @@ void loop()
if ( sht.isConnected() )
{
start = micros();
bool b = sht.read(); // default = true/fast slow = false
bool b = sht.read(); // default = true/fast slow = false
stop = micros();
int error = sht.getError();
@ -72,5 +73,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -13,7 +13,7 @@
uint32_t start;
uint32_t stop;
SHT31 sht;
SHT31 sht(SHT31_ADDRESS, &Wire); // use explicit address and Wire
void setup()
@ -24,8 +24,8 @@ void setup()
Serial.println(SHT31_LIB_VERSION);
Wire.begin();
sht.begin(SHT31_ADDRESS);
Wire.setClock(100000);
sht.begin();
uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
@ -35,7 +35,7 @@ void setup()
void loop()
{
sht.read(); // default = true/fast slow = false
sht.read(); // default = true/fast slow = false
Serial.print("\t");
Serial.print(sht.lastRead());
Serial.print("\t");
@ -46,5 +46,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -14,7 +14,7 @@ uint32_t start;
uint32_t stop;
uint32_t cnt;
SHT31 sht;
SHT31 sht(SHT31_ADDRESS); // uses explicit address
void setup()
@ -25,8 +25,8 @@ void setup()
Serial.println(SHT31_LIB_VERSION);
Wire.begin();
sht.begin(SHT31_ADDRESS);
Wire.setClock(100000);
sht.begin();
uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
@ -45,9 +45,9 @@ void loop()
if (sht.dataReady())
{
start = micros();
bool success = sht.readData(); // default = true = fast
bool success = sht.readData(); // default = true = fast
stop = micros();
sht.requestData(); // request for next sample
sht.requestData(); // request for next sample
Serial.print("\t");
Serial.print(stop - start);
@ -62,19 +62,23 @@ void loop()
rawHumidity = sht.getRawHumidity();
Serial.print(rawTemperature, HEX);
Serial.print(" = ");
Serial.print(rawTemperature * (175.0 / 65535) - 45, 1); // This formula comes from page 14 of the SHT31 datasheet
// This formula comes from page 14 of the SHT31 datasheet
Serial.print(rawTemperature * (175.0 / 65535) - 45, 1);
Serial.print("°C\t");
Serial.print(sht.getRawHumidity(), HEX);
Serial.print(" = ");
Serial.print(rawHumidity * (100.0 / 65535), 1); // This formula comes from page 14 of the SHT31 datasheet
// This formula comes from page 14 of the SHT31 datasheet
Serial.print(rawHumidity * (100.0 / 65535), 1);
Serial.print("%\t");
Serial.println(cnt);
cnt = 0;
}
}
cnt++; // simulate other activity
cnt++; // simulate other activity
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -24,8 +24,8 @@ void setup()
Serial.println(SHT31_LIB_VERSION);
Wire.begin();
sht.begin(SHT31_ADDRESS);
Wire.setClock(100000);
sht.begin();
uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
@ -36,7 +36,7 @@ void setup()
void loop()
{
start = micros();
sht.read(false); // default = true/fast slow = false
sht.read(false); // default = true/fast slow = false
stop = micros();
Serial.print("\t");
Serial.print(stop - start);
@ -48,5 +48,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -13,7 +13,7 @@
// TwoWire myWire(&sercom5, 0, 1);
TwoWire myWire = Wire1; // test.
TwoWire myWire = Wire1; // test.
// note: address reuse on second I2C bus
@ -23,10 +23,10 @@ TwoWire myWire = Wire1; // test.
#define SHT31_ADDRESS_4 0x45
SHT31 sht_1(&Wire);
SHT31 sht_2(&Wire);
SHT31 sht_3(&myWire);
SHT31 sht_4(&myWire);
SHT31 sht_1(SHT31_ADDRESS_1, &Wire);
SHT31 sht_2(SHT31_ADDRESS_2, &Wire);
SHT31 sht_3(SHT31_ADDRESS_3, &myWire);
SHT31 sht_4(SHT31_ADDRESS_4, &myWire);
bool b1, b2, b3, b4;
@ -44,16 +44,16 @@ void setup()
myWire.begin();
myWire.setClock(100000);
// see datasheet for details
// pinPeripheral(0, PIO_SERCOM_ALT);
// pinPeripheral(1, PIO_SERCOM_ALT);
// see datasheet for details
// pinPeripheral(0, PIO_SERCOM_ALT);
// pinPeripheral(1, PIO_SERCOM_ALT);
b1 = sht_1.begin(SHT31_ADDRESS_1);
b2 = sht_2.begin(SHT31_ADDRESS_2);
b3 = sht_3.begin(SHT31_ADDRESS_3);
b4 = sht_4.begin(SHT31_ADDRESS_4);
b1 = sht_1.begin();
b2 = sht_2.begin();
b3 = sht_3.begin();
b4 = sht_4.begin();
// see if they are connected
// see if they are connected
Serial.print("BEGIN:\t");
Serial.print(b1);
Serial.print("\t");
@ -69,7 +69,7 @@ void setup()
void loop()
{
// read all sensors that are found
// read all sensors that are found
if (b1) sht_1.read();
if (b2) sht_2.read();
if (b3) sht_3.read();
@ -96,4 +96,4 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -17,8 +17,13 @@ TwoWire myWire(&sercom5, 0, 1);
// TwoWire myWire = Wire1;
uint8_t addr[4] = { 0x44, 0x45, 0x44, 0x45 };
SHT31 sht[4] = { SHT31(&Wire), SHT31(&Wire), SHT31(&myWire), SHT31(&myWire) };
SHT31 sht[4] = {
SHT31(0x44, &Wire),
SHT31(0x45, &Wire),
SHT31(0x44, &myWire),
SHT31(0x45, &myWire)
};
bool b[4];
@ -34,19 +39,15 @@ void setup()
myWire.begin();
myWire.setClock(100000);
// see datasheet for details
// see datasheet for details
pinPeripheral(0, PIO_SERCOM_ALT);
pinPeripheral(1, PIO_SERCOM_ALT);
for (uint8_t i = 0; i < 4; i++)
{
b[i] = sht[i].begin(addr[i]);
}
// see if they are connected
// show they are connected
Serial.print("BEGIN:\t");
for (uint8_t i = 0; i < 4; i++)
{
b[i] = sht[i].begin();
Serial.print(b[i]);
Serial.print("\t");
}
@ -56,7 +57,7 @@ void setup()
void loop()
{
// read all that are found
// read all that are found
for (uint8_t i = 0; i < 4; i++)
{
if (b[i]) sht[i].read();
@ -78,4 +79,4 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

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

View File

@ -1,5 +1,5 @@
name=SHT31
version=0.4.0
version=0.5.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the SHT31 temperature and humidity sensor

View File

@ -83,9 +83,11 @@ unittest(test_constants_2)
unittest(test_begin)
{
SHT31 sht;
SHT31 sht(0x44);
bool b = sht.begin(0x44);
Wire.begin();
bool b = sht.begin();
assertEqual(b, true);
assertTrue(sht.reset());
@ -107,8 +109,11 @@ unittest(test_begin)
unittest(test_read)
{
SHT31 sht;
bool b = sht.begin(0x44);
SHT31 sht(0x44);
Wire.begin();
bool b = sht.begin();
assertEqual(b, true);
assertTrue(sht.isConnected());
@ -137,8 +142,11 @@ unittest(test_read)
unittest(test_readStatus)
{
SHT31 sht;
bool b = sht.begin(0x44);
SHT31 sht(0x44);
Wire.begin();
bool b = sht.begin();
assertEqual(b, true);
assertEqual(0xFFFF, sht.readStatus());
@ -149,8 +157,11 @@ unittest(test_readStatus)
unittest(test_heater)
{
SHT31 sht;
bool b = sht.begin(0x44);
SHT31 sht(0x44);
Wire.begin();
bool b = sht.begin();
assertEqual(b, true);
assertTrue(sht.heatOn());
@ -169,8 +180,11 @@ unittest(test_heater)
unittest(test_async)
{
SHT31 sht;
bool b = sht.begin(0x44);
SHT31 sht(0x44);
Wire.begin();
bool b = sht.begin();
assertEqual(b, true);
assertTrue(sht.requestData());
@ -197,4 +211,4 @@ unittest(test_async)
unittest_main()
// --------
// -- END OF FILE --