Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1225

Python • Script runs fine from Thonny, but not SSH or Geany

$
0
0
I have a project that uses load cells and an HX711 to measure the weight of my dog food bucket every hour. I'd like to set it up to run on an SSH command or cronjob, or maybe initiated by a switch on the dog food cabinet door. I have one script that does a calibration and saves info, then another script uses that info to get the weight and send it to my Android phone on a loop of 1 hour. This all works well and has been very reliable.

I successfully adapted the looping script to what I provided below, but it is not working from any other way but Thonny. If I use terminal, it fails. If I run it by SSH from another console, it fails. If I try to call it from another script, it fails.

Here is the script that runs well from Thonny, but no other way:

Code:

import timeimport sysimport RPi.GPIO as GPIOfrom hx711 import HX711import requestsfrom ShaneKeys import ARKEY1from gpiozero import LEDimport jsonimport time 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'])     val = hx.get_weight(5)    wholeval = (val * 1)    newval = wholeval / 100    roundedval = round(newval, 2)    #roundedval = roundedval + 25.52####################    Adjusted for loadedzero calibration    current_time = time.strftime("%H:%M:%S", time.localtime())    print(roundedval, current_time)    #print(current_time)    weight = str(roundedval)                if roundedval > 10:        LED2.off()        LED1.on()    else:        LED2.on()        LED1.off()                 try:        #LED2.off()        r = requests.post(ARKEY1 + "|dogfood|" + 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()         #except (KeyboardInterrupt, SystemExit):            #cleanAndExit() if __name__ == "__main__":    measure_weight()
I always get the following failure unless running from Thonny:

Code:

Traceback (most recent call last):  File "/home/pi/hx711py/checkdogfood.py", line 66, in <module>    measure_weight()  File "/home/pi/hx711py/checkdogfood.py", line 25, in measure_weight    calibration_data = load_calibration_data()  File "/home/pi/hx711py/checkdogfood.py", line 21, in load_calibration_data    with open('calibration_data.json', 'r') as f:FileNotFoundError: [Errno 2] No such file or directory: 'calibration_data.json'
Why does it only run from Thonny? I have used python and python3 in my SSH commands and other scripts. When I run it from Geany, it causes errors in the script that is already running, pretty much going out of calibration and reporting crazy numbers. I even tried running the one time check while the looping script was stopped. I would much rather have this run from a cronjob or other trigger instead of a loop.

Important to note that it runs fine from Thonny with no effect on any other script already running. In Thonny it is fine. But nowhere else.

Statistics: Posted by duckredbeard — Sun Feb 02, 2025 3:15 am



Viewing all articles
Browse latest Browse all 1225

Trending Articles