DHT.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #define DHT11 11 /**< DHT TYPE 11 */
  39. #define DHT12 12 /**< DHY TYPE 12 */
  40. #define DHT22 22 /**< DHT TYPE 22 */
  41. #define DHT21 21 /**< DHT TYPE 21 */
  42. #define AM2301 21 /**< AM2301 */
  43. /*!
  44. * @brief Class that stores state and functions for DHT
  45. */
  46. class DHT {
  47. public:
  48. DHT(uint8_t pin, uint8_t type, uint8_t count = 6);
  49. void begin(uint8_t usec = 55);
  50. float readTemperature(bool S = false, bool force = false);
  51. float convertCtoF(float);
  52. float convertFtoC(float);
  53. float computeHeatIndex(bool isFahrenheit = true);
  54. float computeHeatIndex(float temperature, float percentHumidity,
  55. bool isFahrenheit = true);
  56. float readHumidity(bool force = false);
  57. bool read(bool force = false);
  58. private:
  59. uint8_t data[5];
  60. uint8_t _pin, _type;
  61. #ifdef __AVR
  62. // Use direct GPIO access on an 8-bit AVR so keep track of the port and
  63. // bitmask for the digital pin connected to the DHT. Other platforms will use
  64. // digitalRead.
  65. uint8_t _bit, _port;
  66. #endif
  67. uint32_t _lastreadtime, _maxcycles;
  68. bool _lastresult;
  69. uint8_t pullTime; // Time (in usec) to pull up data line before reading
  70. uint32_t expectPulse(bool level);
  71. };
  72. /*!
  73. * @brief Class that defines Interrupt Lock Avaiability
  74. */
  75. class InterruptLock {
  76. public:
  77. InterruptLock() {
  78. #if !defined(ARDUINO_ARCH_NRF52)
  79. noInterrupts();
  80. #endif
  81. }
  82. ~InterruptLock() {
  83. #if !defined(ARDUINO_ARCH_NRF52)
  84. interrupts();
  85. #endif
  86. }
  87. };
  88. #endif