Skip to content

Commit e5361f6

Browse files
authored
refactor API, begin + constructor (#40)
- refactor API, constructor and begin() - add **getAddress()** - update readme.md - update examples - minor edits
1 parent 62b5078 commit e5361f6

File tree

17 files changed

+162
-140
lines changed

17 files changed

+162
-140
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.5.0] - 2023-12-09
10+
- refactor API, constructor and begin()
11+
- add **getAddress()**
12+
- update readme.md
13+
- update examples
14+
- minor edits
15+
16+
----
17+
918
## [0.4.0] - 2023-09-21
1019
- fix #38 support for Wire1 for ESP32
1120
- update examples

README.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,43 @@ A derived class for using the SHT31 sensor with SoftWire (soft I2C) can be found
4040
- https://github.com/RobTillaart/SHT31_SW
4141

4242

43+
#### 0.5.0 Breaking change
44+
45+
Version 0.5.0 introduced a breaking change.
46+
You cannot set the pins in **begin()** any more.
47+
This reduces the dependency of processor dependent Wire implementations.
48+
The user has to call **Wire.begin()** and can optionally set the Wire pins
49+
before calling **begin()**.
50+
51+
52+
#### Related
53+
54+
- https://github.com/RobTillaart/SHT31
55+
- https://github.com/RobTillaart/SHT31_SW
56+
- https://github.com/RobTillaart/SHT31_SWW
57+
- https://github.com/RobTillaart/SHT85
58+
59+
4360
## Interface
4461

4562
```cpp
4663
#include "SHT31.h"
4764
```
4865

66+
#### Constructor
4967

50-
#### Base interface
51-
52-
- **SHT31(TwoWire \*wire = &Wire)** constructor. Optional select the I2C bus (Wire, Wire1 etc).
53-
- **bool begin(uint8_t address, uint8_t dataPin, uint8_t clockPin)** begin function for ESP8266 & ESP32;
54-
returns false if device address is incorrect or device cannot be reset.
55-
- **bool begin(uint8_t dataPin, uint8_t clockPin)** same as above. With default SHT_DEFAULT_ADDRESS.
56-
- **bool begin(uint8_t address = SHT_DEFAULT_ADDRESS)**
68+
- **SHT31(uint8_t address = SHT_DEFAULT_ADDRESS, TwoWire \*wire = &Wire)** constructor.
69+
Optional select address and the I2C bus (Wire, Wire1 etc).
70+
- **bool begin()**
5771
Returns false if device address is incorrect or device cannot be reset.
72+
- **bool isConnected()** check sensor is reachable over I2C. Returns false if not connected.
73+
- **uint8_t getAddress()** returns address set in the constructor.
74+
75+
76+
#### Read
77+
5878
- **bool read(bool fast = true)** blocks 4 (fast) or 15 (slow) milliseconds + actual read + math.
5979
Does read both the temperature and humidity.
60-
- **bool isConnected()** check sensor is reachable over I2C. Returns false if not connected.
6180
- **uint16_t readStatus()** details see datasheet and **Status fields** below.
6281
- **uint32_t lastRead()** in milliSeconds since start of program.
6382
- **bool reset(bool hard = false)** resets the sensor, soft reset by default. Returns false if it fails.

SHT31.cpp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT31.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.4.0
4+
// VERSION: 0.5.0
55
// DATE: 2019-02-08
66
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
77
// https://www.adafruit.com/product/2857
@@ -26,10 +26,10 @@
2626
#define SHT31_HEATER_TIMEOUT 180000UL // milliseconds
2727

2828

29-
SHT31::SHT31(TwoWire *wire)
29+
SHT31::SHT31(uint8_t address, TwoWire *wire)
3030
{
3131
_wire = wire;
32-
_address = 0;
32+
_address = address;
3333
_lastRead = 0;
3434
_rawTemperature = 0;
3535
_rawHumidity = 0;
@@ -41,41 +41,28 @@ SHT31::SHT31(TwoWire *wire)
4141
}
4242

4343

