Skip to content

Commit 25942ac

Browse files
committed
Added support for Celsius in computeHeatIndex
1 parent fcd865b commit 25942ac

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

DHT.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void DHT::begin(void) {
2020
_lastreadtime = 0;
2121
}
2222

23-
//boolean S == Scale. True == Farenheit; False == Celcius
23+
//boolean S == Scale. True == Fahrenheit; False == Celcius
2424
float DHT::readTemperature(bool S) {
2525
float f;
2626

@@ -76,18 +76,30 @@ float DHT::readHumidity(void) {
7676
return NAN;
7777
}
7878

79-
float DHT::computeHeatIndex(float tempFahrenheit, float percentHumidity) {
79+
//boolean isFahrenheit: True == Fahrenheit; False == Celcius
80+
float DHT::computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit) {
8081
// Adapted from equation at: https://github.com/adafruit/DHT-sensor-library/issues/9 and
8182
// Wikipedia: http://en.wikipedia.org/wiki/Heat_index
83+
if (!isFahrenheit) {
84+
return -8.784695 +
85+
1.61139411 * temperature +
86+
2.338549 * percentHumidity +
87+
-0.14611605 * temperature*percentHumidity +
88+
-0.01230809 * pow(temperature, 2) +
89+
-0.01642482 * pow(percentHumidity, 2) +
90+
0.00221173 * pow(temperature, 2) * percentHumidity +
91+
0.00072546 * temperature*pow(percentHumidity, 2) +
92+
-0.00000358 * pow(temperature, 2) * pow(percentHumidity, 2);
93+
}
8294
return -42.379 +
83-
2.04901523 * tempFahrenheit +
95+
2.04901523 * temperature +
8496
10.14333127 * percentHumidity +
85-
-0.22475541 * tempFahrenheit*percentHumidity +
86-
-0.00683783 * pow(tempFahrenheit, 2) +
97+
-0.22475541 * temperature*percentHumidity +
98+
-0.00683783 * pow(temperature, 2) +
8799
-0.05481717 * pow(percentHumidity, 2) +
88-
0.00122874 * pow(tempFahrenheit, 2) * percentHumidity +
89-
0.00085282 * tempFahrenheit*pow(percentHumidity, 2) +
90-
-0.00000199 * pow(tempFahrenheit, 2) * pow(percentHumidity, 2);
100+
0.00122874 * pow(temperature, 2) * percentHumidity +
101+
0.00085282 * temperature*pow(percentHumidity, 2) +
102+
-0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2);
91103
}
92104

93105

DHT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DHT {
3333
float readTemperature(bool S=false);
3434
float convertCtoF(float);
3535
float convertFtoC(float);
36-
float computeHeatIndex(float tempFahrenheit, float percentHumidity);
36+
float computeHeatIndex(float tempFahrenheit, float percentHumidity, bool isFahrenheit=false);
3737
float readHumidity(void);
3838
boolean read(void);
3939

examples/DHTtester/DHTtester.ino

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ void loop() {
4646
float t = dht.readTemperature();
4747
// Read temperature as Fahrenheit
4848
float f = dht.readTemperature(true);
49-
49+
5050
// Check if any reads failed and exit early (to try again).
5151
if (isnan(h) || isnan(t) || isnan(f)) {
5252
Serial.println("Failed to read from DHT sensor!");
5353
return;
5454
}
5555

56-
// Compute heat index
57-
// Must send in temp in Fahrenheit!
58-
float hi = dht.computeHeatIndex(f, h);
56+
// Compute heat index in Celsius
57+
float hic = dht.computeHeatIndex(t, h);
58+
// Compute heat index in Fahrenheit
59+
float hif = dht.computeHeatIndex(f, h, true);
5960

6061
Serial.print("Humidity: ");
6162
Serial.print(h);
@@ -66,6 +67,8 @@ void loop() {
6667
Serial.print(f);
6768
Serial.print(" *F\t");
6869
Serial.print("Heat index: ");
69-
Serial.print(hi);
70+
Serial.print(hic);
71+
Serial.print(" *C ");
72+
Serial.print(hif);
7073
Serial.println(" *F");
7174
}

0 commit comments

Comments
 (0)