1
0
mirror of https://github.com/skaringa/water-counter.git synced 2024-11-22 01:14:24 +01:00

Use serial line to communicate with arduino

This commit is contained in:
Martin Kompf 2016-04-02 18:37:03 +02:00
parent 5a58e22bb8
commit 8a3a787351
2 changed files with 25 additions and 31 deletions

View File

@ -1,3 +0,0 @@
gpio unexport 9
gpio export 9 in
gpio edge 9 rising

View File

@ -1,13 +1,13 @@
#!/usr/bin/python -u #!/usr/bin/python -u
# #
# emeir.py # wairc.py
# #
# Program to read the electrical meter using a reflective light sensor # Program to read the water counter using a reflective light sensor
# This is the data recording part running on a Raspberry Pi. # This is the data recording part running on a Raspberry Pi.
# It retrieves data from the Arduino over USB serial and stores # It retrieves data from the Arduino over serial and stores
# counter and consumption values into a round robin database. # counter and consumption values into a round robin database.
# Copyright 2015 Martin Kompf # Copyright 2015-16 Martin Kompf
# #
# This program is free software: you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
@ -26,12 +26,12 @@ import time
import sys import sys
import os import os
import re import re
import select
import argparse import argparse
import serial
import rrdtool import rrdtool
# GPIO pin to read from # Serial port of arduino
gpio_pin = 9 port = '/dev/ttyAMA0'
# counter unit: 1 revolution = x m^3 # counter unit: 1 revolution = x m^3
trigger_step = 0.001 trigger_step = 0.001
@ -78,12 +78,6 @@ def last_rrd_count():
handle.close() handle.close()
return val return val
# Setup gpio edge interrupt triggering
def setup_gpio(pin):
os.system("gpio export {} in".format(pin))
os.system("gpio edge {} rising".format(pin))
# Main # Main
def main(): def main():
# Check command args # Check command args
@ -94,29 +88,32 @@ def main():
if args.create: if args.create:
create_rrd() create_rrd()
# Open serial line
ser = serial.Serial(port, 9600)
if not ser.isOpen():
print "Unable to open serial port %s" % port
sys.exit(1)
trigger_state = 0
counter = last_rrd_count() counter = last_rrd_count()
print "restoring counter to %f" % counter print "restoring counter to %f" % counter
# open gpio input value file
# and register handler for edge trigger
setup_gpio(gpio_pin)
f = open("/sys/class/gpio/gpio{}/value".format(gpio_pin), 'r')
epoll = select.epoll()
epoll.register(f, select.EPOLLIN | select.EPOLLET)
events = epoll.poll() # eat the first edge
while(1==1): while(1==1):
events = epoll.poll() # Read line from arduino and convert to trigger value
f.seek(0) line = ser.readline()
value = f.read(1) line = line.strip()
if value == '1':
# trigger edge detected -> update count rrd old_state = trigger_state
if line == '1':
trigger_state = 1
elif line == '0':
trigger_state = 0
if old_state == 1 and trigger_state == 0:
# trigger active -> update count rrd
counter += trigger_step counter += trigger_step
update = "N:%.3f:%.3f" % (counter, trigger_step) update = "N:%.3f:%.3f" % (counter, trigger_step)
#print update #print update
rrdtool.update(count_rrd, update) rrdtool.update(count_rrd, update)
f.close()
if __name__ == '__main__': if __name__ == '__main__':
main() main()