DHT.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* DHT library
  2. MIT license
  3. written by Adafruit Industries
  4. */
  5. #ifndef DHT_H
  6. #define DHT_H
  7. #if ARDUINO >= 100
  8. #include "Arduino.h"
  9. #else
  10. #include "WProgram.h"
  11. #endif
  12. // Uncomment to enable printing out nice debug messages.
  13. //#define DHT_DEBUG
  14. // Define where debug output will be printed.
  15. #define DEBUG_PRINTER Serial
  16. // Setup debug printing macros.
  17. #ifdef DHT_DEBUG
  18. #define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
  19. #define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
  20. #else
  21. #define DEBUG_PRINT(...) {}
  22. #define DEBUG_PRINTLN(...) {}
  23. #endif
  24. // Define types of sensors.
  25. #define DHT11 11
  26. #define DHT22 22
  27. #define DHT21 21
  28. #define AM2301 21
  29. class DHT {
  30. public:
  31. DHT(uint8_t pin, uint8_t type, uint8_t count=6);
  32. void begin(void);
  33. float readTemperature(bool S=false, bool force=false);
  34. float convertCtoF(float);
  35. float convertFtoC(float);
  36. float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=true);
  37. float readHumidity(bool force=false);
  38. boolean read(bool force=false);
  39. private:
  40. uint8_t data[5];
  41. uint8_t _pin, _type;
  42. #ifdef __AVR
  43. // Use direct GPIO access on an 8-bit AVR so keep track of the port and bitmask
  44. // for the digital pin connected to the DHT. Other platforms will use digitalRead.
  45. uint8_t _bit, _port;
  46. #endif
  47. uint32_t _lastreadtime, _maxcycles;
  48. bool _lastresult;
  49. uint32_t expectPulse(bool level);
  50. };
  51. class InterruptLock {
  52. public:
  53. InterruptLock() {
  54. noInterrupts();
  55. }
  56. ~InterruptLock() {
  57. interrupts();
  58. }
  59. };
  60. #endif