2019-07-04 18:06:42 +03:00
|
|
|
/*!
|
|
|
|
* @file DHT.h
|
|
|
|
*
|
|
|
|
* This is a library for DHT series of low cost temperature/humidity sensors.
|
|
|
|
*
|
2020-04-08 21:36:24 +03:00
|
|
|
* You must have Adafruit Unified Sensor Library library installed to use this
|
|
|
|
* class.
|
2019-07-04 18:06:42 +03:00
|
|
|
*
|
|
|
|
* Adafruit invests time and resources providing this open source code,
|
|
|
|
* please support Adafruit andopen-source hardware by purchasing products
|
|
|
|
* from Adafruit!
|
|
|
|
*
|
|
|
|
* Written by Adafruit Industries.
|
|
|
|
*
|
|
|
|
* MIT license, all text above must be included in any redistribution
|
|
|
|
*/
|
2015-06-27 01:14:17 +03:00
|
|
|
|
|
|
|
#ifndef DHT_H
|
|
|
|
#define DHT_H
|
|
|
|
|
2019-07-04 18:06:42 +03:00
|
|
|
#include "Arduino.h"
|
2015-06-27 01:14:17 +03:00
|
|
|
|
2019-07-04 18:06:42 +03:00
|
|
|
/* Uncomment to enable printing out nice debug messages. */
|
2015-06-27 01:14:17 +03:00
|
|
|
//#define DHT_DEBUG
|
|
|
|
|
2020-04-08 21:36:24 +03:00
|
|
|
#define DEBUG_PRINTER \
|
|
|
|
Serial /**< Define where debug output will be printed. \
|
|
|
|
*/
|
2019-07-04 18:06:42 +03:00
|
|
|
|
|
|
|
/* Setup debug printing macros. */
|
2015-06-27 01:14:17 +03:00
|
|
|
#ifdef DHT_DEBUG
|
2020-04-08 21:36:24 +03:00
|
|
|
#define DEBUG_PRINT(...) \
|
|
|
|
{ DEBUG_PRINTER.print(__VA_ARGS__); }
|
|
|
|
#define DEBUG_PRINTLN(...) \
|
|
|
|
{ DEBUG_PRINTER.println(__VA_ARGS__); }
|
2015-06-27 01:14:17 +03:00
|
|
|
#else
|
2020-04-08 21:36:24 +03:00
|
|
|
#define DEBUG_PRINT(...) \
|
|
|
|
{} /**< Debug Print Placeholder if Debug is disabled */
|
|
|
|
#define DEBUG_PRINTLN(...) \
|
|
|
|
{} /**< Debug Print Line Placeholder if Debug is disabled */
|
2015-06-27 01:14:17 +03:00
|
|
|
#endif
|
|
|
|
|
2019-07-04 18:06:42 +03:00
|
|
|
/* Define types of sensors. */
|
2020-04-08 21:36:24 +03:00
|
|
|
#define DHT11 11 /**< DHT TYPE 11 */
|
|
|
|
#define DHT12 12 /**< DHY TYPE 12 */
|
|
|
|
#define DHT22 22 /**< DHT TYPE 22 */
|
|
|
|
#define DHT21 21 /**< DHT TYPE 21 */
|
2019-07-04 18:06:42 +03:00
|
|
|
#define AM2301 21 /**< AM2301 */
|
2015-06-27 01:14:17 +03:00
|
|
|
|
2020-04-08 21:36:24 +03:00
|
|
|
/*!
|
2019-07-04 18:06:42 +03:00
|
|
|
* @brief Class that stores state and functions for DHT
|
|
|
|
*/
|
2015-06-27 01:14:17 +03:00
|
|
|
class DHT {
|
2020-04-08 21:36:24 +03:00
|
|
|
public:
|
|
|
|
DHT(uint8_t pin, uint8_t type, uint8_t count = 6);
|
|
|
|
void begin(uint8_t usec = 55);
|
|
|
|
float readTemperature(bool S = false, bool force = false);
|
|
|
|
float convertCtoF(float);
|
|
|
|
float convertFtoC(float);
|
|
|
|
float computeHeatIndex(bool isFahrenheit = true);
|
|
|
|
float computeHeatIndex(float temperature, float percentHumidity,
|
|
|
|
bool isFahrenheit = true);
|
|
|
|
float readHumidity(bool force = false);
|
|
|
|
bool read(bool force = false);
|
2015-06-27 01:14:17 +03:00
|
|
|
|
2020-04-08 21:36:24 +03:00
|
|
|
private:
|
2015-07-24 19:08:47 +03:00
|
|
|
uint8_t data[5];
|
2015-10-13 07:30:01 +03:00
|
|
|
uint8_t _pin, _type;
|
2020-04-08 21:36:24 +03:00
|
|
|
#ifdef __AVR
|
|
|
|
// Use direct GPIO access on an 8-bit AVR so keep track of the port and
|
|
|
|
// bitmask for the digital pin connected to the DHT. Other platforms will use
|
|
|
|
// digitalRead.
|
|
|
|
uint8_t _bit, _port;
|
|
|
|
#endif
|
2015-07-22 22:13:56 +03:00
|
|
|
uint32_t _lastreadtime, _maxcycles;
|
2015-06-27 01:14:17 +03:00
|
|
|
bool _lastresult;
|
2019-02-14 20:24:17 +03:00
|
|
|
uint8_t pullTime; // Time (in usec) to pull up data line before reading
|
2015-06-27 01:14:17 +03:00
|
|
|
|
|
|
|
uint32_t expectPulse(bool level);
|
|
|
|
};
|
|
|
|
|
2020-04-08 21:36:24 +03:00
|
|
|
/*!
|
2019-07-04 18:06:42 +03:00
|
|
|
* @brief Class that defines Interrupt Lock Avaiability
|
|
|
|
*/
|
2015-07-02 03:53:53 +03:00
|
|
|
class InterruptLock {
|
2020-04-08 21:36:24 +03:00
|
|
|
public:
|
|
|
|
InterruptLock() {
|
|
|
|
#if !defined(ARDUINO_ARCH_NRF52)
|
2015-07-02 03:53:53 +03:00
|
|
|
noInterrupts();
|
2018-08-14 22:44:40 +03:00
|
|
|
#endif
|
2020-04-08 21:36:24 +03:00
|
|
|
}
|
|
|
|
~InterruptLock() {
|
|
|
|
#if !defined(ARDUINO_ARCH_NRF52)
|
2015-07-02 03:53:53 +03:00
|
|
|
interrupts();
|
2018-08-14 22:44:40 +03:00
|
|
|
#endif
|
2020-04-08 21:36:24 +03:00
|
|
|
}
|
2015-07-02 03:53:53 +03:00
|
|
|
};
|
|
|
|
|
2015-06-27 01:14:17 +03:00
|
|
|
#endif
|