Skip to content

Commit 8eb4fbc

Browse files
committed
0.2.0 SHT85
1 parent f054543 commit 8eb4fbc

File tree

9 files changed

+128
-50
lines changed

9 files changed

+128
-50
lines changed

libraries/SHT85/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ https://github.com/hawesg/SHT31D_Particle_Photon_ClosedCube
7070

7171
#### Base interface
7272

73+
- **SHT()** constructor of the base class. **getType()** will return 0.
74+
- **SHT30()** constructor.
75+
- **SHT31()** constructor.
76+
- **SHT35()** constructor.
7377
- **SHT85()** constructor.
78+
- **uint8_t getType()** returns numeric part of sensor type.
7479
- **begin(address, dataPin, clockPin)** begin function for ESP8266 & ESP32; **WARNING: not verified yet**
7580
returns false if device address is incorrect or device cannot be reset.
7681
- **bool begin(address, TwoWire \*wire = &Wire)** for platforms with multiple I2C busses.
@@ -80,8 +85,9 @@ Does read both the temperature and humidity.
8085
- **uint16_t readStatus()** details see datasheet and **Status fields** below.
8186
- **uint32_t lastRead()** in milliSeconds since start of program.
8287
- **bool reset(bool hard = false)** resets the sensor, soft reset by default. Returns false if fails.
83-
- **float getHumidity()** computes the relative humidity in % based off the latest raw reading, and returns it.
84-
- **float getTemperature()** computes the temperature in °C based off the latest raw reading, and returns it.
88+
- **float getHumidity()** computes the relative humidity in % based on the latest raw reading, and returns it.
89+
- **float getTemperature()** computes the temperature in °C based on the latest raw reading, and returns it.
90+
- **float getFahrenheit()** computes the temperature in °F based on the latest raw reading, and returns it.
8591
- **uint16_t getRawHumidity()** returns the raw two-byte representation of humidity directly from the sensor.
8692
- **uint16_t getRawTemperature()** returns the raw two-byte representation of temperature directly from the sensor.
8793

@@ -90,7 +96,7 @@ Note that the temperature and humidity values are recalculated on every call to
9096

9197
#### Error interface
9298

93-
- **getError()** returns last set error flag and clear it.
99+
- **int getError()** returns last set error flag and clear it.
94100
Be sure to clear the error flag by calling **getError()** before calling any command as the error flag could be from a previous command.
95101

96102
| Error | Symbolic | Description
@@ -104,7 +110,7 @@ Be sure to clear the error flag by calling **getError()** before calling any com
104110
| 0x86 | SHT_ERR_CRC_HUM | CRC error in humidity |
105111
| 0x87 | SHT_ERR_CRC_STATUS | CRC error in statusfield |
106112
| 0x88 | SHT_ERR_HEATER_COOLDOWN | Heater need to cool down |
107-
| 0x88 | SHT_ERR_HEATER_ON | Could not switch on heater |
113+
| 0x89 | SHT_ERR_HEATER_ON | Could not switch on heater |
108114

109115

110116
#### Heater interface
@@ -136,7 +142,7 @@ Will switch the heater off if max heating time has passed.
136142

137143
See async example for usage
138144

139-
- **bool requestData()** requests a new measurement. Returns false if this fails.
145+
- **bool requestData()** requests a new measurement. Returns false if the request fails.
140146
- **bool dataReady()** checks if enough time has passed to read the data. (15 milliseconds)
141147
- **bool readData(bool fast = true)** fast = true skips the CRC check.
142148
Returns false if reading fails or in case of a CRC failure.
@@ -169,11 +175,8 @@ Returns false if reading fails or in case of a CRC failure.
169175
## Future
170176

171177
- verify working with ESP32
172-
- merge with other SHT sensors if possible
173-
- SHT_BASE class ?
174178
- investigate command ART (auto sampling at 4 Hz)
175179
- investigate command BREAK (stop auto sampling)
176-
- direct Fahrenheit formula ?
177180
- improve error handling / status. (all code paths)
178181

179182

libraries/SHT85/SHT85.cpp

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT85.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.4
4+
// VERSION: 0.2.0
55
// DATE: 2021-02-10
66
// PURPOSE: Arduino library for the SHT85 temperature and humidity sensor
77
// https://nl.rs-online.com/web/p/temperature-humidity-sensor-ics/1826530
@@ -15,6 +15,8 @@
1515
// 0.1.3 2021-08-06 expose raw data from sensor
1616
// 0.1.4 2021-08-24 prevent heater to switch on too fast.
1717
// update readme
18+
// 0.2.0 2021-08-24 split off base class
19+
// create derived classes SHT85, 30, 31, 35
1820

1921

2022
#include "SHT85.h"
@@ -34,7 +36,8 @@
3436
#define SHT_HEAT_OFF 0x3066
3537
#define SHT_HEATER_TIMEOUT 180000UL // milliseconds
3638

