B4R Code Snippet Calculate Heat Index (B4x compatible)

I wanted to cycle between the actual air temperature and heat index on my little ESP-12E pool project. This can be used in any B4x IDE. It requires a known temperature and humidity.

Calculating the heat index requires the temperature to be in Fahrenheit, so if working in Celsius, you need to convert it first: F = (C x 1.8) + 32
To convert Fahrenheit back to Celcius: C = (F - 32) / 1.8

Source of the formula to calculate heat index:
Calculator
Formula

The Calculator webpage mentions that heat index results below 80 may be meaningless if outside the range of the chart, so if it calculates a heat index below 80 then I just return the original temperature and ignore the heat index.

I've spot-checked the calculations in my code against their online calculator and they all came back the same. One slight difference is that I am using decimals (fractions) with the temperature that I send to the sub and the online calculator works with whole numbers (rounding up or down).

Wind speed can also be a factor in calculating the heat index, but I do not have that data in my project nor do I know how to factor that into the formula.

B4X:
Private Sub HeatIndex (dTempF As Double, dHum As Double) As Double
    'Source:  http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
    
    Dim HI As Double, Adjustment As Double
    
    
    'First get simplified heat index to see if it is at 80 F or higher
    'HI = 0.5 * {T + 61.0 + [(T-68.0)*1.2] + (RH*0.094)}
    
    HI = .5 * (dTempF + 61 + ((dTempF - 68) * 1.2)) + (dHum * .094)
    
    'Per the equation and chart, if heat index is below 80 then calculating the heat index
    'could produce meaningless results
    If HI < 80 Then Return dTempF
    
    'If heat index is 80 or higher, do a more thorough calculation
    'HI = -42.379 + 2.04901523*T + 10.14333127*RH - .22475541*T*RH
    ' - .00683783*T*T - .05481717*RH*RH + .00122874*T*T*RH
    ' + .00085282*T*RH*RH - .00000199*T*T*RH*RH
    
    HI = -42.379 + (2.04901523 * dTempF) + (10.14333127 * dHum) - (.22475541 * dTempF * dHum) _
    - (.00683783 * dTempF * dTempF) - (.05481717 * dHum * dHum) + (.00122874 * dTempF * dTempF * dHum) _
    + (.00085282 * dTempF * dHum * dHum) - (.00000199 * dTempF * dTempF * dHum * dHum)
    
    'Adjustments
    'If the RH is less than 13% and the temperature is between 80 and 112 degrees F,
    'then the following adjustment is subtracted from HI:
    'Adjustment = [(13-RH)/4]*SQRT{[17-Abs(T-95.)]/17}
    
    If dHum < 13 And dTempF > 79.9 And dTempF < 112.1 Then
        Adjustment = ((13 - dHum) / 4) * Sqrt((17 - Abs(dTempF - 95)) / 17)
        HI = HI - Adjustment
        Return HI
    End If
    
    'If the RH is greater than 85% and the temperature is between 80 and 87 degrees F,
    'then the following adjustment is added to HI:
    'Adjustment = [(RH-85)/10] * [(87-T)/5]
    
    If dHum > 85 And dTempF > 79.9 And dTempF < 87.1 Then
        Adjustment = ((dHum - 85) / 10) * ((87-dTempF) / 5)
        HI = HI + Adjustment
        Return HI
    End If
    
    'If we get here, then no adjustments are necessary so return what we came up with in the
    'long equation above
    Return HI
End Sub
 
Top