44-
#if defined(ESP8266) || defined(ESP32)
45-
bool SHT31::begin(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
44+
bool SHT31::begin()
4645
{
47-
if ((address != 0x44) && (address != 0x45))
46+
if ((_address != 0x44) && (_address != 0x45))
4847
{
4948
return false;
5049
}
51-
_address = address;
52-
53-
if ((dataPin < 255) && (clockPin < 255))
54-
{
55-
_wire->begin(dataPin, clockPin);
56-
} else {
57-
_wire->begin();
58-
}
5950
return reset();
6051
}
6152

6253

63-
bool SHT31::begin(const uint8_t dataPin, const uint8_t clockPin)
54+
bool SHT31::isConnected()
6455
{
65-
return begin(SHT_DEFAULT_ADDRESS, dataPin, clockPin);
56+
_wire->beginTransmission(_address);
57+
int rv = _wire->endTransmission();
58+
if (rv != 0) _error = SHT31_ERR_NOT_CONNECT;
59+
return (rv == 0);
6660
}
67-
#endif
6861

6962

70-
bool SHT31::begin(const uint8_t address)
63+
uint8_t SHT31::getAddress()
7164
{
72-
if ((address != 0x44) && (address != 0x45))
73-
{
74-
return false;
75-
}
76-
_address = address;
77-
_wire->begin();
78-
return reset();
65+
return _address;
7966
}
8067

8168

@@ -90,14 +77,6 @@ bool SHT31::read(bool fast)
9077
}
9178

9279

93-
bool SHT31::isConnected()
94-
{
95-
_wire->beginTransmission(_address);
96-
int rv = _wire->endTransmission();
97-
if (rv != 0) _error = SHT31_ERR_NOT_CONNECT;
98-
return (rv == 0);
99-
}
100-
10180
#ifdef doc
10281
// bit - description
10382
// ==================

SHT31.h

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: SHT31.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.4.0
5+
// VERSION: 0.5.0
66
// DATE: 2019-02-08
77
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
88
// https://www.adafruit.com/product/2857
@@ -13,7 +13,7 @@
1313
#include "Wire.h"
1414

1515

16-
#define SHT31_LIB_VERSION (F("0.4.0"))
16+
#define SHT31_LIB_VERSION (F("0.5.0"))
1717

1818
#ifndef SHT_DEFAULT_ADDRESS
1919
#define SHT_DEFAULT_ADDRESS 0x44
@@ -44,21 +44,17 @@
4444
class SHT31
4545
{
4646
public:
47-
SHT31(TwoWire *wire = &Wire);
47+
SHT31(uint8_t address = SHT_DEFAULT_ADDRESS, TwoWire *wire = &Wire);
4848

49-
#if defined(ESP8266) || defined(ESP32)
50-
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
51-
// use SHT_DEFAULT_ADDRESS
52-
bool begin(const uint8_t dataPin, const uint8_t clockPin);
53-
#endif
54-
bool begin(const uint8_t address = SHT_DEFAULT_ADDRESS);
55-
56-
// blocks 15 milliseconds + actual read + math
57-
bool read(bool fast = true);
49+
bool begin();
50+
uint8_t getAddress();
5851

5952
// check sensor is reachable over I2C
6053
virtual bool isConnected();
6154

55+
// blocks 15 milliseconds + actual read + math
56+
bool read(bool fast = true);
57+
6258
// details see datasheet; summary in SHT31.cpp file
6359
uint16_t readStatus();
6460

@@ -92,18 +88,16 @@ class SHT31
9288
int getError(); // clears error flag
9389

9490
protected:
95-
uint8_t _address;
96-
uint8_t _heatTimeout; // seconds
97-
uint32_t _lastRead;
98-
uint32_t _lastRequest; // for async interface
99-
uint32_t _heaterStart;
100-
uint32_t _heaterStop;
101-
bool _heaterOn;
102-
91+
uint8_t _address;
92+
uint8_t _heatTimeout; // seconds
93+
uint32_t _lastRead;
94+
uint32_t _lastRequest; // for async interface
95+
uint32_t _heaterStart;
96+
uint32_t _heaterStop;
97+
bool _heaterOn;
10398
uint16_t _rawHumidity;
10499
uint16_t _rawTemperature;
105-
106-
uint8_t _error;
100+
uint8_t _error;
107101

108102
private:
109103
uint8_t crc8(const uint8_t *data, uint8_t len);

examples/SHT31_I2Cspeed/SHT31_I2Cspeed.ino

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
uint32_t start;
1414
uint32_t stop;
1515

16-
SHT31 sht;
16+
SHT31 sht(SHT31_ADDRESS);
1717

1818

1919
void setup()
@@ -24,8 +24,8 @@ void setup()
2424
Serial.println(SHT31_LIB_VERSION);
2525

2626
Wire.begin();
27-
sht.begin(SHT31_ADDRESS);
2827
Wire.setClock(100000);
28+
sht.begin();
2929

3030
uint16_t stat = sht.readStatus();
3131
Serial.print(stat, HEX);
@@ -41,23 +41,24 @@ void loop()
4141
Wire.setClock(I2Cfreq);
4242
test();
4343
}
44+
Serial.println();
4445
}
4546

