From bed8e27ef2e0a59b91b5189d4f556d1305e3abd3 Mon Sep 17 00:00:00 2001 From: flamerten Date: Sat, 17 Dec 2022 13:18:56 +0800 Subject: [PATCH 1/3] Add HeatIndexmethod --- Adafruit_BME280.cpp | 34 ++++++++++++++++++++++++++++++++++ Adafruit_BME280.h | 2 ++ 2 files changed, 36 insertions(+) diff --git a/Adafruit_BME280.cpp b/Adafruit_BME280.cpp index 4c82e05..c50f8e3 100644 --- a/Adafruit_BME280.cpp +++ b/Adafruit_BME280.cpp @@ -447,6 +447,40 @@ float Adafruit_BME280::readAltitude(float seaLevel) { return 44330.0 * (1.0 - pow(atmospheric / seaLevel, 0.1903)); } +/*! + * Computes Heat Index based on https://byjus.com/heat-index-formula/ + * + * @param isCelcius - if true return Heat Index in degrees celcius else return in Farenheit + */ +float Adafruit_BME280::readHeatIndex(bool isCelcius){ + float tempF = readTemperature() * 1.8 + 32; + float humidity = readHumidity(); + float hi = 0.5 * (tempF + 61.0 + ((tempF - 68.0) * 1.2) + (humidity * 0.094)); + + if (hi > 79) { + hi = -42.379 + 2.04901523 * tempF + 10.14333127 * humidity + + -0.22475541 * tempF * humidity + + -0.00683783 * pow(tempF, 2) + + -0.05481717 * pow(humidity, 2) + + 0.00122874 * pow(tempF, 2) * humidity + + 0.00085282 * tempF * pow(humidity, 2) + + -0.00000199 * pow(tempF, 2) * pow(humidity, 2); + + if ((humidity < 13) && (tempF >= 80.0) && + (tempF <= 112.0)) + hi -= ((13.0 - humidity) * 0.25) * + sqrt((17.0 - abs(tempF - 95.0)) * 0.05882); + + else if ((humidity > 85.0) && (tempF >= 80.0) && + (tempF <= 87.0)) + hi += ((humidity - 85.0) * 0.1) * ((87.0 - tempF) * 0.2); + } + + if(isCelcius) return (hi - 32) * 0.5556; //convert back to celcius + else return hi; + +} + /*! * Calculates the pressure at sea level (in hPa) from the specified * altitude (in meters), and atmospheric pressure (in hPa). diff --git a/Adafruit_BME280.h b/Adafruit_BME280.h index ffa8018..41f4f8e 100644 --- a/Adafruit_BME280.h +++ b/Adafruit_BME280.h @@ -229,6 +229,8 @@ class Adafruit_BME280 { float readTemperature(void); float readPressure(void); float readHumidity(void); + float readHeatIndex(bool isCelcius); + float readAltitude(float seaLevel); float seaLevelForAltitude(float altitude, float pressure); From 8a1d2dc9728ce2e6c6740a7e798dbce6763012bb Mon Sep 17 00:00:00 2001 From: flamerten Date: Sat, 17 Dec 2022 22:24:25 +0800 Subject: [PATCH 2/3] Fix Clang formatting --- Adafruit_BME280.cpp | 24 +++++++++++------------- Adafruit_BME280.h | 1 - 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Adafruit_BME280.cpp b/Adafruit_BME280.cpp index c50f8e3..4388c3c 100644 --- a/Adafruit_BME280.cpp +++ b/Adafruit_BME280.cpp @@ -449,36 +449,34 @@ float Adafruit_BME280::readAltitude(float seaLevel) { /*! * Computes Heat Index based on https://byjus.com/heat-index-formula/ - * - * @param isCelcius - if true return Heat Index in degrees celcius else return in Farenheit + * @param isCelcius - if true return Heat Index in degrees celcius else return + * in Farenheit */ -float Adafruit_BME280::readHeatIndex(bool isCelcius){ +float Adafruit_BME280::readHeatIndex(bool isCelcius) { float tempF = readTemperature() * 1.8 + 32; float humidity = readHumidity(); float hi = 0.5 * (tempF + 61.0 + ((tempF - 68.0) * 1.2) + (humidity * 0.094)); if (hi > 79) { hi = -42.379 + 2.04901523 * tempF + 10.14333127 * humidity + - -0.22475541 * tempF * humidity + - -0.00683783 * pow(tempF, 2) + + -0.22475541 * tempF * humidity + -0.00683783 * pow(tempF, 2) + -0.05481717 * pow(humidity, 2) + 0.00122874 * pow(tempF, 2) * humidity + 0.00085282 * tempF * pow(humidity, 2) + -0.00000199 * pow(tempF, 2) * pow(humidity, 2); - if ((humidity < 13) && (tempF >= 80.0) && - (tempF <= 112.0)) + if ((humidity < 13) && (tempF >= 80.0) && (tempF <= 112.0)) hi -= ((13.0 - humidity) * 0.25) * sqrt((17.0 - abs(tempF - 95.0)) * 0.05882); - else if ((humidity > 85.0) && (tempF >= 80.0) && - (tempF <= 87.0)) + else if ((humidity > 85.0) && (tempF >= 80.0) && (tempF <= 87.0)) hi += ((humidity - 85.0) * 0.1) * ((87.0 - tempF) * 0.2); - } - - if(isCelcius) return (hi - 32) * 0.5556; //convert back to celcius - else return hi; + } + if (isCelcius) + return (hi - 32) * 0.5556; // convert back to celcius + else + return hi; } /*! diff --git a/Adafruit_BME280.h b/Adafruit_BME280.h index 41f4f8e..3036d47 100644 --- a/Adafruit_BME280.h +++ b/Adafruit_BME280.h @@ -231,7 +231,6 @@ class Adafruit_BME280 { float readHumidity(void); float readHeatIndex(bool isCelcius); - float readAltitude(float seaLevel); float seaLevelForAltitude(float altitude, float pressure); uint32_t sensorID(void); From 55a2d11ab51c58d6b25439f7d21ce0adf2b04c2b Mon Sep 17 00:00:00 2001 From: flamerten Date: Sat, 17 Dec 2022 23:09:29 +0800 Subject: [PATCH 3/3] Added return documentation to method --- Adafruit_BME280.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Adafruit_BME280.cpp b/Adafruit_BME280.cpp index 4388c3c..111a3df 100644 --- a/Adafruit_BME280.cpp +++ b/Adafruit_BME280.cpp @@ -451,6 +451,8 @@ float Adafruit_BME280::readAltitude(float seaLevel) { * Computes Heat Index based on https://byjus.com/heat-index-formula/ * @param isCelcius - if true return Heat Index in degrees celcius else return * in Farenheit + * @returns Heat Index in Celcius or Farenhite depending on parameter + * isCelcius */ float Adafruit_BME280::readHeatIndex(bool isCelcius) { float tempF = readTemperature() * 1.8 + 32;