DHT.h 2.5 KB

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