Skip to content

Commit

Permalink
Merge pull request #7 from DevanceJ/feat/heat-index
Browse files Browse the repository at this point in the history
adds ftoc and ctof functions along with a getHeatIndex function using…
  • Loading branch information
dvarrel authored Oct 5, 2024
2 parents f40ac2e + e8ccfc7 commit 48aa698
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
46 changes: 42 additions & 4 deletions DHT22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,54 @@ String DHT22::getRawStrData(){
return r;
}

// Convert Celsius to Fahrenheit
float DHT22::convertCtoF(float c) { return c * 1.8 + 32; }
// Convert Fahrenheit to Celsius
float DHT22::convertFtoC(float f) { return (f - 32) * 0.55555; }

float DHT22::getHumidity(){
if (readSensor() != OK) return -1;
else return _h16bits/10.0;
}


float DHT22::getTemperature(){
// M = true : Celsius ; M = false : Fahrenheit
float DHT22::getTemperature(bool M){
if (readSensor() != OK) return -273;
if (bitRead(_t16bits,15)) return (_t16bits & 0x7FFF)/-10.0;
else return _t16bits/10.0;
float temperature = (bitRead(_t16bits, 15)) ? (_t16bits & 0x7FFF) / -10.0 : _t16bits / 10.0;
return M ? temperature : convertCtoF(temperature);
}

// https://www.weather.gov/media/ffc/ta_htindx.PDF
float DHT22::getHeatIndex(float temperature, float percentHumidity, bool isCelsius){
float hi;

if (isCelsius)
temperature = convertCtoF(temperature);

hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) +
(percentHumidity * 0.094));

if (hi > 79)
{
hi = -42.379 + 2.04901523 * temperature + 10.14333127 * percentHumidity +
-0.22475541 * temperature * percentHumidity +
-0.00683783 * pow(temperature, 2) +
-0.05481717 * pow(percentHumidity, 2) +
0.00122874 * pow(temperature, 2) * percentHumidity +
0.00085282 * temperature * pow(percentHumidity, 2) +
-0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2);

if ((percentHumidity < 13) && (temperature >= 80.0) &&
(temperature <= 112.0))
hi -= ((13.0 - percentHumidity) * 0.25) *
sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);

else if ((percentHumidity > 85.0) && (temperature >= 80.0) &&
(temperature <= 87.0))
hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2);
}

return isCelsius ? convertFtoC(hi) : hi;
}

uint8_t DHT22::getLastError(){
Expand Down
14 changes: 12 additions & 2 deletions DHT22.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,18 @@ class DHT22{
/** @return humidity %rH **/
float getHumidity();

/** @return temperature in °C **/
float getTemperature();
/** if M=true, return temperature in °C, else in °F **/
/** @return temperature in °C or °F **/
float getTemperature(bool M = true);

/** @return temp in °C **/
float convertCtoF(float);

/** @return temp in °F **/
float convertFtoC(float);

/** @return heat index in °C or °F **/
float getHeatIndex(float t, float h, bool isCelsius = true);

/** @return code from last readSensor() request **/
uint8_t getLastError();
Expand Down

0 comments on commit 48aa698

Please sign in to comment.