import requests import json import RPi.GPIO as GPIO import os, sys, math, logging import time import datetime from threading import Timer #from datetime import datetime from decimal import * from flowMeter import * from config import * from functools import partial #config file is defining KEY and BASE_URL logging.basicConfig( filename='gpioDaemon.log', level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(message)s') DEBUG=True logging.info('Running gpio-daemon') logging.info('DEBUG %s', DEBUG) # Tell GPIO library to use GPIO references GPIO.setmode(GPIO.BCM) KEG_1 = 17 KEG_2 = 23 KEG_3 = 22 # Define pins for diff kegerators GPIO.setup(KEG_1 , GPIO.IN) GPIO.setup(KEG_2 , GPIO.IN) GPIO.setup(KEG_3 , GPIO.IN) logging.info('Setup GPIO pin as input') logging.info('Setup Flowmeter') #pulling from flowmeter.py fm = FlowMeter('', ['beer']) #Class GpioDaemon: def sensorCallback1(kegNum, channel): # Called if sensor output goes LOW timestamp = time.time() stamp = datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S') currentTime = int(time.time() * FlowMeter.MS_IN_A_SECOND) #print "Sensor LOW " + stamp if fm.enabled == True: fm.update(currentTime) fm.kegNum = kegNum #defines the current keg logging.info(fm.lastClick) def pourDrinkEvent(clicks, kegNum): #drink is being poured #need to wait for it to finish logging.info('in drink loop') logging.info('initial clicks %s', clicks) logging.info('initial fmclicks %s', fm.clicks) time.sleep(3) logging.info('after sleep clicks %s', clicks) logging.info('after sleep fmclicks %s', fm.clicks) if(fm.clicks <= 3): #ghost pour logging.info('ghost pour, %s', fm.clicks) fm.clear() elif(fm.clicks == clicks and fm.clicks > 3): #not pouring? logging.info(fm.clicks) reportDrinkEvent(fm.clicks, kegNum) else: #return to loop logging.info('in pourDrinkEvent else condition') pourDrinkEvent(fm.clicks, kegNum) def reportDrinkEvent(clicks, kegNum): #the server should calculate oz from ticks but I was getting bad readings from it. mlVolume = clicks*2.25 myDrink = {'kegNum' : kegNum, 'ticks': clicks, 'volume_ml': mlVolume} p = requests.post(BASE_URL+'kegAmount', data=myDrink) #drink reported, resetting indictator fm.clear() data = p.json() logging.info(data) return data def pourGet(): return fm.getFormattedTotalPour() def pourReset(): fm.clear() def stillAlive(): Timer(600, stillAlive).start() logging.info('still alive') def main(): #initial build out GPIO.add_event_detect(KEG_1, GPIO.FALLING, callback=partial(sensorCallback1,1)) #1 defines keg num GPIO.add_event_detect(KEG_2, GPIO.FALLING, callback=partial(sensorCallback1,2)) GPIO.add_event_detect(KEG_3, GPIO.FALLING, callback=partial(sensorCallback1,3)) stillAlive() try: while True: time.sleep(1) #logging.info('in sleep loop') #push this off to definition if(fm.clicks > 0): pourDrinkEvent(fm.clicks, fm.kegNum) except KeyboardInterrupt: GPIO.cleanup() sys.exit() if __name__=="__main__": main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question