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

Commit f1c5b3a

Browse files
author
Craig Ringer
committed
Port to Arduino 1.0 and more as per README changes
1 parent a655c62 commit f1c5b3a

File tree

5 files changed

+87
-25
lines changed

5 files changed

+87
-25
lines changed

DHT22.cpp

+12-17
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.5: 15 Jan 2012 by Craig Ringer
24+
- Updated to build against Arduino 1.0
25+
- Made accessors inline in the header so they can be optimized away
26+
2327
Version 0.4: 24-Jan-2011 by Ben Adams
2428
Added return code constants to keywords.txt
2529
Returns DHT_ERROR_CHECKSUM on check sum mismatch
@@ -41,10 +45,10 @@ The Arduino OneWire lib
4145
*/
4246

4347
#include "DHT22.h"
44-
#include "pins_arduino.h"
48+
#include <Arduino.h>
49+
#include <pins_arduino.h>
4550

4651
extern "C" {
47-
#include "WConstants.h"
4852
#include <avr/io.h>
4953
#include <avr/interrupt.h>
5054
#include <avr/pgmspace.h>
@@ -61,7 +65,7 @@ extern "C" {
6165

6266
DHT22::DHT22(uint8_t pin)
6367
{
64-
_bitmask = digitalPinToBitMask(pin);
68+
_bitmask = digitalPinToBitMask(pin);
6569
_baseReg = portInputRegister(digitalPinToPort(pin));
6670
_lastReadTime = millis();
6771
_lastHumidity = DHT22_ERROR_VALUE;
@@ -201,16 +205,17 @@ DHT22_ERROR_t DHT22::readData()
201205
}
202206
}
203207

204-
_lastHumidity = (float(currentHumidity & 0x7FFF) / 10.0);
208+
_lastHumidity = currentHumidity & 0x7FFF;
205209
if(currentTemperature & 0x8000)
206210
{
207-
// Below zero, non standard way of encoding negative numbers!
211+
// Below zero, non standard way of encoding negative numbers!
212+
// Convert to native negative format.
208213
currentTemperature &= 0x7FFF;
209-
_lastTemperature = (float(currentTemperature) / 10.0) * -1.0;
214+
_lastTemperature = -currentTemperature;
210215
}
211216
else
212217
{
213-
_lastTemperature = float(currentTemperature) / 10.0;
218+
_lastTemperature = currentTemperature;
214219
}
215220

216221
csPart1 = currentHumidity >> 8;
@@ -224,16 +229,6 @@ DHT22_ERROR_t DHT22::readData()
224229
return DHT_ERROR_CHECKSUM;
225230
}
226231

227-
float DHT22::getHumidity()
228-
{
229-
return _lastHumidity;
230-
}
231-
232-
float DHT22::getTemperatureC()
233-
{
234-
return _lastTemperature;
235-
}
236-
237232
//
238233
// This is used when the millis clock rolls over to zero
239234
//

DHT22.h

+44-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <inttypes.h>
55

6-
#define DHT22_ERROR_VALUE -99.5
6+
#define DHT22_ERROR_VALUE -995
77

88
typedef enum
99
{
@@ -23,15 +23,54 @@ class DHT22
2323
uint8_t _bitmask;
2424
volatile uint8_t *_baseReg;
2525
unsigned long _lastReadTime;
26-
float _lastHumidity;
27-
float _lastTemperature;
26+
short int _lastHumidity;
27+
short int _lastTemperature;
2828

2929
public:
3030
DHT22(uint8_t pin);
31-
DHT22_ERROR_t readData(void);
31+
DHT22_ERROR_t readData();
32+
short int getHumidityInt();
33+
short int getTemperatureCInt();
34+
void clockReset();
35+
#if !defined(DHT22_NO_FLOAT)
3236
float getHumidity();
3337
float getTemperatureC();
34-
void clockReset();
38+
#endif
3539
};
3640

