-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDHT22.h
89 lines (73 loc) · 2.08 KB
/
DHT22.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*!
* @file DHT22.h
* @brief Arduino library for interface with DHT22 sensor
* @n without use of pointers.
* @author dvarrel
* @version 1.0.7
* @date 2024-10-05
* @url https://github.com/dvarrel/DHT22.git
*/
#ifndef _DHT22_H
#define _DHT22_H
#include "Arduino.h"
class DHT22{
public:
enum error{
OK,
ERR_TIMING_80,
ERR_TIMING_50,
ERR_TIMING_BITS,
ERR_CRC
};
private:
uint8_t _pinData;
uint32_t _timer;
uint64_t _rawData;
uint16_t _h16bits;
uint16_t _t16bits;
uint8_t _crc8bits;
uint8_t _timing80L = 80;
uint8_t _timing80H = 80;
uint8_t _timing50 = 50;
uint8_t _timingBit0 = 27; //specs for AM2303
uint8_t _timingBit1 = 70; //specs for AM2303
bool _firstStart=true;
error _lastError;
const uint8_t T = 30;
const uint16_t cSamplingTime = 2100; // ms : dht22 sampling rate ~0.5Hz
public:
DHT22(uint8_t pinData){
_pinData = pinData;
};
/** @return 40bits of data sensor : h16 + t16 + crc8**/
uint64_t getRawData();
/** @return 40bits in a String ("0" left include)**/
String getRawStrData();
/** @return humidity %rH **/
float getHumidity();
/** if M=true, return temperature in °C, else in °F **/
/** @return temperature in °C or °F **/
float getTemperature(bool M = true);
/** @return temp in °C **/
float convertCtoF(float);
/** @return temp in °F **/
float convertFtoC(float);
/** @return heat index in °C or °F **/
float getHeatIndex(float t, float h, bool isCelsius = true);
/** @return code from last readSensor() request **/
uint8_t getLastError();
/** @return String with timings, 40bits, and calculate values**/
String debug();
private:
/** @brief measure timings of sensor*/
void measureTimings();
/**
*@brief read the 40bits sensor data
*@return 0 if no error, num of error instead
*/
uint8_t readSensor();
/** @brief compute checksum of h & t data
* @return true if checksum==crc**/
bool computeCRC();
};
#endif