119 lines
3.2 KiB
C
Raw Normal View History

2020-11-27 11:33:55 +01:00
#pragma once
2019-02-18 14:01:41 +01:00
//
// FILE: SHT31.h
// AUTHOR: Rob Tillaart
2023-09-21 16:39:19 +02:00
// VERSION: 0.4.0
2019-02-18 14:01:41 +01:00
// DATE: 2019-02-08
2020-11-27 11:33:55 +01:00
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
2019-02-18 14:01:41 +01:00
// https://www.adafruit.com/product/2857
2020-11-27 11:33:55 +01:00
// URL: https://github.com/RobTillaart/SHT31
2019-02-18 14:01:41 +01:00
2021-01-29 12:31:58 +01:00
2019-02-18 14:01:41 +01:00
#include "Arduino.h"
2023-09-21 16:39:19 +02:00
#include "Wire.h"
2019-02-18 14:01:41 +01:00
2021-01-29 12:31:58 +01:00
2023-09-21 16:39:19 +02:00
#define SHT31_LIB_VERSION (F("0.4.0"))
2021-01-29 12:31:58 +01:00
2023-09-21 16:39:19 +02:00
#ifndef SHT_DEFAULT_ADDRESS
2022-01-18 12:30:33 +01:00
#define SHT_DEFAULT_ADDRESS 0x44
#endif
2020-11-27 11:33:55 +01:00
2023-03-22 21:52:02 +01:00
// fields readStatus
2020-11-27 11:33:55 +01:00
#define SHT31_STATUS_ALERT_PENDING (1 << 15)
#define SHT31_STATUS_HEATER_ON (1 << 13)
#define SHT31_STATUS_HUM_TRACK_ALERT (1 << 11)
#define SHT31_STATUS_TEMP_TRACK_ALERT (1 << 10)
#define SHT31_STATUS_SYSTEM_RESET (1 << 4)
#define SHT31_STATUS_COMMAND_STATUS (1 << 1)
#define SHT31_STATUS_WRITE_CRC_STATUS (1 << 0)
2019-02-18 14:01:41 +01:00
2023-03-22 21:52:02 +01:00
// error codes
2021-01-29 12:31:58 +01:00
#define SHT31_OK 0x00
#define SHT31_ERR_WRITECMD 0x81
#define SHT31_ERR_READBYTES 0x82
#define SHT31_ERR_HEATER_OFF 0x83
#define SHT31_ERR_NOT_CONNECT 0x84
#define SHT31_ERR_CRC_TEMP 0x85
#define SHT31_ERR_CRC_HUM 0x86
#define SHT31_ERR_CRC_STATUS 0x87
2021-08-24 16:01:55 +02:00
#define SHT31_ERR_HEATER_COOLDOWN 0x88
#define SHT31_ERR_HEATER_ON 0x89
2021-01-29 12:31:58 +01:00
2019-02-18 14:01:41 +01:00
class SHT31
{
public:
2023-09-21 16:39:19 +02:00
SHT31(TwoWire *wire = &Wire);
2019-02-18 14:01:41 +01:00
2020-11-27 11:33:55 +01:00
#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
2023-03-22 21:52:02 +01:00
// use SHT_DEFAULT_ADDRESS
2022-01-18 12:30:33 +01:00
bool begin(const uint8_t dataPin, const uint8_t clockPin);
2020-11-27 11:33:55 +01:00
#endif
2023-09-21 16:39:19 +02:00
bool begin(const uint8_t address = SHT_DEFAULT_ADDRESS);
2020-11-27 11:33:55 +01:00
2023-03-22 21:52:02 +01:00
// blocks 15 milliseconds + actual read + math
2019-02-18 14:01:41 +01:00
bool read(bool fast = true);
2023-03-22 21:52:02 +01:00
// check sensor is reachable over I2C
virtual bool isConnected();
2021-01-29 12:31:58 +01:00
2023-03-22 21:52:02 +01:00
// details see datasheet; summary in SHT31.cpp file
2019-02-18 14:01:41 +01:00
uint16_t readStatus();
2023-03-22 21:52:02 +01:00
// lastRead is in milliSeconds since start
2019-02-18 14:01:41 +01:00
uint32_t lastRead() { return _lastRead; };
2021-01-29 12:31:58 +01:00
bool reset(bool hard = false);
2019-03-05 18:45:36 +01:00
2023-03-22 21:52:02 +01: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.
2020-11-27 11:33:55 +01:00
void setHeatTimeout(uint8_t seconds);
2021-08-24 16:01:55 +02:00
uint8_t getHeatTimeout() { return _heatTimeout; };
2021-01-29 12:31:58 +01:00
bool heatOn();
bool heatOff();
bool isHeaterOn(); // is the sensor still heating up?
bool heatUp() { return isHeaterOn(); }; // will be obsolete in future
2019-02-18 14:01:41 +01:00
2022-01-18 12:30:33 +01:00
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; };
2019-02-18 14:01:41 +01:00
2019-03-05 18:45:36 +01:00
// ASYNC INTERFACE
2021-01-29 12:31:58 +01:00
bool requestData();
2019-03-05 18:45:36 +01:00
bool dataReady();
2020-11-27 11:33:55 +01:00
bool readData(bool fast = true);
2019-03-05 18:45:36 +01:00
2023-03-22 21:52:02 +01:00
int getError(); // clears error flag
2019-02-18 14:01:41 +01:00
2023-03-22 21:52:02 +01:00
protected:
2021-08-24 16:01:55 +02:00
uint8_t _address;
2023-03-22 21:52:02 +01:00
uint8_t _heatTimeout; // seconds
2019-02-18 14:01:41 +01:00
uint32_t _lastRead;
2023-03-22 21:52:02 +01:00
uint32_t _lastRequest; // for async interface
2020-11-27 11:33:55 +01:00
uint32_t _heaterStart;
2021-08-24 16:01:55 +02:00
uint32_t _heaterStop;
bool _heaterOn;
2020-11-27 11:33:55 +01:00
2022-01-18 12:30:33 +01:00
uint16_t _rawHumidity;
uint16_t _rawTemperature;
2021-01-29 12:31:58 +01:00
uint8_t _error;
2023-03-22 21:52:02 +01:00
private:
uint8_t crc8(const uint8_t *data, uint8_t len);
virtual bool writeCmd(uint16_t cmd);
virtual bool readBytes(uint8_t n, uint8_t *val);
TwoWire* _wire;
2019-02-18 14:01:41 +01:00
};
2023-03-22 21:52:02 +01:00
// -- END OF FILE --