Skip to content

Commit

Permalink
sync with SHT85 (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Jan 18, 2022
1 parent 4b358c7 commit 2845e4b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
27 changes: 10 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ https://github.com/hawesg/SHT31D_Particle_Photon_ClosedCube
#### Base interface

- **SHT31()** constructor.
- **bool begin(uint8_taddress, uint8_tdataPin, uint8_tclockPin)** begin function for ESP8266 & ESP32;
- **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 address)** for single I2C bus platforms, e.g UNO.
- **bool begin(uint8_t address, TwoWire \*wire)** for platforms with multiple I2C buses.
- **bool begin(uint8_t dataPin, uint8_t clockPin)** same as above. With default SHT_DEFAULT_ADDRESS.
- **bool begin(uint8_t address, TwoWire \*wire = &Wire)** for platforms with multiple I2C buses.
- **bool begin(TwoWire \*wire = &Wire)** same as above.
With default SHT_DEFAULT_ADDRESS.
- **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 fails.
- **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.
- **bool reset(bool hard = false)** resets the sensor, soft reset by default. Returns false if it fails.
- **float getHumidity()** computes the relative humidity in % based on the latest raw reading, and returns it.
- **float getTemperature()** computes the temperature in °C based on the latest raw reading, and returns it.
- **float getFahrenheit()** computes the temperature in °F based on the latest raw reading, and returns it..
- **uint16_t getRawHumidity()** returns the raw two-byte representation of humidity directly from the sensor.
- **uint16_t getRawTemperature()** returns the raw two-byte representation of temperature directly from the sensor.

Expand Down Expand Up @@ -144,16 +147,6 @@ See examples.

## Future

- merge with other SHT sensors if possible
- direct Fahrenheit formula ?
- improve error handling / status. (all code paths)
- verify working with ESP32
- investigate command ART (auto sampling at 4 Hz)
- investigate command BREAK (stop auto sampling)
- software I2C experiments?
- separate releaseNotes.md


#### Wont
- keep in sync with SHT85 library


24 changes: 16 additions & 8 deletions SHT31.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: SHT31.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.5
// VERSION: 0.3.6
// DATE: 2019-02-08
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
// https://www.adafruit.com/product/2857
Expand Down Expand Up @@ -30,6 +30,7 @@
// update readme
// 0.3.4 2021-09-19 update build-CI
// 0.3.5 2021-12-28 update library.json, readme, license, minor edits
// 0.3.6 2022-01-18 sync with SHT85 lib


#include "SHT31.h"
Expand All @@ -49,12 +50,13 @@
#define SHT31_HEAT_OFF 0x3066
#define SHT31_HEATER_TIMEOUT 180000UL // milliseconds


SHT31::SHT31()
{
_address = 0;
_lastRead = 0;
rawTemperature = 0;
rawHumidity = 0;
_rawTemperature = 0;
_rawHumidity = 0;
_heatTimeout = 0;
_heaterStart = 0;
_heaterStop = 0;
Expand All @@ -81,13 +83,13 @@ bool SHT31::begin(const uint8_t address, const uint8_t dataPin, const uint8_t cl
}
return reset();
}
#endif


bool SHT31::begin(const uint8_t address)
bool SHT31::begin(const uint8_t dataPin, const uint8_t clockPin)
{
return begin(address, &Wire);
return begin(SHT_DEFAULT_ADDRESS, dataPin, clockPin);
}
#endif


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


bool SHT31::begin(TwoWire *wire)
{
return begin(SHT_DEFAULT_ADDRESS, wire);
}


bool SHT31::read(bool fast)
{
if (writeCmd(fast ? SHT31_MEASUREMENT_FAST : SHT31_MEASUREMENT_SLOW) == false)
Expand Down Expand Up @@ -284,8 +292,8 @@ bool SHT31::readData(bool fast)
}
}

rawTemperature = (buffer[0] << 8) + buffer[1];
rawHumidity = (buffer[3] << 8) + buffer[4];
_rawTemperature = (buffer[0] << 8) + buffer[1];
_rawHumidity = (buffer[3] << 8) + buffer[4];

_lastRead = millis();

Expand Down
29 changes: 18 additions & 11 deletions SHT31.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: SHT31.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.5
// VERSION: 0.3.6
// DATE: 2019-02-08
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
// https://www.adafruit.com/product/2857
Expand All @@ -14,8 +14,11 @@
#include "Wire.h"


#define SHT31_LIB_VERSION (F("0.3.5"))
#define SHT31_LIB_VERSION (F("0.3.6"))

#ifndef SHT_DEFAULT_ADDRESS
#define SHT_DEFAULT_ADDRESS 0x44
#endif

// fields readStatus
#define SHT31_STATUS_ALERT_PENDING (1 << 15)
Expand Down Expand Up @@ -46,9 +49,12 @@ class SHT31

#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);
bool begin(const uint8_t address, TwoWire *wire);
bool begin(const uint8_t address, TwoWire *wire = &Wire);
// use SHT_DEFAULT_ADDRESS
bool begin(TwoWire *wire = &Wire);

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

float getHumidity() { return rawHumidity * (100.0 / 65535); };
float getTemperature() { return rawTemperature * (175.0 / 65535) - 45; };
uint16_t getRawHumidity() {return rawHumidity ; };
uint16_t getRawTemperature() {return rawTemperature; };
float getHumidity() { return _rawHumidity * (100.0 / 65535); };
float getTemperature() { return _rawTemperature * (175.0 / 65535) - 45; };
float getFahrenheit() { return _rawTemperature * (63.0 /13107.0) - 49; };
uint16_t getRawHumidity() { return _rawHumidity; };
uint16_t getRawTemperature() { return _rawTemperature; };


// ASYNC INTERFACE
bool requestData();
Expand All @@ -101,8 +108,8 @@ class SHT31
uint32_t _heaterStop;
bool _heaterOn;

uint16_t rawHumidity;
uint16_t rawTemperature;
uint16_t _rawHumidity;
uint16_t _rawTemperature;

uint8_t _error;
};
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/SHT31"
},
"version": "0.3.5",
"version": "0.3.6",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SHT31
version=0.3.5
version=0.3.6
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the SHT31 temperature and humidity sensor
Expand Down

0 comments on commit 2845e4b

Please sign in to comment.