37-
SHT85::SHT85()
39+
40+
SHT::SHT()
3841
{
3942
_address = 0;
4043
_lastRead = 0;
@@ -45,11 +48,12 @@ SHT85::SHT85()
4548
_heaterStop = 0;
4649
_heaterOn = false;
4750
_error = SHT_OK;
51+
_type = 0;
4852
}
4953

5054

5155
#if defined(ESP8266) || defined(ESP32)
52-
bool SHT85::begin(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
56+
bool SHT::begin(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
5357
{
5458
if ((address != 0x44) && (address != 0x45))
5559
{
@@ -69,7 +73,7 @@ bool SHT85::begin(const uint8_t address, const uint8_t dataPin, const uint8_t cl
6973
#endif
7074

7175

72-
bool SHT85::begin(const uint8_t address, TwoWire *wire)
76+
bool SHT::begin(const uint8_t address, TwoWire *wire)
7377
{
7478
if ((address != 0x44) && (address != 0x45))
7579
{
@@ -82,7 +86,7 @@ bool SHT85::begin(const uint8_t address, TwoWire *wire)
8286
}
8387

8488

85-
bool SHT85::read(bool fast)
89+
bool SHT::read(bool fast)
8690
{
8791
if (writeCmd(fast ? SHT_MEASUREMENT_FAST : SHT_MEASUREMENT_SLOW) == false)
8892
{
@@ -93,7 +97,7 @@ bool SHT85::read(bool fast)
9397
}
9498

9599

96-
bool SHT85::isConnected()
100+
bool SHT::isConnected()
97101
{
98102
_wire->beginTransmission(_address);
99103
int rv = _wire->endTransmission();
@@ -132,7 +136,7 @@ bool SHT85::isConnected()
132136
#endif
133137

134138

135-
uint16_t SHT85::readStatus()
139+
uint16_t SHT::readStatus()
136140
{
137141
uint8_t status[3] = { 0, 0, 0 };
138142
// page 13 datasheet
@@ -156,7 +160,7 @@ uint16_t SHT85::readStatus()
156160
}
157161

158162

159-
bool SHT85::reset(bool hard)
163+
bool SHT::reset(bool hard)
160164
{
161165
bool b = writeCmd(hard ? SHT_HARD_RESET : SHT_SOFT_RESET);
162166
if (b == false)
@@ -168,14 +172,14 @@ bool SHT85::reset(bool hard)
168172
}
169173

170174

171-
void SHT85::setHeatTimeout(uint8_t seconds)
175+
void SHT::setHeatTimeout(uint8_t seconds)
172176
{
173177
_heatTimeout = seconds;
174178
if (_heatTimeout > 180) _heatTimeout = 180;
175179
}
176180

177181

178-
bool SHT85::heatOn()
182+
bool SHT::heatOn()
179183
{
180184
if (isHeaterOn()) return true;
181185
if ((_heaterStop > 0) && (millis() - _heaterStop < SHT_HEATER_TIMEOUT))
@@ -194,7 +198,7 @@ bool SHT85::heatOn()
194198
}
195199

196200

197-
bool SHT85::heatOff()
201+
bool SHT::heatOff()
198202
{
199203
// always switch off the heater - ignore _heaterOn flag.
200204
if (writeCmd(SHT_HEAT_OFF) == false)
@@ -208,7 +212,7 @@ bool SHT85::heatOff()
208212
}
209213

210214

211-
bool SHT85::isHeaterOn()
215+
bool SHT::isHeaterOn()
212216
{
213217
if (_heaterOn == false)
214218
{
@@ -224,7 +228,7 @@ bool SHT85::isHeaterOn()
224228
}
225229

226230

227-
bool SHT85::requestData()
231+
bool SHT::requestData()
228232
{
229233
if (writeCmd(SHT_MEASUREMENT_SLOW) == false)
230234
{
@@ -235,13 +239,13 @@ bool SHT85::requestData()
235239
}
236240

237241

238-
bool SHT85::dataReady()
242+
bool SHT::dataReady()
239243
{
240244
return ((millis() - _lastRequest) > 15); // TODO MAGIC NR
241245
}
242246

243247

244-
bool SHT85::readData(bool fast)
248+
bool SHT::readData(bool fast)
245249
{
246250
uint8_t buffer[6];
247251
if (readBytes(6, (uint8_t*) &buffer[0]) == false)
@@ -272,7 +276,7 @@ bool SHT85::readData(bool fast)
272276
}
273277

274278

275-
int SHT85::getError()
279+
int SHT::getError()
276280
{
277281
int rv = _error;
278282
_error = SHT_OK;
@@ -282,7 +286,7 @@ int SHT85::getError()
282286

283287
//////////////////////////////////////////////////////////
284288

285-
uint8_t SHT85::crc8(const uint8_t *data, uint8_t len)
289+
uint8_t SHT::crc8(const uint8_t *data, uint8_t len)
286290
{
287291
// CRC-8 formula from page 14 of SHT spec pdf
288292
const uint8_t POLY(0x31);
@@ -301,7 +305,7 @@ uint8_t SHT85::crc8(const uint8_t *data, uint8_t len)
301305
}
302306

303307

304-
bool SHT85::writeCmd(uint16_t cmd)
308+
bool SHT::writeCmd(uint16_t cmd)
305309
{
306310
_wire->beginTransmission(_address);
307311
_wire->write(cmd >> 8 );
@@ -315,7 +319,7 @@ bool SHT85::writeCmd(uint16_t cmd)
315319
}
316320

317321

318-
bool SHT85::readBytes(uint8_t n, uint8_t *val)
322+
bool SHT::readBytes(uint8_t n, uint8_t *val)
319323
{
320324
int rv = _wire->requestFrom(_address, (uint8_t) n);
321325
if (rv == n)
@@ -330,4 +334,34 @@ bool SHT85::readBytes(uint8_t n, uint8_t *val)
330334
return false;
331335
}
332336

337+
338+
339+
////////////////////////////////////////////////////////
340+
//
341+
// DERIVED
342+
//
343+
SHT30::SHT30()
344+
{
345+
_type = 30;
346+
};
347+
348+
349+
SHT31::SHT31()
350+
{
351+
_type = 31;
352+
};
353+
354+
355+
SHT35::SHT35()
356+
{
357+
_type = 35;
358+
};
359+
360+
361+
SHT85::SHT85()
362+
{
363+
_type = 85;
364+
};
365+
366+
333367
// -- END OF FILE --

libraries/SHT85/SHT85.h

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: SHT85.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.4
5+
// VERSION: 0.2.0
66
// DATE: 2021-02-10
77
// PURPOSE: Arduino library for the SHT85 temperature and humidity sensor
88
// https://nl.rs-online.com/web/p/temperature-humidity-sensor-ics/1826530
@@ -25,7 +25,8 @@
2525
#include "Wire.h"
2626

2727

28-
#define SHT85_LIB_VERSION (F("0.1.4"))
28+
#define SHT_LIB_VERSION (F("0.2.0"))
29+
#define SHT85_LIB_VERSION SHT_LIB_VERSION
2930

3031

3132
// fields readStatus
@@ -50,16 +51,18 @@
5051
#define SHT_ERR_HEATER_ON 0x89
5152

5253

53-
class SHT85
54+
class SHT
5455
{
5556
public:
56-
SHT85();
57+
SHT();
5758

5859
#if defined(ESP8266) || defined(ESP32)
5960
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
6061
#endif
6162
bool begin(const uint8_t address, TwoWire *wire = &Wire);
6263

64+
uint8_t getType() { return _type; };
65+
6366
// blocks 15 milliseconds + actual read + math
6467
bool read(bool fast = true);
6568

@@ -79,14 +82,14 @@ class SHT85
7982
// and let it cool down at least 3 minutes.
8083
void setHeatTimeout(uint8_t seconds);
8184
uint8_t getHeatTimeout() { return _heatTimeout; };
82-
;
85+
8386
bool heatOn();
8487
bool heatOff();
85-
bool isHeaterOn(); // is the sensor still heating up?
88+
bool isHeaterOn(); // is the sensor still heating up?
8689

8790
float getHumidity() { return _rawHumidity * (100.0 / 65535); };
8891
float getTemperature() { return _rawTemperature * (175.0 / 65535) - 45; };
89-
// float getFahrenheit() { return _rawTemperature * (63.0 /13107.0) - 49; };
92+
float getFahrenheit() { return _rawTemperature * (63.0 /13107.0) - 49; };
9093
uint16_t getRawHumidity() {return _rawHumidity; };
9194
uint16_t getRawTemperature() {return _rawTemperature; };
9295

@@ -98,7 +101,7 @@ class SHT85
98101
int getError(); // clears error flag
99102

100103

101-
private:
104+
protected:
102105
uint8_t crc8(const uint8_t *data, uint8_t len);
103106
bool writeCmd(uint16_t cmd);
104107
bool readBytes(uint8_t n, uint8_t *val);
@@ -111,11 +114,47 @@ class SHT85
111114
uint32_t _heaterStart;
112115
uint32_t _heaterStop;
113116
bool _heaterOn;
117+
uint8_t _type;
114118

115119
uint16_t _rawHumidity;
116120
uint16_t _rawTemperature;
117121

118122
uint8_t _error;
119123
};
120124

125+
126+
127+
////////////////////////////////////////////////////////
128+
//
129+
// DERIVED
130+
//
131+
class SHT30 : public SHT
132+
{
133+
public:
134+
SHT30();
135+
};
136+
137+
138+
class SHT31 : public SHT
139+
{
140+
public:
141+
SHT31();
142+
};
143+
144+
145+
class SHT35 : public SHT
146+
{
147+
public:
148+
SHT35();
149+
};
150+
151+
152+
class SHT85 : public SHT
153+
{
154+
public:
155+
SHT85();
156+
};
157+
158+
159+
121160
// -- END OF FILE --

0 commit comments

Comments
 (0)