Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
yanbec committed Jun 22, 2018
2 parents 0ba0e12 + e1553a9 commit afac105
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 54 deletions.
7 changes: 3 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,12 @@ or
values: TempUnit_Celsius = return degrees Celsius, TempUnit_Fahrenheit = return degrees Fahrenheit
```

#### int HeatIndex(float temperature, float humidity, TempUnit tempUnit = TempUnit_Celsius)
#### float HeatIndex(float temperature, float humidity, TempUnit tempUnit = TempUnit_Celsius)

Calculate the heat index based on the temperature and humidity with the specified units.
The heat index is only calculated if the temperature is above 26.7°C or 80°F and humidity above 40%.
The precision is +/- 0.7°C / 1.3°F.
The U.S. NWS algorithm is used.
```
return: int heat index
return: float heat index in TempUnit
* Temperature: float
values: any float related to TempUnit
Expand Down
12 changes: 3 additions & 9 deletions examples/Environment_Calculations/Environment_Calculations.ino
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,9 @@ void printBME280Data
client->print(String( presUnit == BME280::PresUnit_hPa ? "hPa" :"Pa")); // expected hPa and Pa only

client->print("\t\tHeat Index: ");
if (temp > (tempUnit == BME280::TempUnit_Celsius ? 26.7 : 80))
{
int heatIndex = EnvironmentCalculations::HeatIndex(temp, hum, envTempUnit);
client->print(heatIndex);
}
else
{
client->print("Temperature out of range");
}
float heatIndex = EnvironmentCalculations::HeatIndex(temp, hum, envTempUnit);
client->print(heatIndex);
client->print("°"+ String(envTempUnit == EnvironmentCalculations::TempUnit_Celsius ? "C" :"F"));

client->print("\t\tAbsolute Humidity: ");
client->println(absHum);
Expand Down
20 changes: 10 additions & 10 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
BME280I2C KEYWORD1
BME280Spi KEYWORD1
begin KEYWORD2
temp KEYWORD2
pres KEYWORD2
hum KEYWORD2
read KEYWORD2
Altitude KEYWORD2
EquivalentSeaLevelPressure KEYWORD2
DewPoint KEYWORD2
HeatIndex KEYWORD2
AbsoluteHumidity KEYWORD2
begin KEYWORD2
temp KEYWORD2
pres KEYWORD2
hum KEYWORD2
read KEYWORD2
Altitude KEYWORD2
EquivalentSeaLevelPressure KEYWORD2
DewPoint KEYWORD2
HeatIndex KEYWORD2
AbsoluteHumidity KEYWORD2
24 changes: 24 additions & 0 deletions src/BME280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ bool BME280::Initialize()
if(success)
{
success &= ReadTrim();

if(m_settings.filter != Filter_Off)
{
InitializeFilter();
}

WriteSettings();
}

Expand All @@ -61,6 +67,24 @@ bool BME280::Initialize()
return m_initialized;
}


/****************************************************************/
bool BME280::InitializeFilter()
{
// Force an unfiltered measurement to populate the filter buffer.
// This fixes a bug that causes the first read to always be 28.82 °C 81732.34 hPa.
Filter filter = m_settings.filter;
m_settings.filter = Filter_Off;

WriteSettings();

float dummy;
read(dummy, dummy, dummy);

m_settings.filter = filter;
}


/****************************************************************/
bool BME280::ReadChipID()
{
Expand Down
8 changes: 7 additions & 1 deletion src/BME280.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,16 @@ class BME280
/* CONSTRUCTOR INIT FUNCTIONS */
/*****************************************************************/

//////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
/// Write configuration to BME280, return true if successful.
/// Must be called from any child classes.
virtual bool Initialize();

///////////////////////////////////////////////////////////////
/// Force a unfiltered measurement to populate the filter
/// buffer.
bool InitializeFilter();


/*****************************************************************/
/* ACCESSOR FUNCTIONS */
Expand Down
2 changes: 1 addition & 1 deletion src/BME280I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ bool BME280I2C::WriteRegister
Wire.write(data);
Wire.endTransmission();

return true; // TODO: Chech return values from wire calls.
return true; // TODO: Check return values from wire calls.
}


