DHT.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*!
  2. * @file DHT.h
  3. *
  4. * This is a library for DHT series of low cost temperature/humidity sensors.
  5. *
  6. * You must have Adafruit Unified Sensor Library library installed to use this
  7. * class.
  8. *
  9. * Adafruit invests time and resources providing this open source code,
  10. * please support Adafruit andopen-source hardware by purchasing products
  11. * from Adafruit!
  12. *
  13. * Written by Adafruit Industries.
  14. *
  15. * MIT license, all text above must be included in any redistribution
  16. */
  17. #ifndef DHT_H
  18. #define DHT_H
  19. #include "Arduino.h"
  20. /* Uncomment to enable printing out nice debug messages. */
  21. //#define DHT_DEBUG
  22. #define DEBUG_PRINTER \
  23. Serial /**< Define where debug output will be printed. \
  24. */
  25. /* Setup debug printing macros. */
  26. #ifdef DHT_DEBUG
  27. #define DEBUG_PRINT(...) \
  28. { DEBUG_PRINTER.print(__VA_ARGS__); }
  29. #define DEBUG_PRINTLN(...) \
  30. { DEBUG_PRINTER.println(__VA_ARGS__); }
  31. #else
  32. #define DEBUG_PRINT(...) \
  33. {} /**< Debug Print Placeholder if Debug is disabled */
  34. #define DEBUG_PRINTLN(...) \
  35. {} /**< Debug Print Line Placeholder if Debug is disabled */
  36. #endif
  37. /* Define types of sensors. */
  38. static const uint8_t DHT11{11}; /**< DHT TYPE 11 */
  39. static const uint8_t DHT12{12}; /**< DHY TYPE 12 */
  40. static const uint8_t DHT21{21}; /**< DHT TYPE 21 */
  41. static const uint8_t DHT22{22}; /**< DHT TYPE 22 */
  42. static const uint8_t AM2301{21}; /**< AM2301 */
  43. #if defined(TARGET_NAME) && (TARGET_NAME == ARDUINO_NANO33BLE)
  44. #ifndef microsecondsToClockCycles
  45. /*!
  46. * As of 7 Sep 2020 the Arduino Nano 33 BLE boards do not have
  47. * microsecondsToClockCycles defined.
  48. */
  49. #define microsecondsToClockCycles(a) ((a) * (SystemCoreClock / 1000000L))
  50. #endif
  51. #endif
  52. /*!
  53. * @brief Class that stores state and functions for DHT
  54. */
  55. class DHT {
  56. public:
  57. DHT(uint8_t pin, uint8_t type, uint8_t count = 6);
  58. void begin(uint8_t usec = 55);
  59. float readTemperature(bool S = false, bool force = false);
  60. float convertCtoF(float);
  61. float convertFtoC(float);
  62. float computeHeatIndex(bool isFahrenheit = true);
  63. float computeHeatIndex(float temperature, float percentHumidity,
  64. bool isFahrenheit = true);
  65. float readHumidity(bool force = false);
  66. bool read(bool force = false);
  67. private:
  68. uint8_t data[5];
  69. uint8_t _pin, _type;
  70. #ifdef __AVR
  71. // Use direct GPIO access on an 8-bit AVR so keep track of the port and
  72. // bitmask for the digital pin connected to the DHT. Other platforms will use
  73. // digitalRead.
  74. uint8_t _bit, _port;
  75. #endif
  76. uint32_t _lastreadtime, _maxcycles;
  77. bool _lastresult;
  78. uint8_t pullTime; // Time (in usec) to pull up data line before reading
  79. uint32_t expectPulse(bool level);
  80. };
  81. /*!
  82. * @brief Class that defines Interrupt Lock Avaiability
  83. */
  84. class InterruptLock {
  85. public:
  86. InterruptLock() {
  87. #if !defined(ARDUINO_ARCH_NRF52)
  88. noInterrupts();
  89. #endif
  90. }
  91. ~InterruptLock() {
  92. #if !defined(ARDUINO_ARCH_NRF52)
  93. interrupts();
  94. #endif
  95. }
  96. };
  97. #endif