From: Steven Baltakatei Sandoval Date: Sat, 2 Oct 2021 02:28:00 +0000 (+0000) Subject: feat(examples):Add absolute humidity mode X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02-2.git/commitdiff_plain/f4b2db2d7a5100e67c2f2102537e318b319c5321?ds=sidebyside;hp=4cbbc360566337406c8bf7b1d3b653d19b41ae4c feat(examples):Add absolute humidity mode --- diff --git a/examples/all-in-one-enviro-mini-bk.py b/examples/all-in-one-enviro-mini-bk.py index bff5497..8913cef 100755 --- a/examples/all-in-one-enviro-mini-bk.py +++ b/examples/all-in-one-enviro-mini-bk.py @@ -142,6 +142,75 @@ def get_cpu_temperature(): output, _error = process.communicate() return float(output[output.index('=') + 1:output.rindex("'")]) +def rel_to_abs(T,P,RH): + """Returns absolute humidity given relative humidity. + + Inputs: + -------- + T : float + Absolute temperature in units Kelvin (K). + P : float + Total pressure in units Pascals (Pa). + RH : float + Relative humidity in units percent (%). + + Output: + -------- + absolute_humidity : float + Absolute humidity in units [kg water vapor / kg dry air]. + + References: + -------- + 1. Sonntag, D. "Advancements in the field of hygrometry". 1994. https://doi.org/10.1127/metz/3/1994/51 + 2. Green, D. "Perry's Chemical Engineers' Handbook" (8th Edition). Page "12-4". McGraw-Hill Professional Publishing. 2007. + + Version: 0.0.1 + Author: Steven Baltakatei Sandoval + License: GPLv3+ + """ + + import math; + + # Check input types + T = float(T); + P = float(P); + RH = float(RH); + + #debug + # print('DEBUG:Input Temperature (K) :' + str(T)); + # print('DEBUG:Input Pressure (Pa) :' + str(P)); + # print('DEBUG:Input Rel. Humidity (%) :' + str(RH)); + + # Set constants and initial conversions + epsilon = 0.62198 # (molar mass of water vapor) / (molar mass of dry air) + t = T - 273.15; # Celsius from Kelvin + P_hpa = P / 100; # hectoPascals (hPa) from Pascals (Pa) + + # Calculate e_w(T), saturation vapor pressure of water in a pure phase, in Pascals + ln_e_w = -6096*T**-1 + 21.2409642 - 2.711193*10**-2*T + 1.673952*10**-5*T**2 + 2.433502*math.log(T); # Sonntag-1994 eq 7; e_w in Pascals + e_w = math.exp(ln_e_w); + e_w_hpa = e_w / 100; # also save e_w in hectoPascals (hPa) + # print('DEBUG:ln_e_w:' + str(ln_e_w)); # debug + # print('DEBUG:e_w:' + str(e_w)); # debug + + # Calculate f_w(P,T), enhancement factor for water + f_w = 1 + (10**-4*e_w_hpa)/(273 + t)*(((38 + 173*math.exp(-t/43))*(1 - (e_w_hpa / P_hpa))) + ((6.39 + 4.28*math.exp(-t / 107))*((P_hpa / e_w_hpa) - 1))); # Sonntag-1994 eq 22. + # print('DEBUG:f_w:' + str(f_w)); # debug + + # Calculate e_prime_w(P,T), saturation vapor pressure of water in air-water mixture, in Pascals + e_prime_w = f_w * e_w; # Sonntag-1994 eq 18 + # print('DEBUG:e_prime_w:' + str(e_prime_w)); # debug + + # Calculate e_prime, vapor pressure of water in air, in Pascals + e_prime = (RH / 100) * e_prime_w; + # print('DEBUG:e_prime:' + str(e_prime)); # debug + + # Calculate r, the absolute humidity, in [kg water vapor / kg dry air] + r = (epsilon * e_prime) / (P - e_prime); + # print('DEBUG:r:' + str(r)); # debug + + return float(r); + # Tuning factor for compensation. Decrease this number to adjust the # temperature down, and increase to adjust up @@ -158,6 +227,7 @@ light = 1 variables = ["temperature", "pressure", "humidity", + "humidity_abs", "light"] values = {} # Initialize values dictionary for v in variables: @@ -186,13 +256,15 @@ def pollSensors(): # now_temp_tuple (°C) # now_pressure_tuple (hPa) # now_humidity_tuple (%) + # now_humidity_abs_tuple (g water vapor / kg dry air) # now_illuminance_tuple (lux) - # Depends: time, bme280, ltr559, get_cpu_temperature() + # Depends: time, bme280, ltr559, get_cpu_temperature(), rel_to_abs() # Tell function to modify these global variables global now_temp_tuple global now_pressure_tuple global now_humidity_tuple + global now_humidity_abs_tuple global now_illuminance_tuple # Initialize cpu_temps = [] @@ -209,9 +281,14 @@ def pollSensors(): now_time_ns = time.time_ns() # Get time reading (unix epoch, nanoseconds) now_pressure = bme280.get_pressure() # get hPa from BME280 sensor now_pressure_tuple = (time.time_ns(), 'hPa', now_pressure) - # Get humidity reading - now_humidity = bme280.get_humidity() # get % humidity from BME280 sensor + # Get relative humidity reading + now_humidity = bme280.get_humidity() # get % relative humidity from BME280 sensor now_humidity_tuple = (time.time_ns(), '%', now_humidity) + # Calculate absolute humidity reading + raw_temp_k = 273.15 + raw_temp; # convert sensor temp from degC to K + now_pressure_pa = now_pressure * 100; # convert sensor pressure from hPa to Pa + now_humidity_abs = rel_to_abs(raw_temp_k,now_pressure_pa,now_humidity); # calc abs humidity + now_humidity_abs_tuple = (time.time_ns(), 'g/kg', now_humidity_abs); # Get light reading proximity = ltr559.get_proximity() # get proximity reading if proximity < 10: @@ -309,6 +386,7 @@ def updateBuffer(): global now_temp_tuple global now_pressure_tuple global now_humidity_tuple + global now_humidity_abs_tuple global now_illuminance_tuple global varLenBuffer global fixLenBuffer @@ -322,6 +400,7 @@ def updateBuffer(): #print('DEBUG:now_temp_tuple:' + str(now_temp_tuple)) #print('DEBUG:now_pressure_tuple:' + str(now_pressure_tuple)) #print('DEBUG:now_humidity_tuple:' + str(now_humidity_tuple)) + #print('DEBUG:now_humidity_abs_tuple:' + str(now_humidity_abs_tuple)) #print('DEBUG:now_illuminance_tuple:' + str(now_illuminance_tuple)) # Append new sensor tuples to varying-length buffer @@ -329,10 +408,12 @@ def updateBuffer(): varLenBuffer[variables[0]].append(now_temp_tuple) ## Pressure varLenBuffer[variables[1]].append(now_pressure_tuple) - ## Humidity + ## Relative Humidity varLenBuffer[variables[2]].append(now_humidity_tuple) + ## Absolute Humidity + varLenBuffer[variables[3]].append(now_humidity_abs_tuple) ## Illuminance - varLenBuffer[variables[3]].append(now_illuminance_tuple) + varLenBuffer[variables[4]].append(now_illuminance_tuple) #print('DEBUG:varLenBuffer:' + str(varLenBuffer)) # Trim outdated sensor tuples from varying-length buffer @@ -445,8 +526,19 @@ try: data = bme280.get_humidity() #display_text(variables[mode], data, unit) display_text2(variables[mode],data,unit,fixLenBuffer) - + if mode == 3: + # variable = "humidity_abs" + unit = "g/kg" + raw_temp = bme280.get_temperature() # get °C from BME280 sensor + raw_temp_k = 273.15 + raw_temp; # convert sensor temp from degC to K + now_pressure = bme280.get_pressure() # get hPa from BME280 sensor + now_pressure_pa = now_pressure * 100; # convert sensor pressure from hPa to Pa + now_humidity = bme280.get_humidity() # get % relative humidity from BME280 sensor + data = rel_to_abs(raw_temp_k,now_pressure_pa,now_humidity); # calc abs humidity + display_text2(variables[mode],data,unit,fixLenBuffer) + + if mode == 4: # variable = "light" unit = "Lux" if proximity < 10: