Hello again,

During the past few days I have been trying to write code so to use a 
Maplin Anemometer, which uses a magentic switch to send pulses as it 
rotates.

Which is connected to my GrovePi.

I have been trying to adapt ppm_counter.py that was posted earlier in this 
group.  The code uses GPIO.inputs, and the Grovepi uses 
grovepi.digitalRead(2) to read the sensor.

I have been trying to adapt the code so it will work with my GrovePi D2 
input, and so far I have not been able to get a wind speed reading.

I have included both ppm_counter.py, and my attempt, in the hope someone 
make some suggestions

Michael

-- 
You received this message because you are subscribed to the Google Groups 
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to weewx-user+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
#!/usr/bin/python

#  ppm_counter.py
#
#  This was written to use the Raspberry Pi as a weather station. Using
#  Argent Data systems wind instruments, we connect on one of the Pi's
#  GPIO inputs and then loop for a set time (sample) and watch the GPIO
#  pin for pulses (counter).  We then take the number of pulses and average them,
#  then we multiply them by the speed correction factor (cfactor) to find
#  the speed in MPH. 
#
#  This is a 5 second average as originally written.  This could be then
#  Put into an array and then a 2 minute average could be calculated for 
#  display as a sustained speed, and the highest reading in a 5 minute set
#  could be used as a gust, with a high speed greater than 10 knots above the
#  lowest lull in a 10 minute period as the Peak Wind.
#  
#  Copyright 2013 cmcdonald <c...@hamshadow.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  
from time import sleep
import time 
import RPi.GPIO as GPIO

DEBUG = 0
counter = 0
finishtime = 0
# This is the correction factor for the Argent Data anemeometor
# one RPS = 1.492 MPH of wind.  RPS not RPM
# if using another instrument, change cfactore to manufactures setting 
cfactor = 1.492
sample = 5
speed = 0
pinin = 17  # This is the GPIO pin we will listen on
state = False

# Setup which GPIO Pin as pin to watch for counter
GPIO.setmode(GPIO.BCM)
GPIO.setup(pinin, GPIO.IN)

def ppm_counter():
#    Loop through for a few seconds (sample) and watch for 
# 	pulses.  (sample) is the time in seconds, then average it by
# 	(sample) for an average of pulses per second, then multiply by
# 	correction factor (cfactor) for speed in MPH
	counter = 0
	# finishtime is right now (clock time) + 5 real seconds, not 
	# CPU seconds
	finishtime = (int(time.time()) + sample)
	#We are going to assume the switch is open, so state = false
	state = False
	while (int(time.time()) < finishtime):
		if DEBUG > 0:
			print (int(time.time()))
		# Check to see if the pin goes Low (switch closed)
		if ( GPIO.input(pinin) == True ):
			# State was false, now set that the switch closed and 
			# watch for it to open
			state = True
		# We see that the switch closed, now wait until it opens
		if ((state == True) and (GPIO.input(pinin) == False)):
			# Reset State to open
			state = False
			#  Increment the counter
			counter = counter + 1
	if DEBUG > 0:
		print counter
#	counter is the total number of pulses during the sample time
#	We need to then calculate the speed at pps*cfactor=speed in MPH
	speed = ((counter / sample)  * cfactor)
	if DEBUG > 0:
		print speed
	return (speed)
		
		
def main():
    while True:        
	    speed = ppm_counter()
	    print speed
    return 0

if __name__ == '__main__':
	main()
	
#! /usr/bin/env python
#    $Revision: 1 $
#    $Edited: WhimickRH $
#    $Date: 2016-08-01 $
#    $Anemometer & Wind Direction

import grovepi
import RPi.GPIO as GPIO
from grovepi import *
import math
import time
from time import sleep
from time import strftime, localtime


# ------------------Global variables----------------------
DEBUG = 0
counter = 0
finishtime = 0
# This is the correction factor for the Argent Data anemeometor
# one RPS = 1.492 MPH of wind.  RPS not RPM
# if using another instrument, change cfactore to manufactures setting
cfactor = 1.492
sample = 5
speed = 0
pulse = 0
state = 0
wind_speed = 0
winddir = 0

# ------------------CONFIGURATION--------------------------

rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
    bus = smbus.SMBus(1)
else:
    bus = smbus.SMBus(0)

# Connect the Maplin Anemometer to digital port D3
# SIG,NC,VCC,GND
grovepi.pinMode(2,"INPUT")

led = 4
digitalWrite(led, 0)

# ------------------ANEMOMETER SENSOR-------------------------
def ppm_counter():
    global pulse, state, speed, counter, finishtime
    #    Loop through for a few seconds (sample) and watch for
    # 	pulses.  (sample) is the time in seconds, then average it by
    # 	(sample) for an average of pulses per second, then multiply by
    # 	correction factor (cfactor) for speed in MPH
    counter = 0
    # finishtime is right now (clock time) + 5 real seconds, not
    # CPU seconds
    finishtime = (int(time.time()) + sample)
    # We are going to assume the switch is open, so pulse = 0
    state = 0
    while (int(time.time()) < finishtime):
        if DEBUG > 1:
            print (int(time.time()))
        # Check to see if the pin goes Low (switch closed)
        pulse = grovepi.digitalRead(2)
        if pulse == 1:
            # pulse was 0, now set that the switch closed and
            # watch for it to open
            digitalWrite(led, 1)
            print 'led on'
            state = 1
        # We see that the switch closed, now wait until it opens
        pulse = grovepi.digitalRead(2)
        if (state == 1) and (pulse == 0):
            # Reset pulse to open
            digitalWrite(led, 0)
            state = 0
            #  Increment the counter
            counter = counter + 1
            print 'led off'
    if DEBUG == 0:
        print counter
    # counter is the total number of pulses during the sample time
    # We need to then calculate the speed at pps*cfactor=speed in MPH
    speed = ((counter / sample) * cfactor)
    if DEBUG == 0:
        print speed
    return()

def get_speed():
    while True:
        speed = ppm_counter()
        print speed
    return()

# -------------------Main Program----------------------
while True:
    try:
        # turn led status light on.

        # digitalWrite(led, 1)

        # Get time
        start_time = time.time()
        datetime = strftime("%Y-%m-%d %H:%M:00", localtime())

        # Get Wind Speed
        # w_pulse = get_w_pulse()
        # Get wind speed and three second peak. (WMO standard)
        windSpeed = get_speed()
        print windSpeed
        if windSpeed == 0:  # Wind speed is zero
            pass
            #winddir = winddir  # set to None to have 0 dir

        # Get End Times and calculate program pause to next collection
        end_time = time.time()
        proc_time = round(end_time - start_time, 4)
        if (proc_time > 1) or (math.ceil(end_time) > math.ceil(start_time)):
            sleep_time = 0
        else:
            sleep_time = math.ceil(end_time) - end_time

    except IOError as e:
        # digitalWrite(led, 0)
        sleep(1.0)
        continue
    else:
        pass

Reply via email to