Skip to content

Commit

Permalink
fix #10 HeatIndex (#11)
Browse files Browse the repository at this point in the history
* fix #10 HeatIndex + update readme.md + fix unit test
  • Loading branch information
RobTillaart committed Jan 6, 2022
1 parent e50eb99 commit 9354eab
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 33 deletions.
10 changes: 5 additions & 5 deletions .arduino-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- due
- zero
- leonardo
# - due
# - zero
# - leonardo
- m4
- esp32
- esp8266
- mega2560
# - esp8266
# - mega2560
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ DHT22 or Sensirion, to make a weather station application.
- **float heatIndex(float Fahrenheit, float humidity)** idem.
- **float heatIndexC(float Celsius, float humidity)** idem.

**0.3.0**
The formula for the **heatIndex()** was taken from https://en.wikipedia.org/wiki/Heat_index.
Since version 0.3.0 the more elaborated version of https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
will be used. Note: there will be performance differences.

Indicative table

| Fahrenheit | Celsius | description | colour code |
|:----------:|:-------:|:------------|------------:|
| 70-79 | 21-26 | warm | green |
| 80-89 | 26-32 | very warm | yellow |
| 90-104 | 32-40 | hot | orange |
| 105-129 | 40-54 | very hot | red |
| > 130 | > 54 | extreme hot | purple |


### WindChill

Expand Down
5 changes: 3 additions & 2 deletions examples/heatindex_table/heatindex_table.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ void setup()
Serial.println(__FILE__);

Serial.println();

Serial.println(" Compare to: https://www.calculator.net/heat-index-calculator.html\n");
Serial.println();

for (int t = 80; t <= 110; t += 2)
{
Serial.print("\t");
Expand Down Expand Up @@ -50,4 +52,3 @@ void loop()


// -- END OF FILE --

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Temperature"
},
"version": "0.2.5",
"version": "0.3.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Temperature
version=0.2.5
version=0.3.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library with weather related functions.
Expand Down
70 changes: 49 additions & 21 deletions temperature.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
//
// FILE: temperature.h
// VERSION: 0.2.5
// VERSION: 0.3.0
// DATE: 2015-03-29
// PURPOSE: collection temperature functions
//
Expand All @@ -16,9 +16,11 @@
// 0.2.3 2020-08-27 fix #5 order of functions, typo, fixed 1 example
// 0.2.4 2021-01-08 Arduino-CI + unit tests
// 0.2.5 2021-12-28 Arduino-CI, library.json, readme.md, license, minor edits
// 0.3.0 2022-01-05 fix #10 update HeatIndex function
// compared with https://www.calculator.net/heat-index-calculator.html


#define TEMPERATURE_VERSION (F("0.2.5"))
#define TEMPERATURE_VERSION (F("0.3.0"))


inline float Fahrenheit(float celsius)
Expand Down Expand Up @@ -87,34 +89,59 @@ float humidex(float celsius, float dewPoint)
}


// https://en.wikipedia.org/wiki/Heat_index
// 0.3.0 => https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
// previous https://en.wikipedia.org/wiki/Heat_index
// TODO add valid range for TF & R
// TF = temp in Fahrenheit
// R = humidity in %
float heatIndex(float TF, float R)
// RH = relative humidity in %
float heatIndex(float TF, float RH)
{
const float c1 = -42.379;
const float c2 = 2.04901523;
const float c3 = 10.14333127;
const float c4 = -0.22475541;
const float c5 = -0.00683783;
const float c6 = -0.05481717;
const float c7 = 0.00122874;
const float c8 = 0.00085282;
const float c9 = -0.00000199;

float A = (( c5 * TF) + c2) * TF + c1;
float B = (((c7 * TF) + c4) * TF + c3) * R;
float C = (((c9 * TF) + c8) * TF + c6) * R * R;

return A + B + C;
float HI = 0;

if (TF >= 80)
{
const float c1 = -42.379;
const float c2 = 2.04901523;
const float c3 = 10.14333127;
const float c4 = -0.22475541;
const float c5 = -0.00683783;
const float c6 = -0.05481717;
const float c7 = 0.00122874;
const float c8 = 0.00085282;
const float c9 = -0.00000199;

float A = (( c5 * TF) + c2) * TF + c1;
float B = (((c7 * TF) + c4) * TF + c3) * RH;
float C = (((c9 * TF) + c8) * TF + c6) * RH * RH;

HI = A + B + C;
if ((RH < 13) && (TF <= 112))
{
HI += ((13 - RH) / 4) * sqrt((17 - abs(TF - 95.0)) / 17);
}
if ((RH > 87) && (TF < 87))
{
HI += ((RH - 85) / 10) * ((87 - TF) / 5);
}
return HI;
}

HI = 0.5 * (TF + 61.0 + ((TF - 68.0) * 1.2) + (RH * 0.094));

return HI;
}


// https://en.wikipedia.org/wiki/Heat_index
// 0.3.0 => https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
// previous https://en.wikipedia.org/wiki/Heat_index
// TODO add valid range for TF & R
// TODO optimize?
float heatIndexC(float celcius, float humidity)
{
float TF = celcius * (9.0 / 5.0) + 32;
float HI = (heatIndex(TF, humidity) - 32) * (5.0 / 9.0);
return HI;
/*
const float c1 = -8.78469475556;
const float c2 = 1.61139411;
const float c3 = 2.33854883889;
Expand All @@ -130,6 +157,7 @@ float heatIndexC(float celcius, float humidity)
float C = (((c9 * celcius) + c8) * celcius + c6) * humidity * humidity;
return A + B + C;
*/
}


Expand Down
8 changes: 5 additions & 3 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ unittest(test_dewpoint)

unittest(test_heatIndex)
{
assertEqualFloat(206.46, heatIndex(20, 50), 0.001);
assertEqualFloat(77.3509, heatIndex(68, 50), 0.001);
assertEqualFloat(25.1949, heatIndexC(20, 50), 0.001);
// Fahrenheit
assertEqualFloat(14.050, heatIndex(20, 50), 0.001);
assertEqualFloat(66.850, heatIndex(68, 50), 0.001);
// Celsius
assertEqualFloat(19.361, heatIndexC(20, 50), 0.001);
}


Expand Down

0 comments on commit 9354eab

Please sign in to comment.