GY-63_MS5611/libraries/SHT2x/SHT2x.h

217 lines
4.3 KiB
C
Raw Normal View History

2021-09-27 14:51:23 -04:00
#pragma once
//
// FILE: SHT2x.h
// AUTHOR: Rob Tillaart, Viktor Balint
2022-11-24 08:09:52 -05:00
// VERSION: 0.2.1
2021-09-27 14:51:23 -04:00
// DATE: 2021-09-25
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
// URL: https://github.com/RobTillaart/SHT2x
//
#include "Arduino.h"
#include "Wire.h"
2022-11-24 08:09:52 -05:00
#define SHT2x_LIB_VERSION (F("0.2.1"))
2021-09-27 14:51:23 -04:00
2021-09-29 07:21:17 -04:00
// fields getStatus
#define SHT2x_STATUS_OPEN_CIRCUIT 0x00
#define SHT2x_STATUS_TEMPERATURE 0x01
#define SHT2x_STATUS_HUMIDITY 0x02
#define SHT2x_STATUS_CLOSED_CIRCUIT 0x03
2021-09-27 14:51:23 -04:00
2021-11-17 12:59:50 -05:00
// error codes
2021-09-29 07:21:17 -04:00
// kept in sync with SHT31 library
2021-09-27 14:51:23 -04:00
#define SHT2x_OK 0x00
#define SHT2x_ERR_WRITECMD 0x81
#define SHT2x_ERR_READBYTES 0x82
#define SHT2x_ERR_HEATER_OFF 0x83
#define SHT2x_ERR_NOT_CONNECT 0x84
#define SHT2x_ERR_CRC_TEMP 0x85
#define SHT2x_ERR_CRC_HUM 0x86
#define SHT2x_ERR_CRC_STATUS 0x87 // not used
#define SHT2x_ERR_HEATER_COOLDOWN 0x88
#define SHT2x_ERR_HEATER_ON 0x89
2022-07-13 09:46:33 -04:00
// 0.2.0
#define SHT2x_ERR_RESOLUTION 0x8A
2021-09-27 14:51:23 -04:00
class SHT2x
{
public:
SHT2x();
#if defined(ESP8266) || defined(ESP32)
2022-06-23 09:15:32 -04:00
bool begin(const int dataPin, const int clockPin);
2021-09-27 14:51:23 -04:00
#endif
bool begin(TwoWire *wire = &Wire);
2021-09-29 07:21:17 -04:00
// check sensor is reachable over I2C
2021-09-27 14:51:23 -04:00
bool isConnected();
2021-09-29 07:21:17 -04:00
// read must be called get getTemperature / getHumidity
2021-09-27 14:51:23 -04:00
bool read();
float getTemperature();
float getHumidity();
2022-07-13 09:46:33 -04:00
uint16_t getRawTemperature() { return _rawTemperature; };
uint16_t getRawHumidity() { return _rawHumidity; };
2021-09-27 14:51:23 -04:00
2021-09-29 07:21:17 -04:00
// might take up to 15 milliseconds.
2021-09-27 14:51:23 -04:00
bool reset();
2021-09-29 07:21:17 -04:00
// from datasheet HTU20
//
// | bits | value | meaning |
// |:------:|:------:|:--------------------|
// | 00 | 0 | open circuit |
// | 01 | 1 | temperature reading |
// | 10 | 2 | humidity reading |
// | 11 | 3 | closed circuit |
//
2021-09-27 14:51:23 -04:00
uint8_t getStatus();
// lastRead is in milliSeconds since start
uint32_t lastRead() { return _lastRead; };
2021-09-29 07:21:17 -04:00
// HEATER
// 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.
2021-09-27 14:51:23 -04:00
void setHeatTimeout(uint8_t seconds);
uint8_t getHeatTimeout() { return _heatTimeout; };
bool heatOn();
bool heatOff();
bool isHeaterOn(); // is the sensor still heating up?
2021-11-17 12:59:50 -05:00
bool setHeaterLevel(uint8_t level); // level = 0..15
bool getHeaterLevel(uint8_t & level); // 0..15
2021-09-27 14:51:23 -04:00
int getError(); // clears error flag
2021-11-17 12:59:50 -05:00
// Electronic ID bytes
uint32_t getEIDA();
uint32_t getEIDB();
uint8_t getFirmwareVersion();
2022-07-13 09:46:33 -04:00
// experimental 0.2.0 - needs testing.
// table 8 SHT20 datasheet
// table 7 shows different timing per resolution
// every level is roughly factor 2 in time.
// RES HUM TEMP
// 0 12 bit 14 bit
// 1 08 bit 12 bit
// 2 10 bit 13 bit
// 3 11 bit 11 bit
// 4..255 returns false
bool setResolution(uint8_t res = 0);
// returns RES set (cached value)
uint8_t getResolution();
bool batteryOK();
2022-11-24 08:09:52 -05:00
protected:
2021-09-27 14:51:23 -04:00
uint8_t crc8(const uint8_t *data, uint8_t len);
bool writeCmd(uint8_t cmd);
bool writeCmd(uint8_t cmd, uint8_t value);
bool readBytes(uint8_t n, uint8_t *val, uint8_t maxDuration);
TwoWire* _wire;
2022-11-24 08:09:52 -05:00
uint8_t _heatTimeout; // seconds
2021-09-27 14:51:23 -04:00
uint32_t _lastRead;
2022-11-24 08:09:52 -05:00
uint32_t _lastRequest; // for async interface
2021-09-27 14:51:23 -04:00
uint32_t _heaterStart;
uint32_t _heaterStop;
bool _heaterOn;
uint16_t _rawHumidity;
uint16_t _rawTemperature;
uint8_t _status;
uint8_t _error;
2022-07-13 09:46:33 -04:00
uint8_t _resolution;
2021-09-27 14:51:23 -04:00
};
////////////////////////////////////////////////////////
//
2021-09-29 07:21:17 -04:00
// DERIVED SHT
2021-09-27 14:51:23 -04:00
//
class SHT20 : public SHT2x
{
public:
SHT20();
};
2021-09-29 07:21:17 -04:00
2021-09-27 14:51:23 -04:00
class SHT21 : public SHT2x
{
public:
SHT21();
};
class SHT25 : public SHT2x
{
public:
SHT25();
};
2021-09-29 07:21:17 -04:00
////////////////////////////////////////////////////////
//
// DERIVED HTU
2021-11-17 12:59:50 -05:00
//
2021-09-29 07:21:17 -04:00
class HTU20 : public SHT2x
{
public:
HTU20();
};
class HTU21 : public SHT2x
{
public:
HTU21();
};
2021-11-17 12:59:50 -05:00
////////////////////////////////////////////////////////
//
// DERIVED Si70xx
//
class Si7013 : public SHT2x
{
public:
Si7013();
};
class Si7020 : public SHT2x
{
public:
Si7020();
};
class Si7021 : public SHT2x
{
public:
Si7021();
};
2021-09-27 14:51:23 -04:00
// -- END OF FILE --