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

Read all 16 possible wind direction values #163

Open
wants to merge 6 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
41 changes: 21 additions & 20 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 @@ -127,36 +127,37 @@ 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

# 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()
voltage = 0.0

value = wind_direction_pin.read_voltage()

closest_index = -1
closest_value = float('inf')
closest_index = -1
closest_value = float('inf')

for i in range(8):
distance = abs(ADC_TO_DEGREES[i] - value)
if distance < closest_value:
closest_value = distance
closest_index = i
for i in range(16):
distance = abs(ADC_TO_DEGREES[i] - value)
if distance < closest_value:
closest_value = distance
closest_index = i

if last_index == closest_index:
break
resistance = (voltage * 10000) / (3.3 - voltage)
logging.info(f"> wind direction stats - voltage: {value}, resistance: {resistance}, closest value: {closest_value}, closest index: {closest_index}")

last_index = closest_index
wind_direction = closest_index * 22.5

return closest_index * 45
offset_wind_direction = (wind_direction + 360 + config.wind_direction_offset) % 360

return offset_wind_direction

def rainfall(seconds_since_last):
amount = 0
Expand Down
10 changes: 8 additions & 2 deletions enviro/config_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -17,12 +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.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")
3 changes: 3 additions & 0 deletions enviro/config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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