0.2.0 SHT85

This commit is contained in:
rob tillaart 2021-08-24 19:59:51 +02:00
parent f0545436f2
commit 8eb4fbc88f
9 changed files with 128 additions and 50 deletions

View File

@ -70,7 +70,12 @@ https://github.com/hawesg/SHT31D_Particle_Photon_ClosedCube
#### Base interface
- **SHT()** constructor of the base class. **getType()** will return 0.
- **SHT30()** constructor.
- **SHT31()** constructor.
- **SHT35()** constructor.
- **SHT85()** constructor.
- **uint8_t getType()** returns numeric part of sensor type.
- **begin(address, dataPin, clockPin)** begin function for ESP8266 & ESP32; **WARNING: not verified yet**
returns false if device address is incorrect or device cannot be reset.
- **bool begin(address, TwoWire \*wire = &Wire)** for platforms with multiple I2C busses.
@ -80,8 +85,9 @@ Does read both the temperature and humidity.
- **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.
- **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.
@ -90,7 +96,7 @@ Note that the temperature and humidity values are recalculated on every call to
#### Error interface
- **getError()** returns last set error flag and clear it.
- **int getError()** returns last set error flag and clear it.
Be sure to clear the error flag by calling **getError()** before calling any command as the error flag could be from a previous command.
| Error | Symbolic | Description
@ -104,7 +110,7 @@ Be sure to clear the error flag by calling **getError()** before calling any com
| 0x86 | SHT_ERR_CRC_HUM | CRC error in humidity |
| 0x87 | SHT_ERR_CRC_STATUS | CRC error in statusfield |
| 0x88 | SHT_ERR_HEATER_COOLDOWN | Heater need to cool down |
| 0x88 | SHT_ERR_HEATER_ON | Could not switch on heater |
| 0x89 | SHT_ERR_HEATER_ON | Could not switch on heater |
#### Heater interface
@ -136,7 +142,7 @@ Will switch the heater off if max heating time has passed.
See async example for usage
- **bool requestData()** requests a new measurement. Returns false if this fails.
- **bool requestData()** requests a new measurement. Returns false if the request fails.
- **bool dataReady()** checks if enough time has passed to read the data. (15 milliseconds)
- **bool readData(bool fast = true)** fast = true skips the CRC check.
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.
## Future
- verify working with ESP32
- merge with other SHT sensors if possible
- SHT_BASE class ?
- investigate command ART (auto sampling at 4 Hz)
- investigate command BREAK (stop auto sampling)
- direct Fahrenheit formula ?
- improve error handling / status. (all code paths)

View File

