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

123 lines
2.6 KiB
C
Raw Normal View History

2022-01-11 14:48:39 -05:00
#pragma once
//
// FILE: DHT20.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
2022-12-21 15:48:21 -05:00
// VERSION: 0.2.1
2022-01-11 14:48:39 -05:00
// HISTORY: See DHT20.cpp
// URL: https://github.com/RobTillaart/DHT20
//
2022-09-11 05:44:04 -04:00
// Always check datasheet - front view
//
// +--------------+
// VDD ----| 1 |
// SDA ----| 2 DHT20 |
// GND ----| 3 |
// SCL ----| 4 |
// +--------------+
2022-01-11 14:48:39 -05:00
#include "Arduino.h"
#include "Wire.h"
2022-12-21 15:48:21 -05:00
#define DHT20_LIB_VERSION (F("0.2.1"))
2022-01-11 14:48:39 -05:00
#define DHT20_OK 0
#define DHT20_ERROR_CHECKSUM -10
#define DHT20_ERROR_CONNECT -11
#define DHT20_MISSING_BYTES -12
2022-09-16 13:27:01 -04:00
#define DHT20_ERROR_BYTES_ALL_ZERO -13
2022-09-17 07:50:11 -04:00
#define DHT20_ERROR_READ_TIMEOUT -14
#define DHT20_ERROR_LASTREAD -15
2022-01-11 14:48:39 -05:00
class DHT20
{
public:
2022-09-18 13:13:39 -04:00
// CONSTRUCTOR
// fixed address 0x38
DHT20(TwoWire *wire = &Wire);
2022-01-11 14:48:39 -05:00
2022-09-18 13:13:39 -04:00
// start the I2C
2022-01-11 14:48:39 -05:00
#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t dataPin, const uint8_t clockPin);
#endif
bool begin();
bool isConnected();
2022-12-21 15:48:21 -05:00
uint8_t getAddress();
2022-01-11 14:48:39 -05:00
2022-09-18 13:13:39 -04:00
2022-09-11 05:44:04 -04:00
// ASYNCHRONUOUS CALL
2022-09-18 13:13:39 -04:00
// trigger acquisition.
2022-09-17 07:50:11 -04:00
int requestData();
2022-09-18 13:13:39 -04:00
// read the raw data.
2022-09-17 07:50:11 -04:00
int readData();
2022-09-18 13:13:39 -04:00
// converts raw databits to temperature and humidity.
2022-09-11 05:44:04 -04:00
int convert();
2022-01-11 14:48:39 -05:00
2022-09-18 13:13:39 -04:00
2022-09-11 05:44:04 -04:00
// SYNCHRONUOUS CALL
2022-09-18 13:13:39 -04:00
// blocking read call to read + convert data
2022-01-11 14:48:39 -05:00
int read();
2022-09-18 13:13:39 -04:00
// access the converted temperature & humidity
2022-09-17 07:50:11 -04:00
float getHumidity();
float getTemperature();
2022-09-18 13:13:39 -04:00
// OFFSET 1st order adjustments
2022-09-17 07:50:11 -04:00
void setHumOffset(float offset);
void setTempOffset(float offset);
float getHumOffset();
float getTempOffset();
2022-09-18 13:13:39 -04:00
2022-09-17 07:50:11 -04:00
// READ STATUS
uint8_t readStatus();
2022-09-18 13:13:39 -04:00
// 3 wrapper functions around readStatus()
2022-09-17 07:50:11 -04:00
bool isCalibrated();
bool isMeasuring();
bool isIdle();
2022-09-18 13:13:39 -04:00
// status from last read()
2022-09-17 07:50:11 -04:00
int internalStatus();
2022-01-11 14:48:39 -05:00
2022-09-18 13:13:39 -04:00
// TIMING
2022-09-17 07:50:11 -04:00
uint32_t lastRead();
uint32_t lastRequest();
2022-01-11 14:48:39 -05:00
2022-09-18 13:13:39 -04:00
// RESET (new since 0.1.4)
// use with care
// returns number of registers reset => must be 3
// 3 = OK
// 0,1,2 = error.
// 255 = no reset needed.
// See datasheet 7.4 Sensor Reading Process, point 1
// use with care
uint8_t resetSensor();
2022-01-11 14:48:39 -05:00
private:
float _humidity;
float _temperature;
float _humOffset;
float _tempOffset;
2022-09-17 07:50:11 -04:00
2022-01-11 14:48:39 -05:00
uint8_t _status;
uint32_t _lastRequest;
uint32_t _lastRead;
uint8_t _bits[7];
uint8_t _crc8(uint8_t *ptr, uint8_t len);
2022-09-18 13:13:39 -04:00
// use with care
bool _resetRegister(uint8_t reg);
2022-01-11 14:48:39 -05:00
TwoWire* _wire;
};
// -- END OF FILE --