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

Weather battery temperature compensation #186

Open
wants to merge 5 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
25 changes: 22 additions & 3 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,10 +190,29 @@ def get_sensor_readings(seconds_since_last, is_usb_power):
ltr_data = ltr559.get_reading()
rain, rain_per_second = rainfall(seconds_since_last)

temperature = bme280_data[0]
humidity = bme280_data[2]

# Compensate for additional heating when on different power sources - this
# also changes the relative humidity value
logging.info(f" - recorded temperature: {temperature}")
logging.info(f" - recorded humidity: {humidity}")
if is_usb_power:
adjusted_temperature = temperature - config.usb_power_temperature_offset
logging.info(f" - USB temperature offset: {config.usb_power_temperature_offset}")
else:
adjusted_temperature = temperature - config.battery_power_temperature_offset
logging.info(f" - Battery temperature offset: {config.battery_power_temperature_offset}")
absolute_humidity = helpers.relative_to_absolute_humidity(humidity, temperature)
humidity = helpers.absolute_to_relative_humidity(absolute_humidity, adjusted_temperature)
temperature = adjusted_temperature
logging.info(f" - adjusted temperature: {temperature}")
logging.info(f" - adjusted humidity: {humidity}")

from ucollections import OrderedDict
return OrderedDict({
"temperature": round(bme280_data[0], 2),
"humidity": round(bme280_data[2], 2),
"temperature": round(temperature, 2),
"humidity": round(humidity, 2),
"pressure": round(bme280_data[1] / 100.0, 2),
"luminance": round(ltr_data[BreakoutLTR559.LUX], 2),
"wind_speed": wind_speed(),
Expand Down
7 changes: 7 additions & 0 deletions enviro/config_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from phew import logging

DEFAULT_USB_POWER_TEMPERATURE_OFFSET = 4.5
DEFAULT_BATTERY_POWER_TEMPERATURE_OFFSET = 0.0


def add_missing_config_settings():
Expand All @@ -17,6 +18,12 @@ def add_missing_config_settings():
except AttributeError:
warn_missing_config_setting("usb_power_temperature_offset")
config.usb_power_temperature_offset = DEFAULT_USB_POWER_TEMPERATURE_OFFSET

try:
config.battery_power_temperature_offset
except AttributeError:
warn_missing_config_setting("battery_power_temperature_offset")
config.battery_power_temperature_offset = DEFAULT_BATTERY_POWER_TEMPERATURE_OFFSET


def warn_missing_config_setting(setting):
Expand Down
6 changes: 6 additions & 0 deletions enviro/config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@
moisture_target_c = 50

# compensate for usb power
# degrees to remove from measured temperature when running on USB
usb_power_temperature_offset = 4.5

# compensate for battery power (only on weather boards)
# degrees to remove from measured temperature when running on battery
# this will vary based on poll and wifi upload frequency with accumulated heat as these values increase
battery_power_temperature_offset = 0.0