feat(examples/a): Add absolute dew point temperature display mode master-bk
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Sat, 2 Oct 2021 04:22:16 +0000 (04:22 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Sat, 2 Oct 2021 04:22:16 +0000 (04:22 +0000)
examples/all-in-one-enviro-mini-bk.py

index 88c3475e18ca76abf592a4e09edc1ab811f89ef0..de97514d6a11f5515a74a166b23e476a984bd20b 100755 (executable)
@@ -211,6 +211,118 @@ def rel_to_abs(T,P,RH):
 
     return float(r);
 
+def rel_to_dpt(T,P,RH):
+    """Returns dew point temperature 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:
+    --------
+    T_d : float
+        Dew point temperature in units Kelvin (K).
+
+    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
+
+    n = 0; repeat_flag = True;
+    while repeat_flag == True:
+        # print('DEBUG:n:' + str(n)); # debug
+
+        # Calculate f_w_td, the enhancement factor for water at dew point temperature.
+        if n == 0:
+            f = 1.0016 + 3.15*10**-6*P_hpa - (0.074 / P_hpa); # Sonntag-1994 eq 24
+            f_w_td = f; # initial approximation
+        elif n > 0:
+            t_d_prev = float(t_d); # save previous t_d value for later comparison
+            f_w_td = 1 + (10**-4*e_w_hpa)/(273 + t_d)*(((38 + 173*math.exp(-t_d/43))*(1 - (e_w_hpa / P_hpa))) + ((6.39 + 4.28*math.exp(-t_d / 107))*((P_hpa / e_w_hpa) - 1))); # Sonntag-1994 eq 22.
+        # print('DEBUG:f_w_td:' + str(f_w_td)); # debug
+
+        # Calculate e, the vapor pressure of water in the pure phase, in Pascals
+        e = (e_prime / f_w_td); # Sonntag-1994 eq 9 and 20
+        # print('DEBUG:e:' + str(e)); # debug
+
+        # Calculate y, an intermediate dew point calculation variable
+        y = math.log(e / 611.213);
+        # print('DEBUG:y:' + str(y)); # debug
+
+        # Calculate t_d, the dew point temperature in degrees Celsius
+        t_d = 13.715*y + 8.4262*10**-1*y**2 + 1.9048*10**-2*y**3 + 7.8158*10**-3*y**4;# Sonntag-1994 eq 10
+        # print('DEBUG:t_d:' + str(t_d)); # debug
+
+        if n == 0:
+            # First run
+            repeat_flag = True;
+        else:
+            # Test t_d accuracy
+            t_d_diff = math.fabs(t_d - t_d_prev);
+            # print('DEBUG:t_d     :' + str(t_d)); # debug
+            # print('DEBUG:t_d_prev:' + str(t_d_prev)); # debug
+            # print('DEBUG:t_d_diff:' + str(t_d_diff)); # debug
+            if t_d_diff < 0.01:
+                repeat_flag = False;
+            else:
+                repeat_flag = True;
+
+        # Calculate T_d, the dew point temperature in Kelvin
+        T_d = 273.15 + t_d;
+        # print('DEBUG:T_d:' + str(T_d)); # debug
+
+        if n > 100:
+            return T_d; # good enough
+
+        # update loop counter
+        n += 1;
+    return T_d;
 
 # Tuning factor for compensation. Decrease this number to adjust the
 # temperature down, and increase to adjust up
@@ -228,6 +340,7 @@ variables = ["temperature",
              "pressure",
              "humidity",
              "humidity_abs",
+             "dewpoint_temperature",
              "light"]
 values = {} # Initialize values dictionary
 for v in variables:
@@ -258,13 +371,14 @@ def pollSensors():
     #         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(), rel_to_abs()
+    # Depends: time, bme280, ltr559, get_cpu_temperature(), rel_to_abs(), rel_to_dpt()
 
     # 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_humidity_dpt_c_tuple
     global now_illuminance_tuple
     # Initialize
     cpu_temps = []
@@ -290,6 +404,10 @@ def pollSensors():
     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);
+    # Calculate dew point temperature
+    now_humidity_dpt = rel_to_dpt(raw_temp_k, now_pressure_pa, now_humidity); # calc K dpt
+    now_humidity_dpt_c = now_humidity_dpt - 273.15;
+    now_humidity_dpt_c_tuple = (time.time_ns(), '°C', now_humidity_dpt_c);
     # Get light reading
     proximity = ltr559.get_proximity() # get proximity reading
     if proximity < 10:
@@ -388,6 +506,7 @@ def updateBuffer():
     global now_pressure_tuple
     global now_humidity_tuple
     global now_humidity_abs_gkg_tuple
+    global now_humidity_dpt_c_tuple
     global now_illuminance_tuple
     global varLenBuffer
     global fixLenBuffer
@@ -402,6 +521,7 @@ def updateBuffer():
     #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_humidity_dpt_c_tuple:' + str(now_humidity_dpt_c_tuple))
     #print('DEBUG:now_illuminance_tuple:' + str(now_illuminance_tuple))    
 
     # Append new sensor tuples to varying-length buffer
@@ -413,8 +533,10 @@ def updateBuffer():
     varLenBuffer[variables[2]].append(now_humidity_tuple)
     ## Absolute Humidity
     varLenBuffer[variables[3]].append(now_humidity_abs_gkg_tuple)
+    ## Dew Point Temperature
+    varLenBuffer[variables[4]].append(now_humidity_dpt_c_tuple)
     ## Illuminance
-    varLenBuffer[variables[4]].append(now_illuminance_tuple)
+    varLenBuffer[variables[5]].append(now_illuminance_tuple)
     #print('DEBUG:varLenBuffer:' + str(varLenBuffer))
 
     # Trim outdated sensor tuples from varying-length buffer
@@ -544,10 +666,29 @@ try:
             # 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));
+            # print('DEBUG:now_humidity_abs_gkg:' + str(now_humidity_abs_gkg));
             display_text2(variables[mode],data,unit,fixLenBuffer)
-            
+
         if mode == 4:
+            # variable = "humidity_abs"
+            unit = "°C"
+            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_dpt = rel_to_dpt(raw_temp_k,now_pressure_pa,now_humidity); # calc K dpt humidity
+            now_humidity_dpt_c = now_humidity_dpt - 273.15; # convert K to °C dpt humidity
+            data = now_humidity_dpt_c;
+            # 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_dpt_c:' + str(now_humidity_dpt_c));
+            display_text2(variables[mode],data,unit,fixLenBuffer)
+            
+        if mode == 5:
             # variable = "light"
             unit = "Lux"
             if proximity < 10: