From ffac2f15b7dca61155b0d84d26e0b95d9cfe6c0c Mon Sep 17 00:00:00 2001 From: Tony DiCola Date: Mon, 2 Jun 2014 12:54:54 -0700 Subject: [PATCH] Update example to show temp in Fahrenheit and add computeHeatIndex function to resolve issue #9. --- DHT.cpp | 14 ++++++++++++++ DHT.h | 1 + examples/DHTtester/DHTtester.ino | 14 +++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/DHT.cpp b/DHT.cpp index efa1d39..84d822d 100644 --- a/DHT.cpp +++ b/DHT.cpp @@ -74,6 +74,20 @@ float DHT::readHumidity(void) { return NAN; } +float DHT::computeHeatIndex(float tempFahrenheit, float percentHumidity) { + // Adapted from equation at: https://github.com/adafruit/DHT-sensor-library/issues/9 and + // Wikipedia: http://en.wikipedia.org/wiki/Heat_index + return -42.379 + + 2.04901523 * tempFahrenheit + + 10.14333127 * percentHumidity + + -0.22475541 * tempFahrenheit*percentHumidity + + -0.00683783 * pow(tempFahrenheit, 2) + + -0.05481717 * pow(percentHumidity, 2) + + 0.00122874 * pow(tempFahrenheit, 2) * percentHumidity + + 0.00085282 * tempFahrenheit*pow(percentHumidity, 2) + + -0.00000199 * pow(tempFahrenheit, 2) * pow(percentHumidity, 2); +} + boolean DHT::read(void) { uint8_t laststate = HIGH; diff --git a/DHT.h b/DHT.h index 1c78c87..3455c06 100644 --- a/DHT.h +++ b/DHT.h @@ -33,6 +33,7 @@ class DHT { void begin(void); float readTemperature(bool S=false); float convertCtoF(float); + float computeHeatIndex(float tempFahrenheit, float percentHumidity); float readHumidity(void); }; diff --git a/examples/DHTtester/DHTtester.ino b/examples/DHTtester/DHTtester.ino index 8f40790..56874ab 100644 --- a/examples/DHTtester/DHTtester.ino +++ b/examples/DHTtester/DHTtester.ino @@ -28,7 +28,13 @@ void loop() { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); + // Read temperature as Celsius float t = dht.readTemperature(); + // Read temperature as Fahrenheit + float f = dht.readTemperature(true); + // Compute heat index + // Must send in temp in Fahrenheit! + float hi = dht.computeHeatIndex(f, h); // check if returns are valid, if they are NaN (not a number) then something went wrong! if (isnan(t) || isnan(h)) { @@ -39,6 +45,12 @@ void loop() { Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); - Serial.println(" *C"); + Serial.print(" *C "); + Serial.print(f); + Serial.print(" *F\t"); + Serial.print("Heat index: "); + Serial.print(hi); + Serial.println(" *F"); + } }