From 594b61e3e34a6e746d2b4c0d859269e17181b7a4 Mon Sep 17 00:00:00 2001 From: Stephen Jefferson Date: Thu, 23 Mar 2023 21:53:03 +0000 Subject: [PATCH 1/6] Add wind dir improvements and logging --- enviro/boards/weather.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index 9b44e13..e2a0b99 100644 --- a/enviro/boards/weather.py +++ b/enviro/boards/weather.py @@ -127,11 +127,12 @@ def wind_speed(sample_time_ms=1000): def wind_direction(): # adc reading voltage to cardinal direction taken from our python - # library - each array index represents a 45 degree step around - # the compass (index 0 == 0, 1 == 45, 2 == 90, etc.) + # library - each array index represents a 22.5 degree step around + # the compass (index 0 == 0, 1 == 22.5, 2 == 45, etc.) # we find the closest matching value in the array and use the index # to determine the heading - ADC_TO_DEGREES = (0.9, 2.0, 3.0, 2.8, 2.5, 1.5, 0.3, 0.6) + ADC_TO_DEGREES = (2.533, 1.308, 1.487, 0.270, 0.300, 0.212, 0.595, 0.408, + 0.926, 0.789, 2.031, 1.932, 3.046, 2.667, 2.859, 2.265) closest_index = -1 last_index = None @@ -139,24 +140,32 @@ def wind_direction(): # ensure we have two readings that match in a row as otherwise if # you read during transition between two values it can glitch # fixes https://github.com/pimoroni/enviro/issues/20 + voltage = 0.0 + loop = 0 + debug = [] while True: value = wind_direction_pin.read_voltage() closest_index = -1 closest_value = float('inf') - for i in range(8): + for i in range(16): distance = abs(ADC_TO_DEGREES[i] - value) if distance < closest_value: closest_value = distance closest_index = i + resistance = (voltage * 10000) / (3.3 - voltage) + logging.info(f"> wind direction stats - voltage: {value}, resistance: {resistance}, closest value: {closest_value}, loops: {loop}") + + # if True: #Test not waitng for two identical readings in a row if last_index == closest_index: break last_index = closest_index + loop += 1 - return closest_index * 45 + return closest_index * 22.5 def rainfall(seconds_since_last): amount = 0 From 513433a4d0d69ac2819e46b2da345376ec496748 Mon Sep 17 00:00:00 2001 From: Stephen Jefferson Date: Fri, 24 Mar 2023 18:21:35 +0000 Subject: [PATCH 2/6] Remove wind dir loop, only one reading --- enviro/boards/weather.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index e2a0b99..0afc996 100644 --- a/enviro/boards/weather.py +++ b/enviro/boards/weather.py @@ -156,10 +156,10 @@ def wind_direction(): closest_index = i resistance = (voltage * 10000) / (3.3 - voltage) - logging.info(f"> wind direction stats - voltage: {value}, resistance: {resistance}, closest value: {closest_value}, loops: {loop}") + logging.info(f"> wind direction stats - voltage: {value}, resistance: {resistance}, closest value: {closest_value}, closest index: {closest_index}, loops: {loop}") - # if True: #Test not waitng for two identical readings in a row - if last_index == closest_index: + if True: #Test not waitng for two identical readings in a row + # if last_index == closest_index: break last_index = closest_index From 3ee0f572f84b63f67c688e408a0ec6934a9bd26b Mon Sep 17 00:00:00 2001 From: Stephen Jefferson Date: Fri, 24 Mar 2023 19:23:08 +0000 Subject: [PATCH 3/6] Add wind dir offset option --- enviro/boards/weather.py | 8 ++++++-- enviro/config_defaults.py | 6 ++++++ enviro/config_template.py | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index 0afc996..885eb17 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 @@ -165,7 +165,11 @@ def wind_direction(): last_index = closest_index loop += 1 - return closest_index * 22.5 + wind_direction = closest_index * 22.5 + + offset_wind_direction = (wind_direction + 360 + config.wind_direction_offset) % 360 + + return offset_wind_direction def rainfall(seconds_since_last): amount = 0 diff --git a/enviro/config_defaults.py b/enviro/config_defaults.py index 63a5877..f09cc87 100644 --- a/enviro/config_defaults.py +++ b/enviro/config_defaults.py @@ -18,6 +18,12 @@ def add_missing_config_settings(): warn_missing_config_setting("usb_power_temperature_offset") config.usb_power_temperature_offset = DEFAULT_USB_POWER_TEMPERATURE_OFFSET + try: + config.wind_direction_offset + except AttributeError: + warn_missing_config_setting("wind_direction_offset") + config.wind_direction_offset = DEFAULT_USB_POWER_TEMPERATURE_OFFSET + try: config.wifi_country except AttributeError: diff --git a/enviro/config_template.py b/enviro/config_template.py index 11404a9..6058b69 100644 --- a/enviro/config_template.py +++ b/enviro/config_template.py @@ -55,3 +55,6 @@ # compensate for usb power usb_power_temperature_offset = 4.5 + +# offset up to +/- 360 degrees for wind direction if you can't reorientate the weather station +wind_direction_offset = 0 \ No newline at end of file From 9d48100ab04ce0fd43f04b6f49eecdce75d13331 Mon Sep 17 00:00:00 2001 From: Stephen Jefferson Date: Sat, 25 Mar 2023 11:50:20 +0000 Subject: [PATCH 4/6] Fully removed loop logic --- enviro/boards/weather.py | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index 885eb17..21626d3 100644 --- a/enviro/boards/weather.py +++ b/enviro/boards/weather.py @@ -141,29 +141,20 @@ def wind_direction(): # you read during transition between two values it can glitch # fixes https://github.com/pimoroni/enviro/issues/20 voltage = 0.0 - loop = 0 - debug = [] - while True: - value = wind_direction_pin.read_voltage() - - closest_index = -1 - closest_value = float('inf') - - for i in range(16): - distance = abs(ADC_TO_DEGREES[i] - value) - if distance < closest_value: - closest_value = distance - closest_index = i + + value = wind_direction_pin.read_voltage() - resistance = (voltage * 10000) / (3.3 - voltage) - logging.info(f"> wind direction stats - voltage: {value}, resistance: {resistance}, closest value: {closest_value}, closest index: {closest_index}, loops: {loop}") + closest_index = -1 + closest_value = float('inf') - if True: #Test not waitng for two identical readings in a row - # if last_index == closest_index: - break + for i in range(16): + distance = abs(ADC_TO_DEGREES[i] - value) + if distance < closest_value: + closest_value = distance + closest_index = i - last_index = closest_index - loop += 1 + resistance = (voltage * 10000) / (3.3 - voltage) + logging.info(f"> wind direction stats - voltage: {value}, resistance: {resistance}, closest value: {closest_value}, closest index: {closest_index}") wind_direction = closest_index * 22.5 From e5f2613108198d350c2f2f9965beb0d489e96322 Mon Sep 17 00:00:00 2001 From: Stephen Jefferson Date: Fri, 26 Apr 2024 18:10:39 +0100 Subject: [PATCH 5/6] Incorrect default wind direction value --- enviro/config_defaults.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/enviro/config_defaults.py b/enviro/config_defaults.py index f09cc87..edde40b 100644 --- a/enviro/config_defaults.py +++ b/enviro/config_defaults.py @@ -2,7 +2,7 @@ from phew import logging DEFAULT_USB_POWER_TEMPERATURE_OFFSET = 4.5 - +DEFAULT_WIND_DIRECTION_OFFSET = 0 def add_missing_config_settings(): try: @@ -17,18 +17,18 @@ 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.wind_direction_offset - except AttributeError: - warn_missing_config_setting("wind_direction_offset") - config.wind_direction_offset = DEFAULT_USB_POWER_TEMPERATURE_OFFSET - + try: config.wifi_country except AttributeError: warn_missing_config_setting("wifi_country") config.wifi_country = "GB" + try: + config.wind_direction_offset + except AttributeError: + warn_missing_config_setting("wind_direction_offset") + config.wind_direction_offset = DEFAULT_WIND_DIRECTION_OFFSET + def warn_missing_config_setting(setting): logging.warn(f"> config setting '{setting}' missing, please add it to config.py") From c83f2cdbe8565976b1a206ba80ebfaa1c99a9b5a Mon Sep 17 00:00:00 2001 From: Stephen Jefferson Date: Fri, 26 Apr 2024 18:58:53 +0100 Subject: [PATCH 6/6] Remove comments from previously removed code. --- enviro/boards/weather.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index 21626d3..6c285d5 100644 --- a/enviro/boards/weather.py +++ b/enviro/boards/weather.py @@ -137,9 +137,6 @@ def wind_direction(): closest_index = -1 last_index = None - # ensure we have two readings that match in a row as otherwise if - # you read during transition between two values it can glitch - # fixes https://github.com/pimoroni/enviro/issues/20 voltage = 0.0 value = wind_direction_pin.read_voltage()