from gpiozero import Button import time import math wind_count = 0 # counts how many half-rotations radius_cm = 9.0 # radius in cm of the anemometer wind_interval = 5 # how often (secs) to report speed ADJUSTMENT = 1.18 # adjustment of the anemometer given by the manufacturer # every half-rotation, add 1 count def spin(): global wind_count wind_count = wind_count + 1 # print("spin" + str(wind_count)) # 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.0 # divided by cm in a km km_per_sec = dist_km / time_sec km_per_hour = km_per_sec * 3600 # multiplied by secs in an hour return km_per_hour * ADJUSTMENT def reset_wind(): global wind_count wind_count = 0 # recommended function to reset the wind_count to zero when assembling the weather station wind_speed_sensor = Button(5) wind_speed_sensor.when_pressed = spin # loop to measure wind speed and report at 5 secs interval while True: wind_count = 0 time.sleep(wind_interval) print( calculate_speed(wind_interval), "km/h")