GY-63_MS5611/libraries/SHT85/SHT85.cpp

526 lines
9.2 KiB
C++
Raw Normal View History

2021-05-26 06:06:51 -04:00
//
// FILE: SHT85.cpp
// AUTHOR: Rob Tillaart
2023-09-21 10:40:54 -04:00
// VERSION: 0.5.0
2021-05-26 06:06:51 -04:00
// 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
// URL: https://github.com/RobTillaart/SHT85
#include "SHT85.h"
2023-04-06 10:54:56 -04:00
// SUPPORTED COMMANDS - single shot mode only
2021-05-26 06:06:51 -04:00
#define SHT_READ_STATUS 0xF32D
#define SHT_CLEAR_STATUS 0x3041
#define SHT_SOFT_RESET 0x30A2
#define SHT_HARD_RESET 0x0006
2023-04-06 10:54:56 -04:00
#define SHT_MEASUREMENT_FAST 0x2416 // page 10 datasheet
#define SHT_MEASUREMENT_SLOW 0x2400 // no clock stretching
2021-05-26 06:06:51 -04:00
#define SHT_HEAT_ON 0x306D
#define SHT_HEAT_OFF 0x3066
2023-04-06 10:54:56 -04:00
#define SHT_HEATER_TIMEOUT 180000UL // milliseconds
2021-05-26 06:06:51 -04:00
2023-05-09 06:44:18 -04:00
#define SHT_GET_SERIAL 0x3682
2021-08-24 13:59:51 -04:00
2023-09-21 10:40:54 -04:00
SHT::SHT(TwoWire *wire)
2021-05-26 06:06:51 -04:00
{
2023-04-06 10:54:56 -04:00
_address = 0;
2023-09-21 10:40:54 -04:00
_wire = wire;
2023-04-06 10:54:56 -04:00
_lastRead = 0;
_rawTemperature = 0;
_rawHumidity = 0;
_heatTimeout = 0;
_heaterStart = 0;
_heaterStop = 0;
_heaterOn = false;
_error = SHT_OK;
_type = 0;
_temperatureOffset = 0;
_humidityOffset = 0;
2021-05-26 06:06:51 -04:00
}
#if defined(ESP8266) || defined(ESP32)
2021-08-24 13:59:51 -04:00
bool SHT::begin(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin)
2021-05-26 06:06:51 -04:00
{
if ((address != 0x44) && (address != 0x45))
{
return false;
}
2021-08-24 12:45:23 -04:00
_address = address;
2021-05-26 06:06:51 -04:00
_wire = &Wire;
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
return reset();
}
2022-01-17 15:15:55 -05:00
bool SHT::begin(const uint8_t dataPin, const uint8_t clockPin)
{
return begin(SHT_DEFAULT_ADDRESS, dataPin, clockPin);
}
2021-05-26 06:06:51 -04:00
#endif
2023-09-21 10:40:54 -04:00
bool SHT::begin(const uint8_t address)
2021-05-26 06:06:51 -04:00
{
if ((address != 0x44) && (address != 0x45))
{
return false;
}
2021-08-24 12:45:23 -04:00
_address = address;
2021-05-26 06:06:51 -04:00
_wire->begin();
return reset();
}
2023-04-06 10:54:56 -04:00
uint8_t SHT::getType()
{
return _type;
};
///////////////////////////////////////////////////
//
// SYNCHRONUOUS interface
//
2021-08-24 13:59:51 -04:00
bool SHT::read(bool fast)
2023-04-06 10:54:56 -04:00
{
2023-05-09 06:44:18 -04:00
// prevent error on failed request.
if (requestData(fast) == false)
{
return false;
}
2023-04-06 10:54:56 -04:00
while(dataReady(fast) == false) yield();
return readData(fast);
}
///////////////////////////////////////////////////
//
// ASYNCHRONUOUS interface
//
bool SHT::requestData(bool fast)
2021-05-26 06:06:51 -04:00
{
if (writeCmd(fast ? SHT_MEASUREMENT_FAST : SHT_MEASUREMENT_SLOW) == false)
{
return false;
}
2023-04-06 10:54:56 -04:00
_lastRequest = millis();
return true;
2021-05-26 06:06:51 -04:00
}
2023-04-06 10:54:56 -04:00
bool SHT::dataReady(bool fast)
{
return ((millis() - _lastRequest) > (fast ? 4 : 15));
}
bool SHT::readData(bool fast)
{
uint8_t buffer[6];
if (readBytes(6, (uint8_t*) &buffer[0]) == false)
{
return false;
}
if (!fast)
{
if (buffer[2] != crc8(buffer, 2))
{
_error = SHT_ERR_CRC_TEMP;
return false;
}
if (buffer[5] != crc8(buffer + 3, 2))
{
_error = SHT_ERR_CRC_HUM;
return false;
}
}
_rawTemperature = (buffer[0] << 8) + buffer[1];
_rawHumidity = (buffer[3] << 8) + buffer[4];
_lastRead = millis();
2023-05-09 06:44:18 -04:00
_error = SHT_OK;
2023-04-06 10:54:56 -04:00
return true;
}
uint32_t SHT::lastRequest()
{
return _lastRequest;
};
///////////////////////////////////////////////////
//
// STATUS
//
2021-08-24 13:59:51 -04:00
bool SHT::isConnected()
2021-05-26 06:06:51 -04:00
{
2021-08-24 12:45:23 -04:00
_wire->beginTransmission(_address);
2021-05-26 06:06:51 -04:00
int rv = _wire->endTransmission();
2023-05-09 06:44:18 -04:00
if (rv != 0)
{
_error = SHT_ERR_NOT_CONNECT;
return false;
}
_error = SHT_OK;
return true;
2021-05-26 06:06:51 -04:00
}
2023-05-09 06:44:18 -04:00
2021-05-26 06:06:51 -04:00
#ifdef doc
2022-11-24 08:11:40 -05:00
// bit - description
// ==================
// 15 Alert pending status
// '0': no pending alerts
// '1': at least one pending alert - default
// 14 Reserved 0
// 13 Heater status
// '0 : Heater OFF - default
// '1 : Heater ON
// 12 Reserved '0
// 11 Humidity tracking alert
// '0 : no alert - default
// '1 : alert
// 10 Temp tracking alert
// '0 : no alert - default
// '1 : alert
// 9:5 Reserved '00000
// 4 System reset detected
// '0': no reset since last clear status register command
// '1': reset detected (hard or soft reset command or supply fail) - default
// 3:2 Reserved 00
// 1 Command status
// '0': last command executed successfully
// '1': last command not processed. Invalid or failed checksum
// 0 Write data checksum status
// '0': checksum of last write correct
// '1': checksum of last write transfer failed
2021-05-26 06:06:51 -04:00
#endif
2021-08-24 13:59:51 -04:00
uint16_t SHT::readStatus()
2021-05-26 06:06:51 -04:00
{
uint8_t status[3] = { 0, 0, 0 };
2022-11-24 08:11:40 -05:00
// page 13 datasheet
2021-05-26 06:06:51 -04:00
if (writeCmd(SHT_READ_STATUS) == false)
{
return 0xFFFF;
}
2022-11-24 08:11:40 -05:00
// 16 bit status + CRC
2021-05-26 06:06:51 -04:00
if (readBytes(3, (uint8_t*) &status[0]) == false)
{
return 0xFFFF;
}
2023-04-06 10:54:56 -04:00
if (status[2] != crc8(status, 2))
2021-05-26 06:06:51 -04:00
{
_error = SHT_ERR_CRC_STATUS;
return 0xFFFF;
}
return (uint16_t) (status[0] << 8) + status[1];
}
2023-04-06 10:54:56 -04:00
uint32_t SHT::lastRead()
{
return _lastRead;
};
2021-08-24 13:59:51 -04:00
bool SHT::reset(bool hard)
2021-05-26 06:06:51 -04:00
{
bool b = writeCmd(hard ? SHT_HARD_RESET : SHT_SOFT_RESET);
if (b == false)
{
return false;
}
2022-11-24 08:11:40 -05:00
delay(1); // table 4 datasheet
2021-05-26 06:06:51 -04:00
return true;
}
2023-04-06 10:54:56 -04:00
int SHT::getError()
{
int rv = _error;
_error = SHT_OK;
return rv;
}
///////////////////////////////////////////////////
//
// HEATER
//
2021-08-24 13:59:51 -04:00
void SHT::setHeatTimeout(uint8_t seconds)
2021-05-26 06:06:51 -04:00
{
2021-08-24 12:45:23 -04:00
_heatTimeout = seconds;
if (_heatTimeout > 180) _heatTimeout = 180;
2021-05-26 06:06:51 -04:00
}
2023-04-06 10:54:56 -04:00
uint8_t SHT::getHeatTimeout()
{
return _heatTimeout;
};
2021-08-24 13:59:51 -04:00
bool SHT::heatOn()
2021-05-26 06:06:51 -04:00
{
2021-08-24 12:45:23 -04:00
if (isHeaterOn()) return true;
if ((_heaterStop > 0) && (millis() - _heaterStop < SHT_HEATER_TIMEOUT))
{
_error = SHT_ERR_HEATER_COOLDOWN;
return false;
}
2021-05-26 06:06:51 -04:00
if (writeCmd(SHT_HEAT_ON) == false)
{
2023-05-09 06:44:18 -04:00
_error = SHT_ERR_HEATER_ON; // more specific error!
2021-05-26 06:06:51 -04:00
return false;
}
_heaterStart = millis();
2021-08-24 12:45:23 -04:00
_heaterOn = true;
2021-05-26 06:06:51 -04:00
return true;
}
2021-08-24 13:59:51 -04:00
bool SHT::heatOff()
2021-05-26 06:06:51 -04:00
{
2022-11-24 08:11:40 -05:00
// always switch off the heater - ignore _heaterOn flag.
2021-05-26 06:06:51 -04:00
if (writeCmd(SHT_HEAT_OFF) == false)
{
2023-05-09 06:44:18 -04:00
_error = SHT_ERR_HEATER_OFF; // can be serious!
2021-05-26 06:06:51 -04:00
return false;
}
2021-08-24 12:45:23 -04:00
_heaterStop = millis();
_heaterOn = false;
2021-05-26 06:06:51 -04:00
return true;
}
2021-08-24 13:59:51 -04:00
bool SHT::isHeaterOn()
2021-05-26 06:06:51 -04:00
{
2021-08-24 12:45:23 -04:00
if (_heaterOn == false)
2021-05-26 06:06:51 -04:00
{
return false;
}
2022-11-24 08:11:40 -05:00
// did not exceed time out
2021-08-24 12:45:23 -04:00
if (millis() - _heaterStart < (_heatTimeout * 1000UL))
2021-05-26 06:06:51 -04:00
{
return true;
}
2021-08-24 12:45:23 -04:00
heatOff();
2021-05-26 06:06:51 -04:00
return false;
}
2023-04-06 10:54:56 -04:00
//////////////////////////////////////////////////////////
//
// TEMPERATURE & HUMIDITY
//
float SHT::getHumidity()
2021-05-26 06:06:51 -04:00
{
2023-04-06 10:54:56 -04:00
float hum = _rawHumidity * (100.0 / 65535);
if (_humidityOffset != 0) hum += _humidityOffset;
return hum;
2021-05-26 06:06:51 -04:00
}
2023-04-06 10:54:56 -04:00
float SHT::getTemperature()
2021-05-26 06:06:51 -04:00
{
2023-04-06 10:54:56 -04:00
float temp = _rawTemperature * (175.0 / 65535) - 45;
if (_temperatureOffset != 0) temp += _temperatureOffset;
return temp;
2021-05-26 06:06:51 -04:00
}
2023-04-06 10:54:56 -04:00
float SHT::getFahrenheit()
2021-05-26 06:06:51 -04:00
{
2023-04-06 10:54:56 -04:00
float temp = _rawTemperature * (63.0 / 13107.0) - 49;
if (_temperatureOffset != 0) temp += _temperatureOffset * 1.8;
return temp;
}
2021-05-26 06:06:51 -04:00
2023-04-06 10:54:56 -04:00
uint16_t SHT::getRawHumidity()
{
return _rawHumidity;
}
2021-05-26 06:06:51 -04:00
2023-04-06 10:54:56 -04:00
uint16_t SHT::getRawTemperature()
{
return _rawTemperature;
2021-05-26 06:06:51 -04:00
}
2023-04-06 10:54:56 -04:00
void SHT::setTemperatureOffset(float offset)
2021-05-26 06:06:51 -04:00
{
2023-04-06 10:54:56 -04:00
_temperatureOffset = offset;
2021-05-26 06:06:51 -04:00
}
2023-04-06 10:54:56 -04:00
float SHT::getTemperatureOffset()
{
return _temperatureOffset;
}
2021-05-26 06:06:51 -04:00
2023-04-06 10:54:56 -04:00
void SHT::setHumidityOffset(float offset)
{
_humidityOffset = offset;
}
float SHT::getHumidityOffset()
{
return _humidityOffset;
}
//////////////////////////////////////////////////////////
//
// PROTECTED
//
uint8_t SHT::crc8(const uint8_t *data, uint8_t len)
2021-05-26 06:06:51 -04:00
{
// CRC-8 formula from page 14 of SHT spec pdf
const uint8_t POLY(0x31);
uint8_t crc(0xFF);
2023-04-06 10:54:56 -04:00
for (uint8_t j = len; j; --j)
2021-05-26 06:06:51 -04:00
{
crc ^= *data++;
2023-04-06 10:54:56 -04:00
for (uint8_t i = 8; i; --i)
2021-05-26 06:06:51 -04:00
{
crc = (crc & 0x80) ? (crc << 1) ^ POLY : (crc << 1);
}
}
return crc;
}
2021-08-24 13:59:51 -04:00
bool SHT::writeCmd(uint16_t cmd)
2021-05-26 06:06:51 -04:00
{
2021-08-24 12:45:23 -04:00
_wire->beginTransmission(_address);
2021-05-26 06:06:51 -04:00
_wire->write(cmd >> 8 );
_wire->write(cmd & 0xFF);
if (_wire->endTransmission() != 0)
{
_error = SHT_ERR_WRITECMD;
return false;
}
2023-05-09 06:44:18 -04:00
_error = SHT_OK;
2021-05-26 06:06:51 -04:00
return true;
}
2021-08-24 13:59:51 -04:00
bool SHT::readBytes(uint8_t n, uint8_t *val)
2021-05-26 06:06:51 -04:00
{
2021-08-24 12:45:23 -04:00
int rv = _wire->requestFrom(_address, (uint8_t) n);
2023-05-09 06:44:18 -04:00
if (rv != n)
2021-05-26 06:06:51 -04:00
{
2023-05-09 06:44:18 -04:00
_error = SHT_ERR_READBYTES;
return false;
2021-05-26 06:06:51 -04:00
}
2023-05-09 06:44:18 -04:00
for (uint8_t i = 0; i < n; i++)
{
val[i] = _wire->read();
}
_error = SHT_OK;
return true;
2021-05-26 06:06:51 -04:00
}
2021-08-24 13:59:51 -04:00
////////////////////////////////////////////////////////
//
2023-04-06 10:54:56 -04:00
// DERIVED CLASSES
2021-08-24 13:59:51 -04:00
//
2023-09-21 10:40:54 -04:00
SHT30::SHT30(TwoWire *wire) : SHT(wire)
2021-08-24 13:59:51 -04:00
{
_type = 30;
2023-05-09 06:44:18 -04:00
}
2021-08-24 13:59:51 -04:00
2023-09-21 10:40:54 -04:00
SHT31::SHT31(TwoWire *wire) : SHT(wire)
2021-08-24 13:59:51 -04:00
{
_type = 31;
2023-05-09 06:44:18 -04:00
}
2021-08-24 13:59:51 -04:00
2023-09-21 10:40:54 -04:00
SHT35::SHT35(TwoWire *wire) : SHT(wire)
2021-08-24 13:59:51 -04:00
{
_type = 35;
2023-05-09 06:44:18 -04:00
}
2021-08-24 13:59:51 -04:00
2023-09-21 10:40:54 -04:00
SHT85::SHT85(TwoWire *wire) : SHT(wire)
2021-08-24 13:59:51 -04:00
{
_type = 85;
2023-05-09 06:44:18 -04:00
}
2023-09-06 05:42:30 -04:00
#if defined(ESP8266) || defined(ESP32)
bool SHT85::begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin)
{
if (address != 0x44) return false;
return SHT::begin(SHT_DEFAULT_ADDRESS, dataPin, clockPin);
}
#endif
2023-09-21 10:40:54 -04:00
bool SHT85::begin(const uint8_t address)
2023-09-06 05:42:30 -04:00
{
if (address != 0x44) return false;
2023-09-21 10:40:54 -04:00
return SHT::begin(address);
2023-09-06 05:42:30 -04:00
}
2023-05-09 06:44:18 -04:00
uint32_t SHT85::GetSerialNumber()
{
uint8_t bytes[6];
if (writeCmd(SHT_GET_SERIAL) == false)
{
return 0xFFFFFFF0;
}
delayMicroseconds(500); // timing sensitive.
if (readBytes(6, (uint8_t*) &bytes[0]) == false)
{
_error = SHT_ERR_SERIAL;
return 0xFFFFFFFF;
}
// check CRC
// todo
// combine bytes to serial.
uint32_t serial = bytes[0];
serial <<= 8;
serial += bytes[1];
serial <<= 8;
serial += bytes[3];
serial <<= 8;
serial += bytes[4];
return serial;
}
2021-08-24 13:59:51 -04:00
2022-11-24 08:11:40 -05:00
// -- END OF FILE --
2023-04-06 10:54:56 -04:00