Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sea level pressure calculation #159

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/boards/enviro-weather.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`|
Expand Down
21 changes: 17 additions & 4 deletions enviro/boards/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -190,14 +190,27 @@ 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 = bme280_data[1] / 100.0
temperature = bme280_data[0]

from ucollections import OrderedDict
return OrderedDict({
"temperature": round(bme280_data[0], 2),
readings = OrderedDict({
"temperature": round(temperature, 2),
"humidity": round(bme280_data[2], 2),
"pressure": round(bme280_data[1] / 100.0, 2),
"pressure": round(pressure, 2),
"luminance": round(ltr_data[BreakoutLTR559.LUX], 2),
"wind_speed": wind_speed(),
"rain": rain,
"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
13 changes: 13 additions & 0 deletions enviro/config_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
5 changes: 5 additions & 0 deletions enviro/config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions enviro/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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