Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit a655c62

Browse files
committed
Version 0.4
1 parent 027d2de commit a655c62

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

DHT22.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
Humidity and Temperature Sensor DHT22 info found at
2121
http://www.sparkfun.com/products/10167
2222
23+
Version 0.4: 24-Jan-2011 by Ben Adams
24+
Added return code constants to keywords.txt
25+
Returns DHT_ERROR_CHECKSUM on check sum mismatch
26+
2327
Version 0.3: 17-Jan-2011 by Ben Adams
2428
This version reads data
2529
Needs check sum code added at the end of readData
@@ -76,7 +80,7 @@ DHT22_ERROR_t DHT22::readData()
7680
uint8_t bitTimes[DHT22_DATA_BIT_COUNT];
7781
int currentHumidity;
7882
int currentTemperature;
79-
uint8_t checkSum;
83+
uint8_t checkSum, csPart1, csPart2, csPart3, csPart4;
8084
unsigned long currentTime;
8185
int i;
8286

@@ -209,9 +213,15 @@ DHT22_ERROR_t DHT22::readData()
209213
_lastTemperature = float(currentTemperature) / 10.0;
210214
}
211215

212-
// TODO: Test the checksum and return DHT_ERROR_CHECKSUM if bad
213-
214-
return DHT_ERROR_NONE;
216+
csPart1 = currentHumidity >> 8;
217+
csPart2 = currentHumidity & 0xFF;
218+
csPart3 = currentTemperature >> 8;
219+
csPart4 = currentTemperature & 0xFF;
220+
if(checkSum == ((csPart1 + csPart2 + csPart3 + csPart4) & 0xFF))
221+
{
222+
return DHT_ERROR_NONE;
223+
}
224+
return DHT_ERROR_CHECKSUM;
215225
}
216226

217227
float DHT22::getHumidity()

README

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
Humidity and Temperature Sensor DHT22 info found at
55
http://www.sparkfun.com/products/10167
66

7+
Version 0.4: 24-Jan-2011 by Ben Adams
8+
Added return code constants to keywords.txt
9+
Returns DHT_ERROR_CHECKSUM on check sum mismatch
10+
711
Version 0.3: 17-Jan-2011 by Ben Adams
812
This version reads data
913
Needs check sum code added at the end of readData

examples/Serial/Serial.pde

+33-12
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,39 @@ void loop(void)
2121
delay(2000);
2222
Serial.print("Requesting data...");
2323
errorCode = myDHT22.readData();
24-
if(errorCode == DHT_ERROR_NONE)
24+
switch(errorCode)
2525
{
26-
Serial.print("Got Data ");
27-
Serial.print(myDHT22.getTemperatureC());
28-
Serial.print("C ");
29-
Serial.print(myDHT22.getHumidity());
30-
Serial.println("%");
31-
}
32-
else
33-
{
34-
Serial.print("Error Code ");
35-
Serial.print(errorCode);
36-
Serial.println(" readData Failed");
26+
case DHT_ERROR_NONE:
27+
Serial.print("Got Data ");
28+
Serial.print(myDHT22.getTemperatureC());
29+
Serial.print("C ");
30+
Serial.print(myDHT22.getHumidity());
31+
Serial.println("%");
32+
break;
33+
case DHT_ERROR_CHECKSUM:
34+
Serial.print("check sum error ");
35+
Serial.print(myDHT22.getTemperatureC());
36+
Serial.print("C ");
37+
Serial.print(myDHT22.getHumidity());
38+
Serial.println("%");
39+
break;
40+
case DHT_BUS_HUNG:
41+
Serial.println("BUS Hung ");
42+
break;
43+
case DHT_ERROR_NOT_PRESENT:
44+
Serial.println("Not Present ");
45+
break;
46+
case DHT_ERROR_ACK_TOO_LONG:
47+
Serial.println("ACK time out ");
48+
break;
49+
case DHT_ERROR_SYNC_TIMEOUT:
50+
Serial.println("Sync Timeout ");
51+
break;
52+
case DHT_ERROR_DATA_TIMEOUT:
53+
Serial.println("Data Timeout ");
54+
break;
55+
case DHT_ERROR_TOOQUICK:
56+
Serial.println("Polled to quick ");
57+
break;
3758
}
3859
}

keywords.txt

+9
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@ clockReset KEYWORD2
2525
#######################################
2626
# Constants (LITERAL1)
2727
#######################################
28+
29+
DHT_ERROR_NONE LITERAL1
30+
DHT_BUS_HUNG LITERAL1
31+
DHT_ERROR_NOT_PRESENT LITERAL1
32+
DHT_ERROR_ACK_TOO_LONG LITERAL1
33+
DHT_ERROR_SYNC_TIMEOUT LITERAL1
34+
DHT_ERROR_DATA_TIMEOUT LITERAL1
35+
DHT_ERROR_CHECKSUM LITERAL1
36+
DHT_ERROR_TOOQUICK LITERAL1

0 commit comments

Comments
 (0)