From 99326ded7d19b59574fb0027711a668392cf3e6d Mon Sep 17 00:00:00 2001 From: MrDrem <4300216+MrDrem@users.noreply.github.com> Date: Wed, 24 Apr 2024 12:02:08 +0100 Subject: [PATCH 1/4] Update weather.py to show 16 wind directions This also resolves the issue with having to tare readings more than once. --- enviro/boards/weather.py | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index 9b44e13..3125c1e 100644 --- a/enviro/boards/weather.py +++ b/enviro/boards/weather.py @@ -127,36 +127,27 @@ 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 + closest_value = float('inf') - # 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 - while True: - value = wind_direction_pin.read_voltage() + 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 - for i in range(8): - distance = abs(ADC_TO_DEGREES[i] - value) - if distance < closest_value: - closest_value = distance - closest_index = i - - if last_index == closest_index: - break - - last_index = closest_index - - return closest_index * 45 + voltage = value + resistance = (voltage * 10000) / (3.3 - voltage) + return closest_index * 22.5 def rainfall(seconds_since_last): amount = 0 From 9f57de2896fcf1e9607d9890523f45960b277699 Mon Sep 17 00:00:00 2001 From: MrDrem <4300216+MrDrem@users.noreply.github.com> Date: Thu, 25 Apr 2024 23:21:56 +0100 Subject: [PATCH 2/4] Update weather.py Taken the earlier code from @Gadgetoid and cleaned it up a little, as it's better than mine was. I'm also going to update the config.py file to add in the required config.wind_direction_offset which is a nice touch --- enviro/boards/weather.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index 3125c1e..cda030c 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 @@ -135,19 +135,28 @@ def wind_direction(): 0.926, 0.789, 2.031, 1.932, 3.046, 2.667, 2.859, 2.265) closest_index = -1 - closest_value = float('inf') - + last_index = None + voltage = 0.0 + 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 - - voltage = value + resistance = (voltage * 10000) / (3.3 - voltage) - return closest_index * 22.5 + logging.info(f"> wind direction stats - voltage: {value}, resistance: {resistance}, closest value: {closest_value}, closest index: {closest_index}") + + 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 From 4488fb43e444eb468db5ef749a62e2b10b6bd743 Mon Sep 17 00:00:00 2001 From: MrDrem <4300216+MrDrem@users.noreply.github.com> Date: Thu, 25 Apr 2024 23:29:26 +0100 Subject: [PATCH 3/4] Update config_defaults.py I think that I have updated this correctly so that the wind direction offset will be set to 0 if not defined --- enviro/config_defaults.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/enviro/config_defaults.py b/enviro/config_defaults.py index 63a5877..6d67af8 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: @@ -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_WIND_DIRECTION_OFFSET + try: config.wifi_country except AttributeError: From 2bcb2e5eb65b1f77d41725648d2fb3a37ae5ce29 Mon Sep 17 00:00:00 2001 From: MrDrem <4300216+MrDrem@users.noreply.github.com> Date: Thu, 25 Apr 2024 23:30:57 +0100 Subject: [PATCH 4/4] Update config_template.py updated to add in weather specific area for the wind_direction_offset --- enviro/config_template.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/enviro/config_template.py b/enviro/config_template.py index 11404a9..8f84679 100644 --- a/enviro/config_template.py +++ b/enviro/config_template.py @@ -53,5 +53,8 @@ moisture_target_b = 50 moisture_target_c = 50 +# weather specific settings +wind_direction_offset = 0 + # compensate for usb power usb_power_temperature_offset = 4.5