Skip to content

Commit 2845e4b

Browse files
authored
sync with SHT85 (#30)
1 parent 4b358c7 commit 2845e4b

File tree

5 files changed

+46
-38
lines changed

5 files changed

+46
-38
lines changed

README.md

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,21 @@ https://github.com/hawesg/SHT31D_Particle_Photon_ClosedCube
3737
#### Base interface
3838

3939
- **SHT31()** constructor.
40-
- **bool begin(uint8_taddress, uint8_tdataPin, uint8_tclockPin)** begin function for ESP8266 & ESP32;
40+
- **bool begin(uint8_t address, uint8_t dataPin, uint8_t clockPin)** begin function for ESP8266 & ESP32;
4141
returns false if device address is incorrect or device cannot be reset.
42-
- **bool begin(uint8_t address)** for single I2C bus platforms, e.g UNO.
43-
- **bool begin(uint8_t address, TwoWire \*wire)** for platforms with multiple I2C buses.
42+
- **bool begin(uint8_t dataPin, uint8_t clockPin)** same as above. With default SHT_DEFAULT_ADDRESS.
43+
- **bool begin(uint8_t address, TwoWire \*wire = &Wire)** for platforms with multiple I2C buses.
44+
- **bool begin(TwoWire \*wire = &Wire)** same as above.
45+
With default SHT_DEFAULT_ADDRESS.
4446
- **bool read(bool fast = true)** blocks 4 (fast) or 15 (slow) milliseconds + actual read + math.
4547
Does read both the temperature and humidity.
4648
- **bool isConnected()** check sensor is reachable over I2C. Returns false if not connected.
4749
- **uint16_t readStatus()** details see datasheet and **Status fields** below.
4850
- **uint32_t lastRead()** in milliSeconds since start of program.
49-
- **bool reset(bool hard = false)** resets the sensor, soft reset by default. Returns false if fails.
50-
- **float getHumidity()** computes the relative humidity in % based off the latest raw reading, and returns it.
51-
- **float getTemperature()** computes the temperature in °C based off the latest raw reading, and returns it.
51+
- **bool reset(bool hard = false)** resets the sensor, soft reset by default. Returns false if it fails.
52+
- **float getHumidity()** computes the relative humidity in % based on the latest raw reading, and returns it.
53+
- **float getTemperature()** computes the temperature in °C based on the latest raw reading, and returns it.
54+
- **float getFahrenheit()** computes the temperature in °F based on the latest raw reading, and returns it..
5255
- **uint16_t getRawHumidity()** returns the raw two-byte representation of humidity directly from the sensor.
5356
- **uint16_t getRawTemperature()** returns the raw two-byte representation of temperature directly from the sensor.
5457

@@ -144,16 +147,6 @@ See examples.
144147

145148
## Future
146149

147-
- merge with other SHT sensors if possible
148-
- direct Fahrenheit formula ?
149-
- improve error handling / status. (all code paths)
150-
- verify working with ESP32
151-
- investigate command ART (auto sampling at 4 Hz)
152-
- investigate command BREAK (stop auto sampling)
153-
- software I2C experiments?
154-
- separate releaseNotes.md
155-
156-
157-
#### Wont
150+
- keep in sync with SHT85 library
158151

159152

SHT31.cpp

Lines changed: 16 additions & 8 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.3.5
4+
// VERSION: 0.3.6
55
// DATE: 2019-02-08
66
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
77
// https://www.adafruit.com/product/2857
@@ -30,6 +30,7 @@
3030
// update readme
3131
// 0.3.4 2021-09-19 update build-CI
3232
// 0.3.5 2021-12-28 update library.json, readme, license, minor edits
33+
// 0.3.6 2022-01-18 sync with SHT85 lib
3334

3435

3536
#include "SHT31.h"
@@ -49,12 +50,13 @@
4950
#define SHT31_HEAT_OFF 0x3066
5051
#define SHT31_HEATER_TIMEOUT 180000UL // milliseconds
5152

53+
5254
SHT31::SHT31()
5355
{
5456
_address = 0;
5557
_lastRead = 0;
56-
rawTemperature = 0;
57-
rawHumidity = 0;
58+
_rawTemperature = 0;
59+
_rawHumidity = 0;
5860
_heatTimeout = 0;
5961
_heaterStart = 0;
6062
_heaterStop = 0;
@@ -81,13 +83,13 @@ bool SHT31::begin(const uint8_t address, const uint8_t dataPin, const uint8_t cl
8183
}
8284
return reset();
8385
}
84-
#endif
8586

8687

87-
bool SHT31::begin(const uint8_t address)
88+
bool SHT31::begin(const uint8_t dataPin, const uint8_t clockPin)
8889
{
89-
return begin(address, &Wire);
90+
return begin(SHT_DEFAULT_ADDRESS, dataPin, clockPin);
9091
}
92+
#endif
9193

9294

9395
bool SHT31::begin(const uint8_t address, TwoWire *wire)
@@ -103,6 +105,12 @@ bool SHT31::begin(const uint8_t address, TwoWire *wire)
103105
}
104106

