Python Forum
Run this once instead of a loop, do I need the 'calibration' steps?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Run this once instead of a loop, do I need the 'calibration' steps?
#1
I am running this code to measure the weight of a water jug. All is working but I expect that I might have issues if the internet is interrupted when it tries to send the requests.post. Is there a way I can run the calibration part (lines 21-26) one time (saving anything important) and then just run it on a cronjob or SSH command?

Here is the whole thing...
import time import sys import RPi.GPIO as GPIO from hx711 import HX711 import requests from ShaneKeys import ARKEY1 from gpiozero import LED LED2 = LED(12) # Red LED to indicate low level. LED1 = LED(16) # Green LED to indicate high level. def cleanAndExit(): print("Cleaning...") print("Bye!") sys.exit() LED1.on() # Indication that the program has started and that LED indications are functional. LED2.on() # Indication that the program has started and that LED indications are functional. hx = HX711(20, 21) hx.set_reading_format("MSB", "MSB") referenceUnit = 114 hx.set_reference_unit(referenceUnit) hx.reset() hx.tare() LED2.off() # Indication that reset and tare functions are complete. time.sleep(5) # Take a moment to reflect. LED1.off() # Indication that reset and tare functions are complete. print("Scale has been set to zero, add weight now...") # This is when I know that the "zero" or "calibration" is done. If not using VNC, I use the LEDs to tell me when to load the load. while True: try: val = hx.get_weight(5) wholeval = (val * -1) # For some reason the "val" was reporting a negative value. This makes it positive. newval = wholeval / 100 # This makes the value in a unit of whole pounds (2154 becomes 21.54 pounds). roundedval = round(newval, 2) # This ensures that the pound value is limited to two decimal places. print(roundedval) # This displays the measured weight in pounds. weight = str(roundedval) if roundedval > 10: # If the volume is greater than 25%, then power on the green LED on GPIO16. LED2.off() LED1.on() else: # If the volume is less than 25%, then power the red LED on GPIO12. LED2.on() LED1.off() r = requests.post(ARKEY1 + "|water|" + str(roundedval)) #This is where the script sends the weight to my phone - the variable "roundedval" is the weight of the keg in pounds. hx.power_down() hx.power_up() time.sleep(1800) #Sleep for 30 minutes then recheck the weight of the water. except (KeyboardInterrupt, SystemExit): cleanAndExit()
Reply
#2
Here I have created two scripts: one for calibration and another for regular measurements.

calibrate.py:

import RPi.GPIO as GPIO from hx711 import HX711 import json from gpiozero import LED LED2 = LED(12) # Red LED to indicate low level. LED1 = LED(16) # Green LED to indicate high level. def calibrate(): LED1.on() # Indication that the program has started and that LED indications are functional. LED2.on() # Indication that the program has started and that LED indications are functional. hx = HX711(20, 21) hx.set_reading_format("MSB", "MSB") referenceUnit = 114 hx.set_reference_unit(referenceUnit) hx.reset() hx.tare() LED2.off() # Indication that reset and tare functions are complete. LED1.off() # Indication that reset and tare functions are complete. print("Calibration complete. Saving calibration data...") # Save calibration data calibration_data = { "referenceUnit": referenceUnit, "offset": hx.get_offset() } with open('calibration_data.json', 'w') as f: json.dump(calibration_data, f) print("Calibration data saved. You can now run the measurement script.") if __name__ == "__main__": try: calibrate() except (KeyboardInterrupt, SystemExit): print("Calibration interrupted.") finally: GPIO.cleanup()
measure_weight.py:

import time import sys import RPi.GPIO as GPIO from hx711 import HX711 import requests from ShaneKeys import ARKEY1 from gpiozero import LED import json LED2 = LED(12) # 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('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']) while True: try: 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}") # You might want to implement a retry mechanism here hx.power_down() hx.power_up() time.sleep(1800) # Sleep for 30 minutes then recheck the weight of the water. except (KeyboardInterrupt, SystemExit): cleanAndExit() if __name__ == "__main__": measure_weight()
Let me know if you face any issue while running these scripts
duckredbeard likes this post
Our Scratch project: scratch geometry dash
Reply
#3
I will certainly try this today. I was just messing with it remotely and accidentally stopped my looping script. The only way to get it going is to be there, remove the weight, restart the existing script, then restore the weight.

It is killing me that I no longer get updates on the quantity of the water jug in the water cooler in my den. Perhaps I should leave work early!

All jokes aside, I see plenty of uses for this script. Under the big bin of dog food to alert me to go get them food. Under their gravity fed) water dispenser so I will know they need water. Under the shelf of my beer fridge to know if my kids are getting into my beer.

I like to create problems just to see if they can be solved with a Raspberry Pi or the Tasker (Android) app.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Sorting Steps MoreMoney 19 6,910 Mar-31-2024, 10:59 AM
Last Post: Pedroski55
  Next steps for using API data Rebster 6 4,457 Oct-10-2020, 05:35 PM
Last Post: perfringo
  Keep Toggle View through Slider Steps yourboyjoe 1 2,559 Aug-10-2020, 07:32 PM
Last Post: Yoriz
  Files to store configuration and steps for a industrial process control application Danieru 1 2,808 Aug-03-2020, 05:43 PM
Last Post: buran
  Pexpect baby steps slouw 9 11,986 May-23-2019, 10:21 AM
Last Post: heiner55
  first steps with python3 hunnimonstr 5 6,516 Jul-03-2017, 08:49 PM
Last Post: hunnimonstr

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.