@ -1,7 +1,7 @@
//
// FILE: SHT85.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.2.0
// DATE: 2021-02-10
// PURPOSE: Arduino library for the SHT85 temperature and humidity sensor
// https://nl.rs-online.com/web/p/temperature-humidity-sensor-ics/1826530
@ -15,6 +15,8 @@
// 0.1.3 2021-08-06 expose raw data from sensor
// 0.1.4 2021-08-24 prevent heater to switch on too fast.
// update readme
// 0.2.0 2021-08-24 split off base class
// create derived classes SHT85, 30, 31, 35
#include "SHT85.h"
@ -34,7 +36,8 @@
#define SHT_HEAT_OFF 0x3066
#define SHT_HEATER_TIMEOUT 180000UL // milliseconds
SHT85::SHT85()
SHT::SHT()
{
_address = 0;
_lastRead = 0;
@ -45,11 +48,12 @@ SHT85::SHT85()
_heaterStop = 0;
_heaterOn = false;
_error = SHT_OK;
_type = 0;
}
#if defined(ESP8266) || defined(ESP32)
bool SHT85::begin(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
bool SHT::begin(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
{
if ((address != 0x44) && (address != 0x45))
{
@ -69,7 +73,7 @@ bool SHT85::begin(const uint8_t address, const uint8_t dataPin, const uint8_t cl
#endif
bool SHT85::begin(const uint8_t address, TwoWire *wire)
bool SHT::begin(const uint8_t address, TwoWire *wire)
{
if ((address != 0x44) && (address != 0x45))
{
@ -82,7 +86,7 @@ bool SHT85::begin(const uint8_t address, TwoWire *wire)
}
bool SHT85::read(bool fast)
bool SHT::read(bool fast)
{
if (writeCmd(fast ? SHT_MEASUREMENT_FAST : SHT_MEASUREMENT_SLOW) == false)
{
@ -93,7 +97,7 @@ bool SHT85::read(bool fast)
}
bool SHT85::isConnected()
bool SHT::isConnected()
{
_wire->beginTransmission(_address);
int rv = _wire->endTransmission();
@ -132,7 +136,7 @@ bool SHT85::isConnected()
#endif
uint16_t SHT85::readStatus()
uint16_t SHT::readStatus()
{
uint8_t status[3] = { 0, 0, 0 };
// page 13 datasheet
@ -156,7 +160,7 @@ uint16_t SHT85::readStatus()
}
bool SHT85::reset(bool hard)
bool SHT::reset(bool hard)
{
bool b = writeCmd(hard ? SHT_HARD_RESET : SHT_SOFT_RESET);
if (b == false)
@ -168,14 +172,14 @@ bool SHT85::reset(bool hard)
}
void SHT85::setHeatTimeout(uint8_t seconds)
void SHT::setHeatTimeout(uint8_t seconds)
{
_heatTimeout = seconds;
if (_heatTimeout > 180) _heatTimeout = 180;
}
bool SHT85::heatOn()
bool SHT::heatOn()
{
if (isHeaterOn()) return true;
if ((_heaterStop > 0) && (millis() - _heaterStop < SHT_HEATER_TIMEOUT))
@ -194,7 +198,7 @@ bool SHT85::heatOn()
}
bool SHT85::heatOff()
bool SHT::heatOff()
{
// always switch off the heater - ignore _heaterOn flag.
if (writeCmd(SHT_HEAT_OFF) == false)
@ -208,7 +212,7 @@ bool SHT85::heatOff()
}
bool SHT85::isHeaterOn()
bool SHT::isHeaterOn()
{
if (_heaterOn == false)
{
@ -224,7 +228,7 @@ bool SHT85::isHeaterOn()
}
bool SHT85::requestData()
bool SHT::requestData()
{
if (writeCmd(SHT_MEASUREMENT_SLOW) == false)
{
@ -235,13 +239,13 @@ bool SHT85::requestData()
}
bool SHT85::dataReady()
bool SHT::dataReady()
{
return ((millis() - _lastRequest) > 15); // TODO MAGIC NR
}
bool SHT85::readData(bool fast)
bool SHT::readData(bool fast)
{
uint8_t buffer[6];
if (readBytes(6, (uint8_t*) &buffer[0]) == false)
@ -272,7 +276,7 @@ bool SHT85::readData(bool fast)
}
int SHT85::getError()
int SHT::getError()
{
int rv = _error;
_error = SHT_OK;
@ -282,7 +286,7 @@ int SHT85::getError()
//////////////////////////////////////////////////////////
uint8_t SHT85::crc8(const uint8_t *data, uint8_t len)
uint8_t SHT::crc8(const uint8_t *data, uint8_t len)
{
// CRC-8 formula from page 14 of SHT spec pdf
const uint8_t POLY(0x31);
@ -301,7 +305,7 @@ uint8_t SHT85::crc8(const uint8_t *data, uint8_t len)
}
bool SHT85::writeCmd(uint16_t cmd)
bool SHT::writeCmd(uint16_t cmd)
{
_wire->beginTransmission(_address);
_wire->write(cmd >> 8 );
@ -315,7 +319,7 @@ bool SHT85::writeCmd(uint16_t cmd)
}
bool SHT85::readBytes(uint8_t n, uint8_t *val)
bool SHT::readBytes(uint8_t n, uint8_t *val)
{
int rv = _wire->requestFrom(_address, (uint8_t) n);
if (rv == n)
@ -330,4 +334,34 @@ bool SHT85::readBytes(uint8_t n, uint8_t *val)
return false;
}
////////////////////////////////////////////////////////
//
// DERIVED
//
SHT30::SHT30()
{
_type = 30;
};
SHT31::SHT31()
{
_type = 31;
};
SHT35::SHT35()
{
_type = 35;
};
SHT85::SHT85()
{
_type = 85;
};
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
//
// FILE: SHT85.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.4
// VERSION: 0.2.0
// DATE: 2021-02-10
// PURPOSE: Arduino library for the SHT85 temperature and humidity sensor
// https://nl.rs-online.com/web/p/temperature-humidity-sensor-ics/1826530
@ -25,7 +25,8 @@
#include "Wire.h"
#define SHT85_LIB_VERSION (F("0.1.4"))
#define SHT_LIB_VERSION (F("0.2.0"))
#define SHT85_LIB_VERSION SHT_LIB_VERSION
// fields readStatus
@ -50,16 +51,18 @@
#define SHT_ERR_HEATER_ON 0x89
class SHT85
class SHT
{
public:
SHT85();
SHT();
#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
#endif
bool begin(const uint8_t address, TwoWire *wire = &Wire);
uint8_t getType() { return _type; };
// blocks 15 milliseconds + actual read + math
bool read(bool fast = true);
@ -79,14 +82,14 @@ public:
// 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 isHeaterOn(); // is the sensor still heating up?
float getHumidity() { return _rawHumidity * (100.0 / 65535); };
float getTemperature() { return _rawTemperature * (175.0 / 65535) - 45; };
// float getFahrenheit() { return _rawTemperature * (63.0 /13107.0) - 49; };
float getFahrenheit() { return _rawTemperature * (63.0 /13107.0) - 49; };
uint16_t getRawHumidity() {return _rawHumidity; };
uint16_t getRawTemperature() {return _rawTemperature; };
@ -98,7 +101,7 @@ public:
int getError(); // clears error flag
private:
protected:
uint8_t crc8(const uint8_t *data, uint8_t len);
bool writeCmd(uint16_t cmd);
bool readBytes(uint8_t n, uint8_t *val);
@ -111,6 +114,7 @@ private:
uint32_t _heaterStart;
uint32_t _heaterStop;
bool _heaterOn;
uint8_t _type;
uint16_t _rawHumidity;
uint16_t _rawTemperature;
@ -118,4 +122,39 @@ private:
uint8_t _error;
};
////////////////////////////////////////////////////////
//
// DERIVED
//
class SHT30 : public SHT
{
public:
SHT30();
};
class SHT31 : public SHT
{
public:
SHT31();
};
class SHT35 : public SHT
{
public:
SHT35();
};
class SHT85 : public SHT
{
public:
SHT85();
};
// -- END OF FILE --

View File

@ -1,7 +1,7 @@
//
// FILE: SHT85_I2C_speed.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.2.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/SHT85
@ -29,8 +29,8 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("SHT85_LIB_VERSION: \t");
Serial.println(SHT85_LIB_VERSION);
Serial.print("SHT_LIB_VERSION: \t");
Serial.println(SHT_LIB_VERSION);
Wire.begin();
sht.begin(SHT85_ADDRESS);

View File

@ -1,7 +1,7 @@
//
// FILE: SHT85_demo.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.2.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/SHT85
@ -29,8 +29,8 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("SHT85_LIB_VERSION: \t");
Serial.println(SHT85_LIB_VERSION);
Serial.print("SHT_LIB_VERSION: \t");
Serial.println(SHT_LIB_VERSION);
Wire.begin();
sht.begin(SHT85_ADDRESS);

View File

@ -1,7 +1,7 @@
//
// FILE: SHT85_demo_plotter.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.2.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/SHT85
@ -31,8 +31,8 @@ void setup()
{
Serial.begin(115200);
// Serial.println(__FILE__);
// Serial.print("SHT85_LIB_VERSION: \t");
// Serial.println(SHT85_LIB_VERSION);
// Serial.print("SHT_LIB_VERSION: \t");
// Serial.println(SHT_LIB_VERSION);
Wire.begin();
sht.begin(SHT85_ADDRESS);

View File

@ -7,6 +7,8 @@ SHT85 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
getType KEYWORD2
read KEYWORD2
isConnected KEYWORD2
readStatus KEYWORD2

View File

@ -1,7 +1,7 @@
{
"name": "SHT85",
"keywords": "SHT85 Temperature Humidity I2C SHT30, SHT31, SHT35",
"description": "Arduino library for the I2C SHT85 temperature and humidity sensor",
"keywords": "SHT85 Temperature Humidity I2C SHT30, SHT31, SHT35, Senserion, temperature, humidity",
"description": "Arduino library for the SHT85, SHT30, SHT31, SHT35 temperature and humidity sensor",
"authors":
[
{
@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/SHT85"
},
"version": "0.1.4",
"version": "0.2.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"

View File

@ -1,9 +1,9 @@
name=SHT85
version=0.1.4
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the SHT85 temperature and humidity sensor
paragraph=Class for SHT85 Temperature Humidity Adafruit I2C
sentence=Arduino library for the SHT85, SHT30, SHT31, SHT35 Senserion temperature and humidity sensor
paragraph=
category=Sensors
url=https://github.com/RobTillaart/SHT85
architectures=*