DHT.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. int readTemperatureInt(bool S=false, bool force=false);
  35. float convertCtoF(float);
  36. float convertFtoC(float);
  37. float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=true);
  38. float readHumidity(bool force=false);
  39. int readHumidityInt(bool force=false);
  40. boolean read(bool force=false);
  41. private:
  42. uint8_t data[5];
  43. uint8_t _pin, _type;
  44. #ifdef __AVR
  45. // Use direct GPIO access on an 8-bit AVR so keep track of the port and bitmask
  46. // for the digital pin connected to the DHT. Other platforms will use digitalRead.
  47. uint8_t _bit, _port;
  48. #endif
  49. uint32_t _lastreadtime, _maxcycles;
  50. bool _lastresult;
  51. uint32_t expectPulse(bool level);
  52. };
  53. class InterruptLock {
  54. public:
  55. InterruptLock() {
  56. noInterrupts();
  57. }
  58. ~InterruptLock() {
  59. interrupts();
  60. }
  61. };
  62. #endif