41+
// Report the humidity in .1 percent increments, such that 635 means 63.5% relative humidity
42+
//
43+
// Converts from the internal integer format on demand, so you might want
44+
// to cache the result.
45+
inline short int DHT22::getHumidityInt()
46+
{
47+
return _lastHumidity;
48+
}
49+
50+
// Get the temperature in decidegrees C, such that 326 means 32.6 degrees C.
51+
// The temperature may be negative, so be careful when handling the fractional part.
52+
inline short int DHT22::getTemperatureCInt()
53+
{
54+
return _lastTemperature;
55+
}
56+
57+
#if !defined(DHT22_NO_FLOAT)
58+
// Return the percentage relative humidity in decimal form
59+
inline float DHT22::getHumidity()
60+
{
61+
return float(_lastHumidity)/10;
62+
}
63+
#endif
64+
65+
#if !defined(DHT22_NO_FLOAT)
66+
// Return the percentage relative humidity in decimal form
67+
//
68+
// Converts from the internal integer format on demand, so you might want
69+
// to cache the result.
70+
inline float DHT22::getTemperatureC()
71+
{
72+
return float(_lastTemperature)/10;
73+
}
74+
#endif //DHT22_SUPPORT_FLOAT
75+
3776
#endif /*_DHT22_H_*/

README

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@
22
Developed by Ben Adams - 2011
33

44
Humidity and Temperature Sensor DHT22 info found at
5-
http://www.sparkfun.com/products/10167
5+
http://www.sparkfun.com/products/10167
6+
The same sensor has also been spotted going by the names RHT22, AM2302
7+
and RHT03 from a variety of vendors. It uses a non-standard 1-wire digital
8+
signalling protocol.
9+
10+
To install this library for use with the Arduino IDE, copy it
11+
to the `libraries' folder and restart the IDE. For an example of
12+
how to use it, see File->Examples->DHT22->Serial .
13+
14+
Version 0.5: 15-Jan-2012 by Craig Ringer
15+
Update to support Arduino 1.0
16+
Make accessors inlineable so they can be optimised away
17+
Preserve original integer values from sensor and offer access to them
18+
Permit floating point support to be disalbed by defining DHT22_NO_FLOAT before including DHT22.h
619

720
Version 0.4: 24-Jan-2011 by Ben Adams
821
Added return code constants to keywords.txt

examples/Serial/Serial.pde renamed to examples/Serial/Serial.ino

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <DHT22.h>
2+
// Only used for sprintf
3+
#include <stdio.h>
24

35
// Data wire is plugged into port 7 on the Arduino
46
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
@@ -17,8 +19,11 @@ void setup(void)
1719
void loop(void)
1820
{
1921
DHT22_ERROR_t errorCode;
20-
22+
23+
// The sensor can only be read from every 1-2s, and requires a minimum
24+
// 2s warm-up after power-on.
2125
delay(2000);
26+
2227
Serial.print("Requesting data...");
2328
errorCode = myDHT22.readData();
2429
switch(errorCode)
@@ -29,6 +34,14 @@ void loop(void)
2934
Serial.print("C ");
3035
Serial.print(myDHT22.getHumidity());
3136
Serial.println("%");
37+
// Alternately, with integer formatting which is clumsier but more compact to store and
38+
// can be compared reliably for equality:
39+
//
40+
char buf[128];
41+
sprintf(buf, "Integer-only reading: Temperature %hi.%01hi C, Humidity %i.%01i %% RH",
42+
myDHT22.getTemperatureCInt()/10, abs(myDHT22.getTemperatureCInt()%10),
43+
myDHT22.getHumidityInt()/10, myDHT22.getHumidityInt()%10);
44+
Serial.println(buf);
3245
break;
3346
case DHT_ERROR_CHECKSUM:
3447
Serial.print("check sum error ");
@@ -56,4 +69,4 @@ void loop(void)
5669
Serial.println("Polled to quick ");
5770
break;
5871
}
59-
}
72+
}

keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ DHT22 KEYWORD1
1414

1515
readData KEYWORD2
1616
getHumidity KEYWORD2
17+
getHumidityInt KEYWORD2
1718
getTemperatureC KEYWORD2
19+
getTemperatureCInt KEYWORD2
1820
clockReset KEYWORD2
1921

2022
#######################################

0 commit comments

Comments
 (0)