@@ -89,16 +89,22 @@ void DHT::read() {
89
89
}
90
90
91
91
const char * DHT::getError () const {
92
- if (_status == 1 ) {
92
+ if (_status == 0 ) {
93
+ return " OK" ;
94
+ } else if (_status == 1 ) {
93
95
return " TO" ;
94
96
} else if (_status == 2 ) {
95
97
return " NACK" ;
96
98
} else if (_status == 3 ) {
97
99
return " DATA" ;
98
100
} else if (_status == 4 ) {
99
101
return " CS" ;
102
+ } else if (_status == 5 ) {
103
+ return " UNDERFLOW" ;
104
+ } else if (_status == 6 ) {
105
+ return " OVERFLOW" ;
100
106
}
101
- return " OK " ;
107
+ return " UNKNOWN " ;
102
108
}
103
109
104
110
void DHT::_handleTimer (DHT* instance) {
@@ -131,8 +137,10 @@ void DHT::_handleData(DHT* instance) {
131
137
}
132
138
133
139
void DHT::_decode (rmt_item32_t * data, int numItems) {
134
- if (numItems != 42 ) {
140
+ if (numItems < 42 ) {
135
141
_status = 5 ;
142
+ } else if (numItems > 42 ) {
143
+ _status = 6 ;
136
144
} else if ((data[0 ].duration0 + data[0 ].duration1 ) < 140 && (data[0 ].duration0 + data[0 ].duration1 ) > 180 ) {
137
145
_status = 2 ;
138
146
} else {
@@ -149,7 +157,7 @@ void DHT::_decode(rmt_item32_t* data, int numItems) {
149
157
}
150
158
}
151
159
if (_data[4 ] == ((_data[0 ] + _data[1 ] + _data[2 ] + _data[3 ]) & 0xFF )) {
152
- _status = 1 ;
160
+ _status = 0 ;
153
161
} else {
154
162
_status = 4 ; // checksum error
155
163
}
@@ -165,17 +173,17 @@ void DHT::_tryCallback() {
165
173
}
166
174
167
175
float DHT11::_getTemperature () {
168
- if (_status < 1 ) return NAN;
176
+ if (_status != 0 ) return NAN;
169
177
return static_cast <float >(_data[2 ]);
170
178
}
171
179
172
180
float DHT11::_getHumidity () {
173
- if (_status < 1 ) return NAN;
181
+ if (_status != 0 ) return NAN;
174
182
return static_cast <float >(_data[0 ]);
175
183
}
176
184
177
185
float DHT22::_getTemperature () {
178
- if (_status < 1 ) return NAN;
186
+ if (_status != 0 ) return NAN;
179
187
float temp = (((_data[2 ] & 0x7F ) << 8 ) | _data[3 ]) * 0.1 ;
180
188
if (_data[2 ] & 0x80 ) { // negative temperature
181
189
temp = -temp;
@@ -184,6 +192,6 @@ float DHT22::_getTemperature() {
184
192
}
185
193
186
194
float DHT22::_getHumidity () {
187
- if (_status < 1 ) return NAN;
195
+ if (_status != 0 ) return NAN;
188
196
return ((_data[0 ] << 8 ) | _data[1 ]) * 0.1 ;
189
197
}
0 commit comments