Skip to content

Commit 4f57bf3

Browse files
jcolliebertmelis
authored andcommitted
Fix error checking
1 parent cfae578 commit 4f57bf3

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/esp32DHT.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,22 @@ void DHT::read() {
8989
}
9090

9191
const char* DHT::getError() const {
92-
if (_status == 1) {
92+
if (_status == 0) {
93+
return "OK";
94+
} else if (_status == 1) {
9395
return "TO";
9496
} else if (_status == 2) {
9597
return "NACK";
9698
} else if (_status == 3) {
9799
return "DATA";
98100
} else if (_status == 4) {
99101
return "CS";
102+
} else if (_status == 5) {
103+
return "UNDERFLOW";
104+
} else if (_status == 6) {
105+
return "OVERFLOW";
100106
}
101-
return "OK";
107+
return "UNKNOWN";
102108
}
103109

104110
void DHT::_handleTimer(DHT* instance) {
@@ -131,8 +137,10 @@ void DHT::_handleData(DHT* instance) {
131137
}
132138

133139
void DHT::_decode(rmt_item32_t* data, int numItems) {
134-
if (numItems != 42) {
140+
if (numItems < 42) {
135141
_status = 5;
142+
} else if (numItems > 42) {
143+
_status = 6;
136144
} else if ((data[0].duration0 + data[0].duration1) < 140 && (data[0].duration0 + data[0].duration1) > 180) {
137145
_status = 2;
138146
} else {
@@ -149,7 +157,7 @@ void DHT::_decode(rmt_item32_t* data, int numItems) {
149157
}
150158
}
151159
if (_data[4] == ((_data[0] + _data[1] + _data[2] + _data[3]) & 0xFF)) {
152-
_status = 1;
160+
_status = 0;
153161
} else {
154162
_status = 4; // checksum error
155163
}
@@ -165,17 +173,17 @@ void DHT::_tryCallback() {
165173
}
166174

167175
float DHT11::_getTemperature() {
168-
if (_status < 1) return NAN;
176+
if (_status != 0) return NAN;
169177
return static_cast<float>(_data[2]);
170178
}
171179

172180
float DHT11::_getHumidity() {
173-
if (_status < 1) return NAN;
181+
if (_status != 0) return NAN;
174182
return static_cast<float>(_data[0]);
175183
}
176184

177185
float DHT22::_getTemperature() {
178-
if (_status < 1) return NAN;
186+
if (_status != 0) return NAN;
179187
float temp = (((_data[2] & 0x7F) << 8) | _data[3]) * 0.1;
180188
if (_data[2] & 0x80) { // negative temperature
181189
temp = -temp;
@@ -184,6 +192,6 @@ float DHT22::_getTemperature() {
184192
}
185193

186194
float DHT22::_getHumidity() {
187-
if (_status < 1) return NAN;
195+
if (_status != 0) return NAN;
188196
return ((_data[0] << 8) | _data[1]) * 0.1;
189197
}

0 commit comments

Comments
 (0)