summaryrefslogtreecommitdiff
path: root/dev_scripts/sen15901anem.py
blob: 6294ea0c1bcf216d3fc69ae16acdd25fcdb2d393 (plain)
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
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")