105107

108+
bool SHT31::begin(TwoWire *wire)
109+
{
110+
return begin(SHT_DEFAULT_ADDRESS, wire);
111+
}
112+
113+
106114
bool SHT31::read(bool fast)
107115
{
108116
if (writeCmd(fast ? SHT31_MEASUREMENT_FAST : SHT31_MEASUREMENT_SLOW) == false)
@@ -284,8 +292,8 @@ bool SHT31::readData(bool fast)
284292
}
285293
}
286294

287-
rawTemperature = (buffer[0] << 8) + buffer[1];
288-
rawHumidity = (buffer[3] << 8) + buffer[4];
295+
_rawTemperature = (buffer[0] << 8) + buffer[1];
296+
_rawHumidity = (buffer[3] << 8) + buffer[4];
289297

290298
_lastRead = millis();
291299

SHT31.h

Lines changed: 18 additions & 11 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.3.5
5+
// VERSION: 0.3.6
66
// DATE: 2019-02-08
77
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
88
// https://www.adafruit.com/product/2857
@@ -14,8 +14,11 @@
1414
#include "Wire.h"
1515

1616

17-
#define SHT31_LIB_VERSION (F("0.3.5"))
17+
#define SHT31_LIB_VERSION (F("0.3.6"))
1818

19+
#ifndef SHT_DEFAULT_ADDRESS
20+
#define SHT_DEFAULT_ADDRESS 0x44
21+
#endif
1922

2023
// fields readStatus
2124
#define SHT31_STATUS_ALERT_PENDING (1 << 15)
@@ -46,9 +49,12 @@ class SHT31
4649

4750
#if defined(ESP8266) || defined(ESP32)
4851
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
52+
// use SHT_DEFAULT_ADDRESS
53+
bool begin(const uint8_t dataPin, const uint8_t clockPin);
4954
#endif
50-
bool begin(const uint8_t address);
51-
bool begin(const uint8_t address, TwoWire *wire);
55+
bool begin(const uint8_t address, TwoWire *wire = &Wire);
56+
// use SHT_DEFAULT_ADDRESS
57+
bool begin(TwoWire *wire = &Wire);
5258

5359
// blocks 15 milliseconds + actual read + math
5460
bool read(bool fast = true);
@@ -69,16 +75,17 @@ class SHT31
6975
// and let it cool down at least 3 minutes.
7076
void setHeatTimeout(uint8_t seconds);
7177
uint8_t getHeatTimeout() { return _heatTimeout; };
72-
;
7378
bool heatOn();
7479
bool heatOff();
7580
bool isHeaterOn(); // is the sensor still heating up?
7681
bool heatUp() { return isHeaterOn(); }; // will be obsolete in future
7782

78-
float getHumidity() { return rawHumidity * (100.0 / 65535); };
79-
float getTemperature() { return rawTemperature * (175.0 / 65535) - 45; };
80-
uint16_t getRawHumidity() {return rawHumidity ; };
81-
uint16_t getRawTemperature() {return rawTemperature; };
83+
float getHumidity() { return _rawHumidity * (100.0 / 65535); };
84+
float getTemperature() { return _rawTemperature * (175.0 / 65535) - 45; };
85+
float getFahrenheit() { return _rawTemperature * (63.0 /13107.0) - 49; };
86+
uint16_t getRawHumidity() { return _rawHumidity; };
87+
uint16_t getRawTemperature() { return _rawTemperature; };
88+
8289

8390
// ASYNC INTERFACE
8491
bool requestData();
@@ -101,8 +108,8 @@ class SHT31
101108
uint32_t _heaterStop;
102109
bool _heaterOn;
103110

104-
uint16_t rawHumidity;
105-
uint16_t rawTemperature;
111+
uint16_t _rawHumidity;
112+
uint16_t _rawTemperature;
106113

107114
uint8_t _error;
108115
};

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/SHT31"
1717
},
18-
"version": "0.3.5",
18+
"version": "0.3.6",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SHT31
2-
version=0.3.5
2+
version=0.3.6
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library for the SHT31 temperature and humidity sensor

0 commit comments

Comments
 (0)