X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02-2.git/blobdiff_plain/4cbbc360566337406c8bf7b1d3b653d19b41ae4c..eba03e3f2ea5a98a2e7bae4d229e0117fe619d9a:/examples/all-in-one-enviro-mini-bk.py?ds=sidebyside diff --git a/examples/all-in-one-enviro-mini-bk.py b/examples/all-in-one-enviro-mini-bk.py index bff5497..88c3475 100755 --- a/examples/all-in-one-enviro-mini-bk.py +++ b/examples/all-in-one-enviro-mini-bk.py @@ -99,7 +99,7 @@ def display_text(variable, data, unit): # Displays data and text on the 0.96" LCD def display_text2(variable, data, unit, values): # Scale the values for the variable between 0 and 1 - print('DEBUG:len(values[' + str(variable) + ']):' + str(len(values[variable]))) + #print('DEBUG:len(values[' + str(variable) + ']):' + str(len(values[variable]))) #print('DEBUG:values[' + str(variable) + ']:' + str(values[variable])) vmin = min(values[variable]) vmax = max(values[variable]) @@ -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_gkg_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_gkg_tuple global now_illuminance_tuple # Initialize cpu_temps = [] @@ -209,9 +281,15 @@ 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 kg/kg abs humidity + now_humidity_abs_gkg = now_humidity_abs * 1000; + now_humidity_abs_gkg_tuple = (time.time_ns(), 'g/kg', now_humidity_abs_gkg); # Get light reading proximity = ltr559.get_proximity() # get proximity reading if proximity < 10: @@ -309,6 +387,7 @@ def updateBuffer(): global now_temp_tuple global now_pressure_tuple global now_humidity_tuple + global now_humidity_abs_gkg_tuple global now_illuminance_tuple global varLenBuffer global fixLenBuffer @@ -322,6 +401,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_gkg_tuple:' + str(now_humidity_abs_gkg_tuple)) #print('DEBUG:now_illuminance_tuple:' + str(now_illuminance_tuple)) # Append new sensor tuples to varying-length buffer @@ -329,10 +409,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_gkg_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 +527,27 @@ 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 + now_humidity_abs = rel_to_abs(raw_temp_k,now_pressure_pa,now_humidity); # calc [kg water / kg dry air] abs humidity + now_humidity_abs_gkg = now_humidity_abs * 1000; # convert kg/kg to g/kg abs humidity + data = now_humidity_abs_gkg; + # print('DEBUG:raw_temp:' + str(raw_temp)); + # print('DEBUG:raw_temp_k:' + str(raw_temp_k)); + # print('DEBUG:now_pressure:' + str(now_pressure)); + # print('DEBUG:now_pressure_pa:' + str(now_pressure_pa)); + # print('DEBUG:now_humidity:' + str(now_humidity)); + # print('DEBUG:now_humidity_abs_gkg:' + str(data)); + display_text2(variables[mode],data,unit,fixLenBuffer) + + if mode == 4: # variable = "light" unit = "Lux" if proximity < 10: