From b19b3a7d5cf32f1cde0d40a5b6d4cb73ac213e79 Mon Sep 17 00:00:00 2001 From: Stephen Jefferson Date: Mon, 20 Mar 2023 14:38:41 +0000 Subject: [PATCH 1/4] Add sea level pressure calculation --- enviro/boards/weather.py | 16 +++++++++++++--- enviro/config_defaults.py | 13 +++++++++++++ enviro/config_template.py | 5 +++++ enviro/helpers.py | 7 +++++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index 9b44e13..8714830 100644 --- a/enviro/boards/weather.py +++ b/enviro/boards/weather.py @@ -3,7 +3,7 @@ from breakout_ltr559 import BreakoutLTR559 from machine import Pin, PWM from pimoroni import Analog -from enviro import i2c, activity_led +from enviro import i2c, activity_led, config import enviro.helpers as helpers from phew import logging from enviro.constants import WAKE_REASON_RTC_ALARM, WAKE_REASON_BUTTON_PRESS @@ -190,11 +190,21 @@ def get_sensor_readings(seconds_since_last, is_usb_power): ltr_data = ltr559.get_reading() rain, rain_per_second = rainfall(seconds_since_last) + # Adjust pressure to calculated sea level value if set to in config + pressure = round(bme280_data[1] / 100.0, 2) + temperature = round(bme280_data[0], 2) + + if config.sea_level_pressure: + logging.info(f" - recorded temperature: {temperature}") + logging.info(f" - recorded pressure: {pressure}") + pressure = round(helpers.get_sea_level_pressure(pressure, temperature, config.height_above_sea_level), 2) + logging.info(f" - calculated mean sea level pressure: {pressure}") + from ucollections import OrderedDict return OrderedDict({ - "temperature": round(bme280_data[0], 2), + "temperature": temperature, "humidity": round(bme280_data[2], 2), - "pressure": round(bme280_data[1] / 100.0, 2), + "pressure": pressure, "luminance": round(ltr_data[BreakoutLTR559.LUX], 2), "wind_speed": wind_speed(), "rain": rain, diff --git a/enviro/config_defaults.py b/enviro/config_defaults.py index 63a5877..0f4ee74 100644 --- a/enviro/config_defaults.py +++ b/enviro/config_defaults.py @@ -23,6 +23,19 @@ def add_missing_config_settings(): except AttributeError: warn_missing_config_setting("wifi_country") config.wifi_country = "GB" + + try: + config.sea_level_pressure + except AttributeError: + warn_missing_config_setting("sea_level_pressure") + config.sea_level_pressure = False + + try: + config.height_above_sea_level + except AttributeError: + warn_missing_config_setting("height_above_sea_level") + config.height_above_sea_level = 0 + def warn_missing_config_setting(setting): logging.warn(f"> config setting '{setting}' missing, please add it to config.py") diff --git a/enviro/config_template.py b/enviro/config_template.py index 11404a9..0ef218b 100644 --- a/enviro/config_template.py +++ b/enviro/config_template.py @@ -55,3 +55,8 @@ # compensate for usb power usb_power_temperature_offset = 4.5 + +# sea level pressure conversion (adjusts measured pressure output for mean sea level value) +sea_level_pressure = False +# height in metres +height_above_sea_level = 0 \ No newline at end of file diff --git a/enviro/helpers.py b/enviro/helpers.py index 1503ad4..fd4ce73 100644 --- a/enviro/helpers.py +++ b/enviro/helpers.py @@ -98,3 +98,10 @@ def get_saturation_vapor_pressure(temperature_in_k): temperature_in_k * (a1*v + a2*v**1.5 + a3*v**3 + a4*v**3.5 + a5*v**4 + a6*v**7.5) ) + +# Calculates mean sea level pressure (QNH) from observed pressure +# https://keisan.casio.com/exec/system/1224575267 +def get_sea_level_pressure(observed_pressure, temperature_in_c, altitude_in_m): +# def sea(pressure, temperature, height): + qnh = observed_pressure * ((1 - ((0.0065 * altitude_in_m) / (temperature_in_c + (0.0065 * altitude_in_m) + 273.15)))** -5.257) + return qnh \ No newline at end of file From f4118ac138f73df00f837548a6e701796a3ef213 Mon Sep 17 00:00:00 2001 From: Stephen Jefferson Date: Mon, 20 Mar 2023 18:25:16 +0000 Subject: [PATCH 2/4] Reduce rounding error --- enviro/boards/weather.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index 8714830..3c9f8c6 100644 --- a/enviro/boards/weather.py +++ b/enviro/boards/weather.py @@ -191,8 +191,8 @@ def get_sensor_readings(seconds_since_last, is_usb_power): rain, rain_per_second = rainfall(seconds_since_last) # Adjust pressure to calculated sea level value if set to in config - pressure = round(bme280_data[1] / 100.0, 2) - temperature = round(bme280_data[0], 2) + pressure = bme280_data[1] / 100.0 + temperature = bme280_data[0] if config.sea_level_pressure: logging.info(f" - recorded temperature: {temperature}") @@ -202,9 +202,9 @@ def get_sensor_readings(seconds_since_last, is_usb_power): from ucollections import OrderedDict return OrderedDict({ - "temperature": temperature, + "temperature": round(temperature, 2), "humidity": round(bme280_data[2], 2), - "pressure": pressure, + "pressure": round(pressure, 2), "luminance": round(ltr_data[BreakoutLTR559.LUX], 2), "wind_speed": wind_speed(), "rain": rain, From b0163b6d5e6fbc311660610e1f37f4e560d1979f Mon Sep 17 00:00:00 2001 From: Stephen Jefferson Date: Fri, 24 Mar 2023 14:42:59 +0000 Subject: [PATCH 3/4] Return sea level in new reading key --- enviro/boards/weather.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index 3c9f8c6..42a81f7 100644 --- a/enviro/boards/weather.py +++ b/enviro/boards/weather.py @@ -193,15 +193,9 @@ def get_sensor_readings(seconds_since_last, is_usb_power): # Adjust pressure to calculated sea level value if set to in config pressure = bme280_data[1] / 100.0 temperature = bme280_data[0] - - if config.sea_level_pressure: - logging.info(f" - recorded temperature: {temperature}") - logging.info(f" - recorded pressure: {pressure}") - pressure = round(helpers.get_sea_level_pressure(pressure, temperature, config.height_above_sea_level), 2) - logging.info(f" - calculated mean sea level pressure: {pressure}") from ucollections import OrderedDict - return OrderedDict({ + readings = OrderedDict({ "temperature": round(temperature, 2), "humidity": round(bme280_data[2], 2), "pressure": round(pressure, 2), @@ -211,3 +205,12 @@ def get_sensor_readings(seconds_since_last, is_usb_power): "rain_per_second": rain_per_second, "wind_direction": wind_direction() }) + + if config.sea_level_pressure: + logging.info(f" - recorded temperature: {temperature}") + logging.info(f" - recorded pressure: {pressure}") + sea_level_pressure = round(helpers.get_sea_level_pressure(pressure, temperature, config.height_above_sea_level), 2) + logging.info(f" - calculated mean sea level pressure: {sea_level_pressure}") + readings["sea_level_pressure"] = round(sea_level_pressure, 2) + + return readings \ No newline at end of file From dfe6c494fe399136c7769470b2e56d8b15e8d3e6 Mon Sep 17 00:00:00 2001 From: Stephen Jefferson Date: Sat, 1 Apr 2023 14:25:13 +0100 Subject: [PATCH 4/4] Documentation --- documentation/boards/enviro-weather.md | 1 + 1 file changed, 1 insertion(+) diff --git a/documentation/boards/enviro-weather.md b/documentation/boards/enviro-weather.md index 7b3866d..25e4ff7 100644 --- a/documentation/boards/enviro-weather.md +++ b/documentation/boards/enviro-weather.md @@ -11,6 +11,7 @@ Enviro Weather is a super slimline all in one board for keeping a (weather) eye |Temperature|`temperature`|celcius|°C|`22.11`| |Humidity|`humidity`|percent|%|`55.42`| |Air Pressure|`pressure`|hectopascals|hPa|`997.16`| +|Adjusted Sea Level Air Pressure|`sea_level_pressure`|hectopascals|hPa|`1014.06`| |Luminance|`luminance`|lux|lx|`35`| |Rainfall|`rain`|millimetres|mm|`1.674`| |Rainfall Average|`rain_per_second`|millimetres per second|mm/s|`1.674`|