Emerson Corder
Emerson Corder

Reputation: 21

Need to get mean and max values with this script

I'm having a few problems. First, given wind speeds seem to be low, especially compared to other weather stations reporting close by (apparently there are lots of enthusiasts in my area). Just watching the trees, flag, bushes, small animals flying across my yard, I can tell 1.6mph is a wee bit low. Everything tested fine inside, and when I run the test script outside its picking up the signal as it spins.

the 2nd problem is it always reports the "mean" and "max" speeds as exactly the same. I've tried to adjust the intervals, but no matter what length of time I put in, they always report as the same numbers.

from gpiozero import Button
import requests
import time
import math
import statistics
import database

wind_count = 0
radius_cm = 9.0
wind_interval = 5
ADJUSTMENT = 1.18
interval = 60
gust = 0

def spin():
    global wind_count
    wind_count = wind_count + 1

def calculate_speed(time_sec):
    global wind_count
    global gust
    circumference_cm = (2 * math.pi) * radius_cm
    rotations = wind_count / 2.0
    dist_cm = circumference_cm * rotations

    dist_km = (circumference_cm * rotations) / 100000

    dist_mi = dist_km * 0.621371
    mi_per_sec = dist_mi / time_sec
    mi_per_hour = mi_per_sec * 3600

    return mi_per_hour * ADJUSTMENT

def reset_wind():
    global wind_count
    wind_count = 0

def reset_gust():
    global gust
    gust = 0

wind_speed_sensor = Button(16)
wind_speed_sensor.when_activated = spin

while True:
    print("Starting Weather Sensor Read Loop...")
    start_time = time.time()
    while time.time() - start_time <= interval:
        print("Start timed loop...")
        wind_start_time = time.time()
        reset_wind()
        reset_gust()
        store_speeds = []
        time.sleep(wind_interval)
    final_speed = calculate_speed(wind_interval)
    store_speeds.append(final_speed)
    wind_gust_speed = (max(store_speeds))
    wind_speed = (statistics.mean(store_speeds))
    print(wind_average, wind_speed)

When I comment out "store_speeds = [ ]" the first loop the speeds are reported the same, every loop after I get a "max" reading thats "different" than the mean. This still troubles me, because why on the first loop are they the same? Am I wrong for thinking with the wind_interval set at 5, and the interval set to 60, that its taking 5 second samples over a 60 second period, giving me 12 sample to get the mean and max from?

My goal is every time it reports, I get a mean and max for that "loop" if possible, and not a mean/max over the time the script stays running before its interrupted/stopped.

Upvotes: 0

Views: 64

Answers (1)

Emerson Corder
Emerson Corder

Reputation: 21

Here is the working and corrected code:

from gpiozero import Button
import time
import math
import statistics

wind_count = 0
radius_cm = 9.0
wind_interval = 5
interval = 30
CM_IN_A_KM = 100000.0
SECS_IN_AN_HOUR = 3600
ADJUSTMENT = 1.18
store_speeds = []

def reset_wind():
    global wind_count
    wind_count = 0

def spin():
    global wind_count
    wind_count = wind_count + 1

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) / CM_IN_A_KM

    km_per_sec = dist_km / time_sec
    km_per_hour = km_per_sec * SECS_IN_AN_HOUR
    mi_per_hour = km_per_hour * 0.6214


    return mi_per_hour * ADJUSTMENT


wind_speed_sensor = Button(16)
wind_speed_sensor.when_pressed = spin

while True:
    store_speeds = []
    for _ in range (interval//wind_interval):
        reset_wind()
        #reset_gust()
        time.sleep(wind_interval)  # counter is spinning in background
        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)


Upvotes: 2

Related Questions