From e8ccfc7003747356d765180a950c1fb1994c05fc Mon Sep 17 00:00:00 2001 From: devance Date: Fri, 4 Oct 2024 17:30:08 +0530 Subject: [PATCH] adds ftoc and ctof functions along with a getHeatIndex function using Lans P. Rothfusz's formula --- DHT22.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++---- DHT22.h | 14 ++++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/DHT22.cpp b/DHT22.cpp index 2f13206..d011bc8 100644 --- a/DHT22.cpp +++ b/DHT22.cpp @@ -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(){ diff --git a/DHT22.h b/DHT22.h index 82ca6cb..5b87f58 100644 --- a/DHT22.h +++ b/DHT22.h @@ -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();