2020-11-27 05:10:47 -05:00
|
|
|
#pragma once
|
2016-12-17 15:03:32 -05:00
|
|
|
//
|
|
|
|
// FILE: dht2pin.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-11-01 15:26:33 -04:00
|
|
|
// VERSION: 0.1.3
|
2016-12-17 15:03:32 -05:00
|
|
|
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
|
2020-11-27 05:10:47 -05:00
|
|
|
// URL: https://github.com/RobTillaart/DHT2pin
|
|
|
|
// http://arduino.cc/playground/Main/DHTLib
|
2022-11-01 15:26:33 -04:00
|
|
|
|
2016-12-17 15:03:32 -05:00
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
2022-11-01 15:26:33 -04:00
|
|
|
|
|
|
|
#define DHT2PIN_LIB_VERSION (F("0.1.3"))
|
2016-12-17 15:03:32 -05:00
|
|
|
|
2021-12-16 07:42:53 -05:00
|
|
|
#define DHTLIB_OK 0
|
|
|
|
#define DHTLIB_ERROR_CHECKSUM -1
|
|
|
|
#define DHTLIB_ERROR_TIMEOUT -2
|
|
|
|
#define DHTLIB_ERROR_CONNECT -3
|
|
|
|
#define DHTLIB_ERROR_ACK_L -4
|
|
|
|
#define DHTLIB_ERROR_ACK_H -5
|
|
|
|
#define DHTLIB_INVALID_VALUE -999
|
2016-12-17 15:03:32 -05:00
|
|
|
|
|
|
|
#define DHTLIB_DHT11_WAKEUP 18
|
|
|
|
#define DHTLIB_DHT_WAKEUP 1
|
|
|
|
|
2022-11-01 15:26:33 -04:00
|
|
|
// max timeout is 100usec.
|
|
|
|
// For a 16Mhz proc that is max 1600 clock cycles
|
|
|
|
// loops using TIMEOUT use at least 4 clock cycli
|
|
|
|
// so 100 us takes max 400 loops
|
|
|
|
// so by dividing F_CPU by 40000 we "fail" as fast as possible
|
2016-12-17 15:03:32 -05:00
|
|
|
#ifdef F_CPU
|
|
|
|
#define DHTLIB_TIMEOUT (F_CPU/40000)
|
|
|
|
#else
|
|
|
|
#define DHTLIB_TIMEOUT (75000000/40000)
|
|
|
|
#endif
|
2021-12-16 07:42:53 -05:00
|
|
|
|
2016-12-17 15:03:32 -05:00
|
|
|
class DHT2pin
|
|
|
|
{
|
|
|
|
public:
|
2022-11-01 15:26:33 -04:00
|
|
|
// return values:
|
|
|
|
// DHTLIB_OK
|
|
|
|
// DHTLIB_ERROR_CHECKSUM
|
|
|
|
// DHTLIB_ERROR_TIMEOUT
|
2021-12-16 07:42:53 -05:00
|
|
|
DHT2pin(uint8_t rpin, uint8_t wpin)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
_rpin = rpin;
|
|
|
|
_wpin = wpin;
|
|
|
|
temperature = 0;
|
|
|
|
humidity = 0;
|
2016-12-17 15:03:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
void begin()
|
|
|
|
{
|
|
|
|
pinMode(_rpin, INPUT);
|
|
|
|
pinMode(_wpin, OUTPUT);
|
|
|
|
}
|
2021-12-16 07:42:53 -05:00
|
|
|
|
2016-12-17 15:03:32 -05:00
|
|
|
int read11();
|
|
|
|
int read();
|
|
|
|
|
|
|
|
inline int read21() { return read(); };
|
|
|
|
inline int read22() { return read(); };
|
|
|
|
inline int read33() { return read(); };
|
|
|
|
inline int read44() { return read(); };
|
|
|
|
|
2017-07-27 09:51:10 -04:00
|
|
|
float humidity;
|
|
|
|
float temperature;
|
2016-12-17 15:03:32 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t _rpin;
|
|
|
|
uint8_t _wpin;
|
2022-11-01 15:26:33 -04:00
|
|
|
uint8_t bits[5]; // buffer to receive data
|
2021-01-29 06:31:58 -05:00
|
|
|
int _readSensor(uint8_t wakeupDelay);
|
2016-12-17 15:03:32 -05:00
|
|
|
};
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2022-11-01 15:26:33 -04:00
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|