I have a scale project that checks the weight of a water cooler's bottle every hour (cronjob). When this runs, it turns on one of two LEDs to indicate the water level. The way the code runs now (below), the appropriate LED stays on for just a few seconds. Since this code runs every hour, I would like the LED to stay on for 57 minutes, which is 3420 seconds. This way the code has run its course before the next time it runs again. This LED gives me a status of the water level at a quick glance.
The code:So if the "roundedval" is more than 10, I want LED1 (green). If it is less than 10, I want LED2 (red). The LED should stay on until just before the code runs again at the top of the hour. I'm ok with having 3 minutes of no LED. Just want this to run its course and not have multiple instances of this code running.
The code:
Code:
import timeimport sysimport RPi.GPIO as GPIOfrom hx711 import HX711import requestsfrom ShaneKeys import ARKEY1from gpiozero import LEDimport jsonfrom time import sleep LED2 = LED(26) # Red LED to indicate low level.LED1 = LED(16) # Green LED to indicate high level. def cleanAndExit(): print("Cleaning...") GPIO.cleanup() print("Bye!") sys.exit() def load_calibration_data(): with open('/home/pi/hx711py/calibration_data.json', 'r') as f: return json.load(f) def measure_weight(): calibration_data = load_calibration_data() hx = HX711(20, 21) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(calibration_data['referenceUnit']) hx.set_offset(calibration_data['offset']) val = hx.get_weight(5) wholeval = (val * -1) newval = wholeval / 100 roundedval = round(newval, 2) print(roundedval) weight = str(roundedval) if roundedval > 10: LED2.off() LED1.on() else: LED2.on() LED1.off() try: r = requests.post(ARKEY1 + "|water|" + str(roundedval)) except requests.exceptions.RequestException as e: print(f"Failed to send data: {e}") hx.power_down() hx.power_up() if __name__ == "__main__": measure_weight()
Statistics: Posted by duckredbeard — Wed Mar 12, 2025 3:18 pm