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

81 lines
2.0 KiB
C
Raw Normal View History

2020-11-27 05:33:55 -05:00
#pragma once
2019-02-18 08:01:41 -05:00
//
// FILE: SHT31.h
// AUTHOR: Rob Tillaart
2020-11-27 05:33:55 -05:00
// VERSION: 0.2.2
2019-02-18 08:01:41 -05:00
// DATE: 2019-02-08
2020-11-27 05:33:55 -05:00
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
2019-02-18 08:01:41 -05:00
// https://www.adafruit.com/product/2857
2020-11-27 05:33:55 -05:00
// URL: https://github.com/RobTillaart/SHT31
2019-02-18 08:01:41 -05:00
//
#include "Arduino.h"
#include "Wire.h"
2020-11-27 05:33:55 -05:00
#define SHT31_LIB_VERSION "0.2.2"
// fields readStatus
#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 08:01:41 -05:00
class SHT31
{
public:
SHT31();
2020-11-27 05:33:55 -05:00
#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
#endif
2019-02-18 08:01:41 -05:00
bool begin(const uint8_t address);
2020-11-27 05:33:55 -05:00
bool begin(const uint8_t address, TwoWire *wire);
// blocks 15 milliseconds + actual read + math
2019-02-18 08:01:41 -05:00
bool read(bool fast = true);
2020-11-27 05:33:55 -05:00
// details see datasheet; summary in SHT31.cpp file
2019-02-18 08:01:41 -05:00
uint16_t readStatus();
2020-11-27 05:33:55 -05:00
// lastRead is in milliSeconds since start
2019-02-18 08:01:41 -05:00
uint32_t lastRead() { return _lastRead; };
2020-11-27 05:33:55 -05:00
void reset(bool hard = false);
2019-03-05 12:45:36 -05:00
// do not use heater for long periods,
// use it for max 3 minutes to heat up
// and let it cool down an equal period.
2020-11-27 05:33:55 -05:00
void setHeatTimeout(uint8_t seconds);
2019-02-18 08:01:41 -05:00
void heatOn();
void heatOff();
2020-11-27 05:33:55 -05:00
bool heatUp(); // is the sensor still heating up?
2019-02-18 08:01:41 -05:00
2020-11-27 05:33:55 -05:00
float getHumidity() { return humidity; };
float getTemperature() { return temperature; };
2019-02-18 08:01:41 -05:00
2019-03-05 12:45:36 -05:00
// ASYNC INTERFACE
void requestData();
bool dataReady();
2020-11-27 05:33:55 -05:00
bool readData(bool fast = true);
2019-03-05 12:45:36 -05:00
2019-02-18 08:01:41 -05:00
private:
2020-11-27 05:33:55 -05:00
uint8_t crc8(const uint8_t *data, int len);
2019-02-18 08:01:41 -05:00
void writeCmd(uint16_t cmd);
void readBytes(uint8_t n, uint8_t *val);
2020-11-27 05:33:55 -05:00
TwoWire* _wire;
2019-02-18 08:01:41 -05:00
uint8_t _addr;
2020-11-27 05:33:55 -05:00
uint8_t _heatTimeOut; // seconds
2019-02-18 08:01:41 -05:00
uint32_t _lastRead;
2019-03-05 12:45:36 -05:00
uint32_t _lastRequest; // for async interface
2020-11-27 05:33:55 -05:00
uint32_t _heaterStart;
float humidity;
float temperature;
2019-02-18 08:01:41 -05:00
};
2020-11-27 05:33:55 -05:00
// -- END OF FILE --