DHT.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef DHT_H
  2. #define DHT_H
  3. #if ARDUINO >= 100
  4. #include "Arduino.h"
  5. #else
  6. #include "WProgram.h"
  7. #endif
  8. #include <Wire.h>
  9. // 8 MHz(ish) AVR ---------------------------------------------------------
  10. #if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL)
  11. #define COUNT 3
  12. // 16 MHz(ish) AVR --------------------------------------------------------
  13. #elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L)
  14. #define COUNT 6
  15. // 48MHz SAMD21J18A (Sodaq Explorer)
  16. #elif (F_CPU == 48000000UL)
  17. #define COUNT 18
  18. // 64MHz NRF52840
  19. #elif (F_CPU == 64000000UL)
  20. #define COUNT 20
  21. // 168MHz STM32F405 STM32F407
  22. #elif (F_CPU == 168000000L)
  23. #define COUNT 40
  24. #elif (F_CPU == 80000000L)
  25. #define COUNT 22
  26. #elif (F_CPU == 160000000L)
  27. #define COUNT 32
  28. #else
  29. #define COUNT 25
  30. //#error "CPU SPEED NOT SUPPORTED"
  31. #endif
  32. //RP2040
  33. #if defined(ARDUINO_ARCH_RP2040)
  34. #define COUNT 10
  35. #endif
  36. /* DHT library
  37. MIT license
  38. written by Adafruit Industries
  39. */
  40. // how many timing transitions we need to keep track of. 2 * number bits + extra
  41. #define MAXTIMINGS 85
  42. #define SERIALPRINT Serial
  43. #define DEFAULT_IIC_ADDR 0x38
  44. #define RESET_REG_ADDR 0xba
  45. #define HUMIDITY_INDEX 0
  46. #define TEMPRATURE_INDEX 1
  47. #define DHT11 11
  48. #define DHT22 22
  49. #define DHT21 21
  50. #define AM2301 21
  51. #define DHT10 10
  52. #define DHT20 10
  53. class DHT {
  54. private:
  55. uint8_t data[6];
  56. uint8_t _pin, _type, _count;
  57. boolean read(void);
  58. unsigned long _lastreadtime;
  59. boolean firstreading;
  60. public:
  61. DHT(uint8_t pin, uint8_t type, uint8_t count = COUNT);
  62. DHT(uint8_t type);
  63. void begin(void);
  64. float readTemperature(bool S = false);
  65. float convertCtoF(float);
  66. float readHumidity(void);
  67. /** Common interface to get temp&humi value.support all DHT device.
  68. @return 0 for calibrated failed,1 for succeed.
  69. **/
  70. int readTempAndHumidity(float* data);
  71. // DHT10 digital interfaces(i2c),onlu for DHT10 .
  72. int i2cReadByte(uint8_t& byte);
  73. int i2cReadBytes(uint8_t* bytes, uint32_t len);
  74. int i2cWriteBytes(uint8_t* bytes, uint32_t len);
  75. int i2cWriteByte(uint8_t byte);
  76. /** Reset sensor.
  77. @return 0 for calibrated failed,1 for succeed.
  78. **/
  79. int DHT10Reset(void);
  80. /** Read status register.check the calibration flag - bit[3]: 1- calibrated ok ,0 - Not calibrated.
  81. @return 0 for calibrated failed,1 for succeed.
  82. **/
  83. int DHT10ReadStatus(void);
  84. /** Init sensor,send 0x08,0x00 to register 0xe1.
  85. @ return : 0 if success, non-zero if failed.
  86. **/
  87. int setSystemCfg(void);
  88. /** Read temp & humi result buf from sensor.
  89. total 6 bytes,the first byte for status register,other 5 bytes for temp & humidity data.
  90. @ return : 0 if success, non-zero if failed.
  91. **/
  92. int readTargetData(uint32_t* data);
  93. /** DHT10 Init function.
  94. Reset sensor and wait for calibration complete.
  95. @ return : 0 if success, non-zero if failed.
  96. **/
  97. int DHT10Init(void);
  98. };
  99. #endif