Hi There
This is one that I made. You can modify it or use any part of it as you wish.
I used it with a PiTFT screen and a battery so that I can take it outside anywhere.
Here is the code. You may need to adjust the tabs and indents to get it working.
cheers
# This programme is written for Raspberry Pi model B+ using Raspbian and the maplin anemometer #N24FR.
# It should work on other Pi's as well however I have only tested it on the older B+ and the Raspberry #PI 2.
# The anemometer has a reed switch and a magnet that switches twice per revolution.
# The RJ11 was removed from the cable and one of the wires is connected to 3.3v pin on the Pi and #the other
# wire connected to PIN 3.
# a 470 ohm resistor is connected between pin3 and GND on the PI
# to act as a pull down resistor to prevent stray voltages.
# (This value resistor just happened to be what I had, it snot critical).
# This appears to achieve good results however I have not tested it against any calibrated #anemometers
# so it can only be used as a rough guide
# During measurements a file will be written on the Desktop "windstats.txt" in which all the readings #will be written
# If the file does not exist it will be created.
# If the file exists the new data will be appended rather than overwritten.
# Please note that this programme only works properly from the terminal screen, not IDLE
# and the card needs to be write enabled
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN)
import datetime
import time
from time import sleep
#setup variables for triggers and units
trigger=0 #this is to stop multiple detection if the anemometer stops in the closed position
pulse=0 # this is the count that we want to know how many actual genuine pulses per revolution
pulsepersecond=0
meterspersec=0
kmh=0
mph=0
beaufort=0
knots=0
#a 1 second switch check loop
print "\033c" #this clears the screen
while True:
endTime = datetime.datetime.now() + datetime.timedelta(seconds=1) #this is the end of the 1 #second loop in seconds
while datetime.datetime.now() <= endTime:
if (GPIO.input(3) ==1):
if trigger == 0 and (GPIO.input(3) ==1):# this stops the switch being read more than once
pulse +=1
trigger=1
else:
trigger=0
pulsepersecond = pulse/2 # divided by 2 because the read switch open and closes twice per revolution
meterspersecond=2*3.142*0.09*pulsepersecond #this is 2 pi r, radius of anemometer arm is 90mm
kmh=meterspersecond*3.6
mph=kmh/1.609344
beaufort=(mph+6)/6
knots=mph*0.86897624190816
pulse=0
if meterspersecond >0: # this just stops the screen printing zeros all the time
print "\033c"
print ("\033[1;37;40m") # bright white colour
print datetime.datetime.now()
print ("\033[1;31;40m"),"____________________________________" #escape code is just to #print red
print ("\033[1;32;40m")," meters per second is ",round(meterspersecond,2),"m/sec"
print ("\033[1;36;40m")," Kilometers per Hour is ",round(kmh,2),"Kph"
print ("\033[1;37;40m")," Miles per Hour is ",round(mph,2),"mph"
print ("\033[1;35;40m")," Beaufort Scale reads ",round(beaufort,1)-0.5 #this seems about as #accurate as I can get with this
print ("\033[1;34;40m")," Knots are ",round(knots,2),"Knots"
print ("\033[1;31;40m"),"____________________________________"
todaysdatetime = datetime.datetime.now()
generatefile=open( '/home/pi/Desktop/windstats.txt', 'a+' ) #a+ means append rather than just #write
generatefile.write(repr(todaysdatetime) +'\n')
generatefile.write( 'meters persecond ' + repr(meterspersecond) + '\n')
generatefile.write( 'Kilometers per hour ' + repr(kmh) + '\n')
generatefile.write( 'Miles per Hour ' + repr(mph) + '\n')
generatefile.write( 'Beaufort Scale ' + repr(beaufort) + '\n')
generatefile.write( 'Knots ' + repr(knots) + '\n')
generatefile.write( "\n")
generatefile.close()
else:
print "\033c" #this clears the screen
print ("\033[1;37;40m") # bright white colour
print datetime.datetime.now()
print ("\033[1;32;40m")
print "Waiting for the wind to blow !!"
sleep (0.2)
Hope this helps