troysie
Posts: 24
Joined: Mon Feb 13, 2017 8:33 am

Python code for flow meter

Wed Dec 06, 2017 3:17 pm

I am using the following python code for a flow meter.
[codeimport RPi.GPIO as GPIO
import time, sys
GPIO.setmode(GPIO.BOARD)
inpt = 11
GPIO.setup(inpt,GPIO.IN)
rate_cnt = 0
tot_cnt = 0
minutes = 0
constant = 0.10
time_new = 0.0

print('Water Flow - Approximate')
print('Control c to exit')

while True:
time_new = time.time() + 60
rate_cnt = 0
while time.time() <= time_new:
if GPIO.input(inpt)!= 0:
rate_cnt += 1
tot_cnt += 1
try:
print(GPIO.input(inpt), end='')
except KeyboardInterrupt:
print('\nCTRL C - Exiting nicely')
GPIO.cleanup()
sys.exit()
minutes += 1
print('\nLiters / min',round(rate_cnt * constant,4))
print('Total liters', round(tot_cnt * constant,4))
print('Time (min & clock) ', minutes, '\t', time.asctime(time.localtime()))

GPIO.cleanup()
print('Done')][/code]

The problem is it keeps running when the flow stops. When the flow stops it stops on either 0 or 1. If it stops on zero it stops recording, but if it stops on 1 it keeps recording. Can I alter the program to stop when the flow stops and start when the flow starts
I am a beginner at python and finding it quite difficult. Any help would be much appreciated
Regards Troysie

User avatar
scruss
Posts: 1398
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Python code for flow meter

Wed Dec 06, 2017 3:49 pm

I think your paste went a bit wrong:

Code: Select all

import RPi.GPIO as GPIO
import time, sys
GPIO.setmode(GPIO.BOARD)
inpt = 11
GPIO.setup(inpt,GPIO.IN)
rate_cnt = 0
tot_cnt = 0
minutes = 0
constant = 0.10
time_new = 0.0

print('Water Flow - Approximate')
print('Control c to exit')

while True:
    time_new = time.time() + 60
    rate_cnt = 0
    while time.time() &lt;= time_new:
        if GPIO.input(inpt)!= 0:
            rate_cnt += 1
            tot_cnt += 1
        try:
           print(GPIO.input(inpt), end='')
        except KeyboardInterrupt:
           print('\nCTRL C - Exiting nicely')
           GPIO.cleanup()
           sys.exit()
    minutes += 1
    print('\nLiters / min',round(rate_cnt * constant,4))
    print('Total liters', round(tot_cnt * constant,4))
    print('Time (min & clock) ', minutes, '\t', time.asctime(time.localtime()))

GPIO.cleanup()
print('Done')]
You might want to look at using a callback function that increments a counter every time a change of state is seen. RPi.GPIO's Interrupts and Edge detection is a little-too-brief introduction. Some potential code is here How to use the water flow sensor with raspberry?, but note the corrections given further down the same page.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

troysie
Posts: 24
Joined: Mon Feb 13, 2017 8:33 am

Re: Python code for flow meter

Wed Dec 06, 2017 5:16 pm

I get invalid syntax on the following line, the semi colon is highlighted

Code: Select all

      while time.time() &lt;= time_new:        
Regards Troysie

User avatar
rpdom
Posts: 11795
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK

Re: Python code for flow meter

Wed Dec 06, 2017 5:45 pm

I suspect that is an HTML markup failure. The code should look like this

Code: Select all

      while time.time() <= time_new:

User avatar
scruss
Posts: 1398
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Python code for flow meter

Thu Dec 07, 2017 12:30 am

yeah, I just pasted back your code from the Python source. Guess I didn't catch all the HTMLizing. I certainly didn't fix anything
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

troysie
Posts: 24
Joined: Mon Feb 13, 2017 8:33 am

Re: Python code for flow meter

Thu Dec 07, 2017 7:56 am

I have found this code on the internet that only operates when flow is detected. It returns sensor High or Low and time. I have tried this and it works. Can I incorporate into this code to record L/min and total volume.

Code: Select all

 #!/usr/bin/python
#--------------------------------------
#    ___  ___  _ ____
#   / _ \/ _ \(_) __/__  __ __
#  / , _/ ___/ /\ \/ _ \/ // /
# /_/|_/_/  /_/___/ .__/\_, /
#                /_/   /___/
#
#       Hall Effect Sensor
#
# This script tests the sensor on GPIO17.
#
# Author : Matt Hawkins
# Date   : 03/04/2017
#
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------

# Import required libraries
import time
import datetime
import RPi.GPIO as GPIO

def sensorCallback(channel):
  # Called if sensor output changes
  timestamp = time.time()
  stamp = datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S')
  if GPIO.input(channel):
    # No magnet
    print("Sensor HIGH " + stamp)
  else:
    # Magnet
    print("Sensor LOW " + stamp)

def main():
  # Wrap main content in a try block so we can
  # catch the user pressing CTRL-C and run the
  # GPIO cleanup function. This will also prevent
  # the user seeing lots of unnecessary error
  # messages.

  try:
    # Loop until users quits with CTRL-C
    while True :
      time.sleep(0.1)

  except KeyboardInterrupt:
    # Reset GPIO settings
    GPIO.cleanup()

# Tell GPIO library to use GPIO references
GPIO.setmode(GPIO.BCM)

print("Setup GPIO pin as input on GPIO17")

# Set Switch GPIO as input
# Pull high by default
GPIO.setup(17 , GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(17, GPIO.BOTH, callback=sensorCallback, bouncetime=200)

if __name__=="__main__":
   main()   
Regards Troysie

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 47 guests