Skip to content
This repository has been archived by the owner on Apr 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from ringerc/master
Browse files Browse the repository at this point in the history
Arduino 1.0 update, access to integer values, inlines
  • Loading branch information
nethoncho committed Mar 25, 2013
2 parents a655c62 + 7fa6ba8 commit 3d20fcd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 26 deletions.
30 changes: 12 additions & 18 deletions DHT22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
Humidity and Temperature Sensor DHT22 info found at
http://www.sparkfun.com/products/10167
Version 0.5: 15 Jan 2012 by Craig Ringer
- Updated to build against Arduino 1.0
- Made accessors inline in the header so they can be optimized away
Version 0.4: 24-Jan-2011 by Ben Adams
Added return code constants to keywords.txt
Returns DHT_ERROR_CHECKSUM on check sum mismatch
Expand All @@ -41,10 +45,10 @@ The Arduino OneWire lib
*/

#include "DHT22.h"
#include "pins_arduino.h"
#include <Arduino.h>
#include <pins_arduino.h>

extern "C" {
#include "WConstants.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
Expand All @@ -61,7 +65,7 @@ extern "C" {

DHT22::DHT22(uint8_t pin)
{
_bitmask = digitalPinToBitMask(pin);
_bitmask = digitalPinToBitMask(pin);
_baseReg = portInputRegister(digitalPinToPort(pin));
_lastReadTime = millis();
_lastHumidity = DHT22_ERROR_VALUE;
Expand Down Expand Up @@ -201,16 +205,16 @@ DHT22_ERROR_t DHT22::readData()
}
}

_lastHumidity = (float(currentHumidity & 0x7FFF) / 10.0);
_lastHumidity = currentHumidity & 0x7FFF;
if(currentTemperature & 0x8000)
{
// Below zero, non standard way of encoding negative numbers!
currentTemperature &= 0x7FFF;
_lastTemperature = (float(currentTemperature) / 10.0) * -1.0;
// Below zero, non standard way of encoding negative numbers!
// Convert to native negative format.
_lastTemperature = -currentTemperature & 0x7FFF;
}
else
{
_lastTemperature = float(currentTemperature) / 10.0;
_lastTemperature = currentTemperature;
}

csPart1 = currentHumidity >> 8;
Expand All @@ -224,16 +228,6 @@ DHT22_ERROR_t DHT22::readData()
return DHT_ERROR_CHECKSUM;
}

float DHT22::getHumidity()
{
return _lastHumidity;
}

float DHT22::getTemperatureC()
{
return _lastTemperature;
}

//
// This is used when the millis clock rolls over to zero
//
Expand Down
49 changes: 44 additions & 5 deletions DHT22.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <inttypes.h>

#define DHT22_ERROR_VALUE -99.5
#define DHT22_ERROR_VALUE -995

typedef enum
{
Expand All @@ -23,15 +23,54 @@ class DHT22
uint8_t _bitmask;
volatile uint8_t *_baseReg;
unsigned long _lastReadTime;
float _lastHumidity;
float _lastTemperature;
short int _lastHumidity;
short int _lastTemperature;

public:
DHT22(uint8_t pin);
DHT22_ERROR_t readData(void);
DHT22_ERROR_t readData();
short int getHumidityInt();
short int getTemperatureCInt();
void clockReset();
#if !defined(DHT22_NO_FLOAT)
float getHumidity();
float getTemperatureC();
void clockReset();
#endif
};

// Report the humidity in .1 percent increments, such that 635 means 63.5% relative humidity
//
// Converts from the internal integer format on demand, so you might want
// to cache the result.
inline short int DHT22::getHumidityInt()
{
return _lastHumidity;
}

// Get the temperature in decidegrees C, such that 326 means 32.6 degrees C.
// The temperature may be negative, so be careful when handling the fractional part.
inline short int DHT22::getTemperatureCInt()
{
return _lastTemperature;
}

#if !defined(DHT22_NO_FLOAT)
// Return the percentage relative humidity in decimal form
inline float DHT22::getHumidity()
{
return float(_lastHumidity)/10;
}
#endif

#if !defined(DHT22_NO_FLOAT)
// Return the percentage relative humidity in decimal form
//
// Converts from the internal integer format on demand, so you might want
// to cache the result.
inline float DHT22::getTemperatureC()
{
return float(_lastTemperature)/10;
}
#endif //DHT22_SUPPORT_FLOAT

#endif /*_DHT22_H_*/
15 changes: 14 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@
Developed by Ben Adams - 2011

Humidity and Temperature Sensor DHT22 info found at
http://www.sparkfun.com/products/10167
http://www.sparkfun.com/products/10167
The same sensor has also been spotted going by the names RHT22, AM2302
and RHT03 from a variety of vendors. It uses a non-standard 1-wire digital
signalling protocol.

To install this library for use with the Arduino IDE, copy it
to the `libraries' folder and restart the IDE. For an example of
how to use it, see File->Examples->DHT22->Serial .

Version 0.5: 15-Jan-2012 by Craig Ringer
Update to support Arduino 1.0
Make accessors inlineable so they can be optimised away
Preserve original integer values from sensor and offer access to them
Permit floating point support to be disalbed by defining DHT22_NO_FLOAT before including DHT22.h

Version 0.4: 24-Jan-2011 by Ben Adams
Added return code constants to keywords.txt
Expand Down
17 changes: 15 additions & 2 deletions examples/Serial/Serial.pde → examples/Serial/Serial.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <DHT22.h>
// Only used for sprintf
#include <stdio.h>

// Data wire is plugged into port 7 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
Expand All @@ -17,8 +19,11 @@ void setup(void)
void loop(void)
{
DHT22_ERROR_t errorCode;


// The sensor can only be read from every 1-2s, and requires a minimum
// 2s warm-up after power-on.
delay(2000);

Serial.print("Requesting data...");
errorCode = myDHT22.readData();
switch(errorCode)
Expand All @@ -29,6 +34,14 @@ void loop(void)
Serial.print("C ");
Serial.print(myDHT22.getHumidity());
Serial.println("%");
// Alternately, with integer formatting which is clumsier but more compact to store and
// can be compared reliably for equality:
//
char buf[128];
sprintf(buf, "Integer-only reading: Temperature %hi.%01hi C, Humidity %i.%01i %% RH",
myDHT22.getTemperatureCInt()/10, abs(myDHT22.getTemperatureCInt()%10),
myDHT22.getHumidityInt()/10, myDHT22.getHumidityInt()%10);
Serial.println(buf);
break;
case DHT_ERROR_CHECKSUM:
Serial.print("check sum error ");
Expand Down Expand Up @@ -56,4 +69,4 @@ void loop(void)
Serial.println("Polled to quick ");
break;
}
}
}
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ DHT22 KEYWORD1

readData KEYWORD2
getHumidity KEYWORD2
getHumidityInt KEYWORD2
getTemperatureC KEYWORD2
getTemperatureCInt KEYWORD2
clockReset KEYWORD2

#######################################
Expand Down

0 comments on commit 3d20fcd

Please sign in to comment.