Expand Down
3 changes: 2 additions & 1 deletion src/BME280SpiSw.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class BME280SpiSw: public BME280{
protected:

////////////////////////////////////////////////////////////////
/// Method used at start up to initialize the class. Starts the I2C interface.
/// Method used at start up to initialize the class. Starts the
/// software SPI interface.
virtual bool Initialize();

private:
Expand Down
86 changes: 61 additions & 25 deletions src/EnvironmentCalculations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ Last Updated: Dec 23 2017.
This header must be included in any derived code or copies of the code.
*/

#include "EnvironmentCalculations.h"

#include <Arduino.h>
#include <math.h>


#define hi_coeff1 -42.379
#define hi_coeff2 2.04901523
#define hi_coeff3 10.14333127
#define hi_coeff4 -0.22475541
#define hi_coeff5 -0.00683783
#define hi_coeff6 -0.05481717
#define hi_coeff7 0.00122874
#define hi_coeff8 0.00085282
#define hi_coeff9 -0.00000199
/****************************************************************/
float EnvironmentCalculations::Altitude
(
Expand All @@ -44,7 +51,7 @@ float EnvironmentCalculations::Altitude
if (!isnan(pressure) && !isnan(referencePressure) && !isnan(outdoorTemp))
{
if(tempUnit != TempUnit_Celsius)
outdoorTemp = (outdoorTemp - 32.0) * (5.0 / 9.0); /*conversion to [C]*/
outdoorTemp = (outdoorTemp - 32.0) * (5.0 / 9.0); /*conversion to [°C]*/

altitude = pow(referencePressure / pressure, 0.190234) - 1;
altitude *= ((outdoorTemp + 273.15) / 0.0065);
Expand Down Expand Up @@ -89,40 +96,69 @@ float EnvironmentCalculations::AbsoluteHumidity


/****************************************************************/
int EnvironmentCalculations::HeatIndex
//FYI: https://ehp.niehs.nih.gov/1206273/ in detail this flow graph: https://ehp.niehs.nih.gov/wp-content/uploads/2013/10/ehp.1206273.g003.png
float EnvironmentCalculations::HeatIndex
(
float temperature,
float humidity,
TempUnit tempUnit
)
{
float heatindex = NAN;
bool metric = true;
float hi[2][9] = { {-8.784695,1.61139411,2.338549,-0.14611605,-1.2308094/100,-1.6424828/100,2.211732/1000,7.2546/10000,-3.582/1000000},
{-42.379,2.04901523,10.1433127,-0.22475541,-6.83783/1000,-5.481717/100,1.22874/1000,8.5282/10000,-1.99/1000000} };
float heatIndex(NAN);

//taken from https://de.wikipedia.org/wiki/Hitzeindex#Berechnung
if (tempUnit != TempUnit_Celsius)
if ( isnan(temperature) || isnan(humidity) )
{
metric = false;
return heatIndex;
}
if (!isnan(humidity) && !isnan(temperature) && humidity>40 && (metric ? temperature>26.7 : temperature>80)) {
heatindex = hi[metric ? 0: 1][0];
heatindex =+ hi[metric ? 0: 1][1] * temperature;
heatindex =+ hi[metric ? 0: 1][2] * humidity;
heatindex =+ hi[metric ? 0: 1][3] * temperature * humidity;
heatindex =+ hi[metric ? 0: 1][4] * temperature * temperature;
heatindex =+ hi[metric ? 0: 1][5] * humidity * humidity;
heatindex =+ hi[metric ? 0: 1][6] * temperature * temperature * humidity;
heatindex =+ hi[metric ? 0: 1][7] * temperature * humidity * humidity;
heatindex =+ hi[metric ? 0: 1][8] * temperature * temperature * humidity * humidity;
return int(heatindex);

if (tempUnit == TempUnit_Celsius)
{
temperature = (temperature * (9.0 / 5.0) + 32.0); /*conversion to [°F]*/
}
else {
return NAN;
// Using both Rothfusz and Steadman's equations
// https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
if (temperature <= 40)
{
heatIndex = temperature; //first red block
}
else
{
heatIndex = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + (humidity * 0.094)); //calculate A -- from the official site, not the flow graph

if (heatIndex >= 79)
{
/*
* calculate B
* the following calculation is optimized. Simply spoken, reduzed cpu-operations to minimize used ram and runtime.
* Check the correctness with the following link:
* https://www.wolframalpha.com/input/?source=nav&i=b%3D+x1+%2B+x2*T+%2B+x3*H+%2B+x4*T*H+%2B+x5*T*T+%2B+x6*H*H+%2B+x7*T*T*H+%2B+x8*T*H*H+%2B+x9*T*T*H*H
*/
heatIndex = hi_coeff1
+ (hi_coeff2 + hi_coeff4 * humidity + temperature * (hi_coeff5 + hi_coeff7 * humidity)) * temperature
+ (hi_coeff3 + humidity * (hi_coeff6 + temperature * (hi_coeff8 + hi_coeff9 * temperature))) * humidity;
//third red block
if ((humidity < 13) && (temperature >= 80.0) && (temperature <= 112.0))
{
heatIndex -= ((13.0 - humidity) * 0.25) * sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
} //fourth red block
else if ((humidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0))
{
heatIndex += (0.02 * (humidity - 85.0) * (87.0 - temperature));
}
}
}

if (tempUnit == TempUnit_Celsius)
{
return (heatIndex - 32.0) * (5.0 / 9.0); /*conversion back to [°C]*/
}
else
{
return heatIndex; //fifth red block
}
}


/****************************************************************/
float EnvironmentCalculations::EquivalentSeaLevelPressure
(
Expand All @@ -137,7 +173,7 @@ float EnvironmentCalculations::EquivalentSeaLevelPressure
if(!isnan(altitude) && !isnan(temp) && !isnan(pres))
{
if(tempUnit != TempUnit_Celsius)
temp = (temp - 32.0) * (5.0 / 9.0); /*conversion to [C]*/
temp = (temp - 32.0) * (5.0 / 9.0); /*conversion to [°C]*/

if(altUnit != AltitudeUnit_Meters)
altitude *= 0.3048; /*conversion to meters*/
Expand Down
7 changes: 4 additions & 3 deletions src/EnvironmentCalculations.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ namespace EnvironmentCalculations
/////////////////////////////////////////////////////////////////
/// Calculate the heatindex based on the humidity and temperature
/// in tempUnit.
/// the heatindex does work for values above 26.7°C/80°F and 40% humidity.
/// The formula based on the Heat Index Equation of the US National Weather Service
/// https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
/// @param temperature in tempUnit
/// @param humidity in percentage
/// @param temptUnit in °C or °F. default=TempUnit_Celsius
/// @return Calculated heatindex as integer in TempUnit
int HeatIndex(
/// @return Calculated heatindex as float in TempUnit
float HeatIndex(
float temperature,
float humidity,
TempUnit tempUnit = TempUnit_Celsius);
Expand Down

0 comments on commit afac105

Please sign in to comment.