-
Notifications
You must be signed in to change notification settings - Fork 0
/
wind.py
55 lines (46 loc) · 2.11 KB
/
wind.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
import RPi.GPIO as GPIO
import time
import datetime
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN,pull_up_down=GPIO.PUD_UP)
def wind(cycles):
start = time.time()
for pulses in range(cycles):
GPIO.wait_for_edge(24, GPIO.RISING) #Rising or falling edge give different results!
# print (pulses) # show the count
duration = time.time() - start # how long it took to count n cycles
frequency = cycles / duration # in Hz (cycles per second)
wind_speed = 1.492 * frequency # multiplier is from the manual
return wind_speed
#print ('Duration is {} seconds for 10 pulses. Frequency is {} hz'.format(duration, frequency))
recent_speeds = []
while True:
# calculate max and average of day's readings. Save data to log file.
wind_speed = (wind(10))
recent_speeds.append(wind_speed) # save readings in a list
# print (recent_speeds)
raw_avg = (sum(recent_speeds)/len(recent_speeds))
avg = format (raw_avg,'.2f')
raw_fastest = max(recent_speeds)
fastest = format(raw_fastest,'.2f')
#print('Avg speed {:.2f}'.format (sum(recent_speeds)/len(recent_speeds))) # average speed
#print ('max {:.2f}'.format (max(recent_speeds))) # fastest reading
#print ('-' * 12)
# keep the list from getting too large, or covering too much time for an average to make sense
# this code deletes the oldest reading each time so the average is the average of the last n readings
# not the whole day.
if len(recent_speeds) > 100:
del recent_speeds[0]
#time.sleep (60) #once a minute should be enough
#print ("{} | {:.2f}".format (datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S"), wind_speed))
try:
log = open('/var/www/html/wind.log', 'a') #was windlog.txt
log.write("{} {:.2f} {} {} \n".format (datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S"), wind_speed, avg, fastest))
log.close()
except KeyboardInterrupt:
log.close()
exit('Thats all, folks')
'''
how is wind speed measured
http://www.wral.com/weather/blogpost/1283652/
'''