from gpiozero import Button import time import math import statistics anem = Button(5) # setup GPIO 5 as a button storeSpeeds = [] windCount = 0 # counts half-rotations radius_cm = 9.0 # radius of anemometer circumference_cm = (2 * math.pi) * radius_cm windInterval = 5 # how often to report speed cmInKm = 100000.0 secsInHour = 3600 anemFactor = 1.18 # anemometer factor is the result of wind energy lost due to the arms turning # every half rotation, add one to count def spin(): global windCount windCount += 1 # calculate wind speed def calculateSpeed(timeSec): global windCount global circumference_cm rotations = windCount / 2.0 # calculate distance travelled by anemometer in km distKm = circumference_cm * rotations / cmInKm speed = ((distKm / timeSec) * secsInHour) * anemFactor return speed def resetWind(): global wind_count wind_count = 0 anem.when_pressed = spin # Loop to measure wind speed and report at 5 second intervals while True: startTime = time.time() while time.time() - startTime <= windInterval: resetWind() # reset count for next 5 second run time.sleep(windInterval) #finalSpeed = calculateSpeed(windInterval) storeSpeeds.append(calculate(windInterval)) # store average speed over 5 seconds windGust = max(storeSpeeds) # maximum gust windSpeed = statistics.mean(storeSpeeds) # running average wind speed print(windSpeed, "km/h", windGust, "km/h")