-
Notifications
You must be signed in to change notification settings - Fork 0
/
wind.py
55 lines (39 loc) · 1.27 KB
/
wind.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from gpiozero import Button
import time
import math
import statistics
wind_count = 0 # Counts how many half-rotations
radius_cm = 9.0 # Radius of your anemometer
wind_interval = 5 # How often (secs) to report speed
ANEMOMETER_FACTOR = 1.18
store_speeds = []
# Every half-rotation, add 1 to count
def spin():
global wind_count
wind_count = wind_count + 1
# print("spin" + str(wind_count))
def reset_wind():
global wind_count
wind_count = 0
# Calculate the wind speed
def calculate_speed(time_sec):
global wind_count
circumference_cm = (2 * math.pi) * radius_cm
rotations = wind_count / 2.0
dist_km = (circumference_cm * rotations) / 100000
km_per_sec = dist_km / time_sec
km_per_hour = km_per_sec * 3600
return km_per_hour * ANEMOMETER_FACTOR
wind_speed_sensor = Button(5)
wind_speed_sensor.when_pressed = spin
# Loop to measure wind speed and report at 5-second intervals
while True:
start_time = time.time()
while time.time() - start_time <= wind_interval:
reset_wind()
time.sleep(wind_interval)
final_speed = calculate_speed(wind_interval)
store_speeds.append(final_speed)
wind_gust = max(store_speeds)
wind_speed = statistics.mean(store_speeds)
print(wind_speed, wind_gust)