-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_station_BYO.py
99 lines (81 loc) · 2.78 KB
/
weather_station_BYO.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from gpiozero import Button
import time
import math
import bme280_sensor
import wind_direction_byo
import statistics
import ds18b20_therm
import database
wind_count = 0 # Counts how many half-rotations
radius_cm = 9.0 # Radius of your anemometer
wind_interval = 5 # How often (secs) to sample speed
interval = 5 # measurements recorded every 5 minutes
CM_IN_A_KM = 100000.0
SECS_IN_AN_HOUR = 3600
ADJUSTMENT = 1.17892550438441
BUCKET_SIZE = 0.2794
rain_count = 0
gust = 0
store_speeds = []
store_directions = []
# Every half-rotation, add 1 to count
def spin():
global wind_count
wind_count = wind_count + 1
#print( wind_count )
def calculate_speed(time_sec):
global wind_count
global gust
circumference_cm = (2 * math.pi) * radius_cm
rotations = wind_count / 2.0
# Calculate distance travelled by a cup in km
dist_km = (circumference_cm * rotations) / CM_IN_A_KM
# Speed = distance / time
km_per_sec = dist_km / time_sec
km_per_hour = km_per_sec * SECS_IN_AN_HOUR
# Calculate speed
final_speed = km_per_hour * ADJUSTMENT
return final_speed
def bucket_tipped():
global rain_count
rain_count = rain_count + 1
#print (rain_count * BUCKET_SIZE)
def reset_rainfall():
global rain_count
rain_count = 0
def reset_wind():
global wind_count
wind_count = 0
def reset_gust():
global gust
gust = 0
print('begin')
wind_speed_sensor = Button(5)
wind_speed_sensor.when_activated = spin
temp_probe = ds18b20_therm.DS18B20()
rain_sensor = Button(6)
rain_sensor.when_pressed = bucket_tipped
db = database.weather_database()
while True:
start_time = time.time()
while time.time() - start_time <= interval:
wind_start_time = time.time()
reset_wind()
#time.sleep(wind_interval)
while time.time() - wind_start_time <= wind_interval:
store_directions.append(wind_direction_byo.get_value())
final_speed = calculate_speed(wind_interval)
store_speeds.append(final_speed)
wind_average = wind_direction_byo.get_average(store_directions)
wind_gust = max(store_speeds)
wind_speed = statistics.mean(store_speeds)
rainfall = rain_count * BUCKET_SIZE
reset_rainfall()
store_speeds = []
#print(store_directions)
store_directions = []
ground_temp = temp_probe.read_temp()
#ground_temp = 0
humidity, pressure, ambient_temp = bme280_sensor.read_all()
print('Wind Dir:',round(wind_average,1), 'Wind Speed:',round(wind_speed,1), 'Wind Gust:',round(wind_gust,1), 'Rainfall:',round(rainfall,1),'Humidity:',round(humidity,1),'Pressure:', round(pressure,1), 'Ambient Temp:',round(ambient_temp,1),'Ground Temp:', round(ground_temp,1))
db.insert(ambient_temp, ground_temp, 0, pressure, humidity, wind_average, wind_speed, wind_gust, rainfall)