Erriez DHT22 library for Arduino  1.2.1
AM2302/AM2303 DHT22 temperature and humidity sensor library for Arduino
ErriezDHT22.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018-2020 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 #ifndef ERRIEZ_DHT22_H_
34 #define ERRIEZ_DHT22_H_
35 
36 #include <Arduino.h>
37 
39 // #define DEBUG_PRINT
40 
42 #define DHT22_MIN_READ_INTERVAL 2000
43 
50 #define DHT22_NUM_DATA_BITS (5 * 8)
51 
53 #ifdef DEBUG_PRINT
54  #define DEBUG_PRINTLN(...) { Serial.println(__VA_ARGS__); }
55 #else
56  #define DEBUG_PRINTLN(...) {}
57 #endif
58 
80 class DHT22
81 {
82 public:
83  explicit DHT22(uint8_t pin);
84  void begin(uint8_t numSamples=0);
85  bool available();
86  bool readSensorData();
87  int16_t readTemperature();
88  int16_t readHumidity();
89 
90 private:
92  unsigned long _lastMeasurementTimestamp;
94  uint32_t _maxCycles;
98  uint32_t cycles[DHT22_NUM_DATA_BITS * 2];
101  uint8_t _data[5];
103  bool _statusLastMeasurement;
104 
106  uint8_t _numSamples;
107 
109  int16_t *_temperatureSamples;
111  uint8_t _temperatureSampleIndex;
113  uint8_t _numTemperatureSamples;
114 
116  int16_t *_humiditySamples;
118  uint8_t _humiditySampleIndex;
120  uint8_t _numHumiditySamples;
121 
123  uint8_t _pin;
124 
125 #ifdef __AVR
126  uint8_t _bit;
129  uint8_t _port;
130 #endif
131 
132  bool generateStart();
133  bool readBytes();
134  uint32_t measurePulseWidth(uint8_t level);
135 };
136 
137 #endif // ERRIEZ_DHT22_H_
int16_t readHumidity()
Read humidity from sensor.
DHT22 sensor class.
Definition: ErriezDHT22.h:80
void begin(uint8_t numSamples=0)
Initialize sensor.
Definition: ErriezDHT22.cpp:75
#define DHT22_NUM_DATA_BITS
Definition: ErriezDHT22.h:50
int16_t readTemperature()
Read temperature from sensor.
bool available()
Check if new temperature or humidity read is allowed.
DHT22(uint8_t pin)
Constructor DHT22 sensor.
Definition: ErriezDHT22.cpp:39
bool readSensorData()
Read data from sensor.