DHT sensor library
DHT.h
Go to the documentation of this file.
1 
18 #ifndef DHT_H
19 #define DHT_H
20 
21 #include "Arduino.h"
22 
23 /* Uncomment to enable printing out nice debug messages. */
24 //#define DHT_DEBUG
25 
26 #define DEBUG_PRINTER \
27  Serial
30 /* Setup debug printing macros. */
31 #ifdef DHT_DEBUG
32 #define DEBUG_PRINT(...) \
33  { DEBUG_PRINTER.print(__VA_ARGS__); }
34 #define DEBUG_PRINTLN(...) \
35  { DEBUG_PRINTER.println(__VA_ARGS__); }
36 #else
37 #define DEBUG_PRINT(...) \
38  {}
39 #define DEBUG_PRINTLN(...) \
40  {}
41 #endif
42 
43 /* Define types of sensors. */
44 #define DHT11 11
45 #define DHT12 12
46 #define DHT22 22
47 #define DHT21 21
48 #define AM2301 21
53 class DHT {
54 public:
55  DHT(uint8_t pin, uint8_t type, uint8_t count = 6);
56  void begin(uint8_t usec = 55);
57  float readTemperature(bool S = false, bool force = false);
58  float convertCtoF(float);
59  float convertFtoC(float);
60  float computeHeatIndex(bool isFahrenheit = true);
61  float computeHeatIndex(float temperature, float percentHumidity,
62  bool isFahrenheit = true);
63  float readHumidity(bool force = false);
64  bool read(bool force = false);
65 
66 private:
67  uint8_t data[5];
68  uint8_t _pin, _type;
69 #ifdef __AVR
70  // Use direct GPIO access on an 8-bit AVR so keep track of the port and
71  // bitmask for the digital pin connected to the DHT. Other platforms will use
72  // digitalRead.
73  uint8_t _bit, _port;
74 #endif
75  uint32_t _lastreadtime, _maxcycles;
76  bool _lastresult;
77  uint8_t pullTime; // Time (in usec) to pull up data line before reading
78 
79  uint32_t expectPulse(bool level);
80 };
81 
85 class InterruptLock {
86 public:
87  InterruptLock() {
88 #if !defined(ARDUINO_ARCH_NRF52)
89  noInterrupts();
90 #endif
91  }
92  ~InterruptLock() {
93 #if !defined(ARDUINO_ARCH_NRF52)
94  interrupts();
95 #endif
96  }
97 };
98 
99 #endif
Class that stores state and functions for DHT.
Definition: DHT.h:56
Class that defines Interrupt Lock Avaiability.
Definition: DHT.h:88