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