2021-05-26 06:06:51 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: SHT85.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-09-21 13:10:25 -04:00
|
|
|
// VERSION: 0.5.1
|
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
|
|
|
|
//
|
2022-11-24 08:11:40 -05:00
|
|
|
// keep lib in sync with https://github.com/RobTillaart/SHT31
|
2021-12-28 08:21:26 -05:00
|
|
|
//
|
2023-04-06 10:54:56 -04:00
|
|
|
// ALWAYS check datasheet for connections.
|
|
|
|
//
|
2022-11-24 08:11:40 -05:00
|
|
|
// TOPVIEW SHT85
|
|
|
|
// +-------+
|
|
|
|
// +-----\ | SDA 4 -----
|
|
|
|
// | +-+ ----+ GND 3 -----
|
|
|
|
// | +-+ ----+ +5V 2 -----
|
|
|
|
// +-----/ | SCL 1 -----
|
|
|
|
// +-------+
|
2021-05-26 06:06:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "Wire.h"
|
|
|
|
|
|
|
|
|
2023-09-21 13:10:25 -04:00
|
|
|
#define SHT_LIB_VERSION (F("0.5.1"))
|
2021-12-28 08:21:26 -05:00
|
|
|
#define SHT85_LIB_VERSION SHT_LIB_VERSION
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2023-04-06 10:54:56 -04:00
|
|
|
#ifndef SHT_DEFAULT_ADDRESS
|
2022-01-17 15:15:55 -05:00
|
|
|
#define SHT_DEFAULT_ADDRESS 0x44
|
|
|
|
#endif
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2022-11-24 08:11:40 -05:00
|
|
|
// fields readStatus
|
2021-12-28 08:21:26 -05:00
|
|
|
#define SHT_STATUS_ALERT_PENDING (1 << 15)
|
|
|
|
#define SHT_STATUS_HEATER_ON (1 << 13)
|
|
|
|
#define SHT_STATUS_HUM_TRACK_ALERT (1 << 11)
|
|
|
|
#define SHT_STATUS_TEMP_TRACK_ALERT (1 << 10)
|
|
|
|
#define SHT_STATUS_SYSTEM_RESET (1 << 4)
|
|
|
|
#define SHT_STATUS_COMMAND_STATUS (1 << 1)
|
|
|
|
#define SHT_STATUS_WRITE_CRC_STATUS (1 << 0)
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2022-11-24 08:11:40 -05:00
|
|
|
// error codes
|
2021-12-28 08:21:26 -05:00
|
|
|
#define SHT_OK 0x00
|
|
|
|
#define SHT_ERR_WRITECMD 0x81
|
|
|
|
#define SHT_ERR_READBYTES 0x82
|
|
|
|
#define SHT_ERR_HEATER_OFF 0x83
|
|
|
|
#define SHT_ERR_NOT_CONNECT 0x84
|
|
|
|
#define SHT_ERR_CRC_TEMP 0x85
|
|
|
|
#define SHT_ERR_CRC_HUM 0x86
|
|
|
|
#define SHT_ERR_CRC_STATUS 0x87
|
|
|
|
#define SHT_ERR_HEATER_COOLDOWN 0x88
|
|
|
|
#define SHT_ERR_HEATER_ON 0x89
|
2023-05-09 06:44:18 -04:00
|
|
|
#define SHT_ERR_SERIAL 0x8A
|
2021-05-26 06:06:51 -04:00
|
|
|
|
|
|
|
|
2021-08-24 13:59:51 -04:00
|
|
|
class SHT
|
2021-05-26 06:06:51 -04:00
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:40:54 -04:00
|
|
|
SHT(TwoWire *wire = &Wire);
|
2021-05-26 06:06:51 -04:00
|
|
|
|
|
|
|
#if defined(ESP8266) || defined(ESP32)
|
2023-04-06 10:54:56 -04:00
|
|
|
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
|
2022-11-24 08:11:40 -05:00
|
|
|
// use SHT_DEFAULT_ADDRESS
|
2023-04-06 10:54:56 -04:00
|
|
|
bool begin(const uint8_t dataPin, const uint8_t clockPin);
|
2021-05-26 06:06:51 -04:00
|
|
|
#endif
|
2023-04-06 10:54:56 -04:00
|
|
|
|
2023-09-21 10:40:54 -04:00
|
|
|
bool begin(const uint8_t address = SHT_DEFAULT_ADDRESS);
|
2023-04-06 10:54:56 -04:00
|
|
|
|
|
|
|
uint8_t getType();
|
|
|
|
|
|
|
|
|
|
|
|
// SYNCHRONOUS INTERFACE
|
|
|
|
// read blocks 4 or 15 milliseconds + actual read + math
|
|
|
|
bool read(bool fast = true);
|
|
|
|
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2023-04-06 10:54:56 -04:00
|
|
|
// ASYNCHRONOUS INTERFACE
|
|
|
|
bool requestData(bool fast = true);
|
|
|
|
bool dataReady(bool fast = true);
|
|
|
|
bool readData(bool fast = true);
|
|
|
|
uint32_t lastRequest();
|
2021-08-24 13:59:51 -04:00
|
|
|
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2023-04-06 10:54:56 -04:00
|
|
|
// STATUS
|
2022-11-24 08:11:40 -05:00
|
|
|
// check sensor is reachable over I2C
|
2023-04-06 10:54:56 -04:00
|
|
|
bool isConnected();
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2022-11-24 08:11:40 -05:00
|
|
|
// details see datasheet; summary in SHT85.cpp file
|
2021-05-26 06:06:51 -04:00
|
|
|
uint16_t readStatus();
|
|
|
|
|
2022-11-24 08:11:40 -05:00
|
|
|
// lastRead is in milliSeconds since start
|
2023-04-06 10:54:56 -04:00
|
|
|
uint32_t lastRead();
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2023-04-06 10:54:56 -04:00
|
|
|
bool reset(bool hard = false);
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2023-04-06 10:54:56 -04:00
|
|
|
// returns last error and clears error flag
|
|
|
|
int getError();
|
2021-12-28 08:21:26 -05:00
|
|
|
|
2023-04-06 10:54:56 -04:00
|
|
|
|
|
|
|
// HEATER
|
2022-11-24 08:11:40 -05:00
|
|
|
// do not use heater for long periods,
|
|
|
|
// use it for max 3 minutes to heat up
|
|
|
|
// and let it cool down at least 3 minutes.
|
2023-04-06 10:54:56 -04:00
|
|
|
void setHeatTimeout(uint8_t seconds);
|
|
|
|
uint8_t getHeatTimeout();
|
|
|
|
bool heatOn();
|
|
|
|
bool heatOff();
|
|
|
|
// is the sensor still heating up?
|
|
|
|
bool isHeaterOn();
|
2021-05-26 06:06:51 -04:00
|
|
|
|
|
|
|
|
2023-04-06 10:54:56 -04:00
|
|
|
// TEMPERATURE & HUMIDITY
|
|
|
|
float getHumidity();
|
|
|
|
float getTemperature();
|
|
|
|
float getFahrenheit();
|
|
|
|
uint16_t getRawHumidity();
|
|
|
|
uint16_t getRawTemperature();
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2023-04-06 10:54:56 -04:00
|
|
|
// TEMPERATURE & HUMIDITY OFFSET
|
|
|
|
void setTemperatureOffset(float offset = 0);
|
|
|
|
float getTemperatureOffset();
|
|
|
|
void setHumidityOffset(float offset = 0);
|
|
|
|
float getHumidityOffset();
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2021-08-07 06:34:52 -04:00
|
|
|
|
2021-08-24 13:59:51 -04:00
|
|
|
protected:
|
2021-08-07 06:34:52 -04:00
|
|
|
uint8_t crc8(const uint8_t *data, uint8_t len);
|
|
|
|
bool writeCmd(uint16_t cmd);
|
|
|
|
bool readBytes(uint8_t n, uint8_t *val);
|
2021-05-26 06:06:51 -04:00
|
|
|
TwoWire* _wire;
|
|
|
|
|
2021-08-24 12:45:23 -04:00
|
|
|
uint8_t _address;
|
2023-04-06 10:54:56 -04:00
|
|
|
uint8_t _heatTimeout; // seconds
|
2021-08-07 06:34:52 -04:00
|
|
|
uint32_t _lastRead;
|
2023-04-06 10:54:56 -04:00
|
|
|
uint32_t _lastRequest; // for async interface
|
|
|
|
uint32_t _heaterStart; // timestamp
|
|
|
|
uint32_t _heaterStop; // timestamp
|
2021-08-24 12:45:23 -04:00
|
|
|
bool _heaterOn;
|
2023-04-06 10:54:56 -04:00
|
|
|
uint8_t _type; // base class = 0
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2021-08-07 06:34:52 -04:00
|
|
|
uint16_t _rawHumidity;
|
|
|
|
uint16_t _rawTemperature;
|
2021-05-26 06:06:51 -04:00
|
|
|
|
2023-04-06 10:54:56 -04:00
|
|
|
// offset in degrees Celsius
|
|
|
|
float _temperatureOffset = 0;
|
|
|
|
float _humidityOffset = 0;
|
|
|
|
|
|
|
|
|
2021-05-26 06:06:51 -04:00
|
|
|
uint8_t _error;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
//
|
|
|
|
class SHT30 : public SHT
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:40:54 -04:00
|
|
|
SHT30(TwoWire *wire = &Wire);
|
2021-08-24 13:59:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SHT31 : public SHT
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:40:54 -04:00
|
|
|
SHT31(TwoWire *wire = &Wire);
|
2021-08-24 13:59:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SHT35 : public SHT
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:40:54 -04:00
|
|
|
SHT35(TwoWire *wire = &Wire);
|
2021-08-24 13:59:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SHT85 : public SHT
|
|
|
|
{
|
|
|
|
public:
|
2023-09-21 10:40:54 -04:00
|
|
|
SHT85(TwoWire *wire = &Wire);
|
2023-09-06 05:42:30 -04:00
|
|
|
|
|
|
|
// catch incorrect calls with an address, only 0x44 allowed, see #19
|
|
|
|
#if defined(ESP8266) || defined(ESP32)
|
|
|
|
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
|
|
|
|
#endif
|
2023-09-21 10:40:54 -04:00
|
|
|
bool begin(const uint8_t address = SHT_DEFAULT_ADDRESS);
|
2023-09-06 05:42:30 -04:00
|
|
|
|
2023-05-09 06:44:18 -04:00
|
|
|
// EXPERIMENTAL for 0.4.1
|
|
|
|
uint32_t GetSerialNumber();
|
2021-08-24 13:59:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-04-06 10:54:56 -04:00
|
|
|
// -- END OF FILE --
|
2021-12-28 08:21:26 -05:00
|
|
|
|