Sep-09-2020, 10:44 PM
Most recent edit is the Reset button to re-establish what full is. The goal is to use the same code for a variety of beers that would all have different full weights. Also, this could be used for placing a 12 pack on the scale and using the reset to "recalibrate" the scale.
Behold the code:
Behold the code:
# Scale project that uses a scale to determine the volume of beer in the keg. # A row of LEDs indicates the state of the program and the volume of the keg. # A requests post initiates a push notification to my phone when the weight of the keg changes. import time import sys import requests from gpiozero import LED #Define LED indicators - these are GPIO numbers LED1 = LED(21) #Blue LED2 = LED(26) #Red LED3 = LED(20) #Amber LED4 = LED(19) #Green LED5 = LED(16) #Green LED6 = LED(13) #Green LED7 = LED(6) #Green LED8 = LED(12) #Green LED9 = LED(5) #Pink Reset = Button(4) #Button to reset the variable "full" to the current scale value EMULATE_HX711=False referenceUnit = 1 if not EMULATE_HX711: import RPi.GPIO as GPIO from hx711 import HX711 else: from emulated_hx711 import HX711 def moving_average(prev_average, new_value, num_steps = 5.0): return (prev_average * (num_steps - 1) + new_value) / num_steps def reset_full(): full = keg1 average = weight = 400000 #just a value close to what a newly deployed keg would be w_threshold = 2000 #approximate value of 3-4 fluid ounces of the beer def cleanandexit(): print("Cleaning...") if not EMULATE_HX711: GPIO.cleanup() print("Bye!") sys.exit() hx = HX711(27, 22) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(1) hx.reset() hx.tare() print("The scale is ready") print("put the keg on...") LED9.blink(.2,3) # indication that the scale is running - not blinking means program crash start = time.time() while True: try: keg1 = hx.get_weight(5) weight = keg1 keg1 = round(keg1,3) start_weight = keg1 print(keg1, average) if time.time() - start > 5: weight = keg1 lost_weight = average - keg1 lost_weight = round(lost_weight,3) if lost_weight > w_threshold: #r = requests.post("https://bit.ly/editedforprivacy") #Join URL that triggers push notification to my phone when beer is served print("You got served") average = keg1 #resets average to new keg1 value for faster recovery print(lost_weight) #visual indication of how much was poured - one pint is about 8400 average = moving_average(average, weight) average = round(average,3) start = time.time() Reset.when_pressed = reset_full #These lines get the percent of full value to properly run the LEDs full = keg1 #a full keg is typically around 505000 to 516000 empty = 89000 #an empty keg shows approx 89000 when placed on the scale scale = 100 / (full - empty) percent_full = (weight - empty) * scale if 1 >= percent_full <= 102: #keg is outside of usable range - flashes when keg is missing (indicating tare is complete at startup) LED1.blink(.2,.2) else: LED1.off() if 1.1 >= percent_full >= 5: #Red less than 5% - DANGER LED2.blink(.5,.5) else: LED2.off() if 100 >= percent_full >= 10: #Amber less than 10% - Warning LED3.on() else: LED3.off() if 100 >= percent_full >= 20: # Green 10-20% - Caution LED4.on() else: LED4.off() if 100 >= percent_full >= 40: #Green2 20-40% LED5.on() else: LED5.off() if 100 >= percent_full >= 60: #Green3 40-60% LED6.on() else: LED6.off() if 100 >= percent_full >= 80: #Green4 60-80% LED7.on() else: LED7.off() if 100 >= keg1 >= 80.1: #Green5 80-100% LED8.on() else: LED8.off() hx.power_down() hx.power_up() time.sleep(1) except (KeyboardInterrupt, SystemExit): cleanandexit()