4647

4748
void test()
4849
{
4950
start = micros();
50-
sht.read(true); // default = true/fast slow = false
51+
sht.read(true); // default = true/fast slow = false
5152
stop = micros();
5253
Serial.print("\t");
5354
Serial.print(stop - start);
5455
Serial.print("\t");
5556
Serial.print(sht.getTemperature(), 1);
5657
Serial.print("\t");
5758
Serial.println(sht.getHumidity(), 1);
58-
delay(100);
59+
delay(1000);
5960
}
6061

6162

62-
// -- END OF FILE --
63+
// -- END OF FILE --
6364

examples/SHT31_async/SHT31_async.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ uint32_t start;
1414
uint32_t stop;
1515
uint32_t cnt;
1616

17-
SHT31 sht;
17+
SHT31 sht; // use default address and Wire
1818

1919

2020
void setup()
@@ -25,8 +25,8 @@ void setup()
2525
Serial.println(SHT31_LIB_VERSION);
2626

2727
Wire.begin();
28-
sht.begin(SHT31_ADDRESS);
2928
Wire.setClock(100000);
29+
sht.begin();
3030

3131
uint16_t stat = sht.readStatus();
3232
Serial.print(stat, HEX);
@@ -42,9 +42,9 @@ void loop()
4242
if (sht.dataReady())
4343
{
4444
start = micros();
45-
bool success = sht.readData(); // default = true = fast
45+
bool success = sht.readData(); // default = true = fast
4646
stop = micros();
47-
sht.requestData(); // request for next sample
47+
sht.requestData(); // request for next sample
4848

4949
Serial.print("\t");
5050
Serial.print(stop - start);
@@ -63,9 +63,9 @@ void loop()
6363
cnt = 0;
6464
}
6565
}
66-
cnt++; // simulate other activity
66+
cnt++; // simulate other activity
6767
}
6868

6969

70-
// -- END OF FILE --
70+
// -- END OF FILE --
7171

examples/SHT31_demo/SHT31_demo.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ void setup()
2424
Serial.println(SHT31_LIB_VERSION);
2525

2626
Wire.begin();
27-
sht.begin(SHT31_ADDRESS);
2827
Wire.setClock(100000);
28+
sht.begin();
2929

3030
uint16_t stat = sht.readStatus();
3131
Serial.print(stat, HEX);
@@ -36,7 +36,7 @@ void setup()
3636
void loop()
3737
{
3838
start = micros();
39-
sht.read(); // default = true/fast slow = false
39+
sht.read(); // default = true/fast slow = false
4040
stop = micros();
4141

4242
Serial.print("\t");
@@ -49,5 +49,5 @@ void loop()
4949
}
5050

5151

52-
// -- END OF FILE --
52+
// -- END OF FILE --
5353

examples/SHT31_heater/SHT31_heater.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#define SHT31_ADDRESS 0x44
1212

13-
SHT31 sht;
13+
SHT31 sht(SHT31_ADDRESS);
1414
uint16_t status;
1515

1616

@@ -22,10 +22,10 @@ void setup()
2222
Serial.println(SHT31_LIB_VERSION);
2323

2424
Wire.begin();
25-
sht.begin(SHT31_ADDRESS);
2625
Wire.setClock(100000);
26+
sht.begin();
2727

28-
sht.setHeatTimeout(30); // heater timeout 30 seconds, just for demo.
28+
sht.setHeatTimeout(30); // heater timeout 30 seconds, just for demo.
2929

3030
status = sht.readStatus();
3131
printHeaterStatus(status);
@@ -47,7 +47,7 @@ void setup()
4747

4848
void loop()
4949
{
50-
// forced switch off
50+
// forced switch off
5151
if (status & SHT31_STATUS_HEATER_ON) sht.heatOff();
5252
}
5353

@@ -65,5 +65,5 @@ void printHeaterStatus(uint16_t status)
6565
}
6666

6767

68-
// -- END OF FILE --
68+
// -- END OF FILE --
6969

0 commit comments

Comments
 (0)