Issue with running python script

What is the problem?

I am trying to create a control button that runs a python script located on my raspberry pi 4 using GCODE System Commands, but I am unable to get the script to run from the button. I also am unsure of how to properly display the terminal output.

What did you already try to solve it?

I have tried rewriting the pathing and taken a look at similar topics, but I cannot get the button to function properly. I also am unaware of how to format the output properly based on the information given by the config.yaml information page.

edited by mod: removed half of salt and key

Does the gcode command work to run the python script from the terminal tab and that part is working?

also, does gcode system commands allow up to 801 on it's numbering? I think maybe it should be OCTO01?

I am unable to type into the terminal, I am currently running the octopi disconnected from the printer as I do not have access to it due to covid restrictions. I am trying to build a plugin that will output sensor data connected to the pi so I have been working it it separate from the printer in the meantime. I will attempt to connect it to a printer in a few hours and see if that resolves the issue.

You can enable the virtual printer in settings and have a fake printer connected to test with...

Thank you, I was unaware of this. This resolved the issue I was having regarding not being able to press the button, but I am now getting an error within the terminal when the command is called.

image

This is my current command definition, I believe it is pathed correctly as if I type this command within the terminal on remote desktop I get a terminal readout that I would expect but it is not functioning and returning that error.

sometime you have to put the interpreter for the file at the top and chmod +x it to make it executable maybe? so something like #!/usr/bin/python3 as the first line of your py file. or are you using a sh script? The important part that the command is configured with the full path to the file as well in the plugin's settings.

I added the suggested line to the code, added the file to chmod +x, but am still getting the same error. The command is shown below, I am trying to run a .py file.

That is probably the issue. I don't think you can sudo with the plugin unless you add a sudoers rule to the OS, but @kantlivelong would have to confirm. With adding those previous bits and making it executable you shouldn't need the python3 command at all I don't think. Does the python file require root access to properly run?

It does not need the root, removing the python3 command does not solve the issue. I am still receiving the same error unfortunately.

Maybe this will help.

I've looked through that thread and recreated the test files you gave, and I am still getting an error from the octoprint terminal. I've used the chmod +x test.sh command, but running sh test.sh does not return any output or error, I am unsure what I am doing wrong.

I am at my wits end, I have all files running properly through the terminal from remote desktop, but I am returning an error instead of printing from the terminal. My programs are:

ADXLrun.sh

#!/bin/bash
python3 /home/pi/ADXLtest2.py

ADXLtest2.py

import time
import board
import busio
import adafruit_adxl34x


print("Started!")

i2c = busio.I2C(board.SCL, board.SDA)
accelerometer = adafruit_adxl34x.ADXL345(i2c)
accelerometer.enable_freefall_detection(threshold=10, time=25)
accelerometer.enable_motion_detection(threshold=18)
accelerometer.enable_tap_detection(tap_count=1, threshold=20, duration=50, latency=20, window=255)

while True:
    print("%f %f %f"%accelerometer.acceleration)
    print("Dropped: %s"%accelerometer.events["freefall"])
    print("Tapped: %s"%accelerometer.events['tap'])
    print("Motion detected: %s"%accelerometer.events['motion'])
    time.sleep(0.5)

OCTO01
/home/pi/ADXLrun.sh

As I see, in your last post you mention that you are trying to use an ADXL345.
AFAIK, only Klipper uses this device.

You may ask that board there too:

Shot in the dark, but where is the adafruit_adxl34x library installed?

You could capture the full output of the python script by modifying your first script to look like this:

#!/bin/bash
python3 /home/pi/ADXLtest2.py >> /home/pi/adxltest.log

That might help to diagnose the issue?

Also, if you enable debug logging for the plugin, it should log the complete output of the command which might also be helpful.

This is the output from ADXLtest2.log with the modification you suggested:
Started!
0.000000 0.000000 0.000000
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 11.924886
Dropped: False
Tapped: False
Motion detected: False
0.039227 3.138128 11.924886
Dropped: False
Tapped: False
Motion detected: True
0.039227 3.059675 11.924886
Dropped: False
Tapped: True
Motion detected: True
0.039227 3.138128 11.924886
Dropped: False
Tapped: False
Motion detected: False
0.039227 3.138128 12.003340
Dropped: False
Tapped: False
Motion detected: True
0.039227 3.098901 11.885660
Dropped: False
Tapped: False
Motion detected: True
0.039227 3.098901 11.885660
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 11.924886
Dropped: False
Tapped: False
Motion detected: True
0.039227 3.098901 11.924886
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 11.885660
Dropped: False
Tapped: False
Motion detected: True
0.117680 3.059675 11.885660
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 11.924886
Dropped: False
Tapped: False
Motion detected: True
0.039227 3.138128 11.885660
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.059675 11.885660
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 12.003340
Dropped: False
Tapped: False
Motion detected: True
0.039227 3.059675 11.885660
Dropped: False
Tapped: False
Motion detected: True
0.039227 3.059675 11.924886
Dropped: False
Tapped: False
Motion detected: False
0.078453 3.098901 11.885660
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 11.964113
Dropped: False
Tapped: False
Motion detected: True
0.117680 3.059675 11.885660
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 11.924886
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 11.964113
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 11.964113
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 11.885660
Dropped: False
Tapped: False
Motion detected: True
0.078453 3.098901 11.885660
Dropped: False
Tapped: False
Motion detected: True
Traceback (most recent call last):
File "/home/pi/ADXLtest2.py", line 19, in
time.sleep(0.5)
KeyboardInterrupt

The adafruit_adxl34x library is installed at /usr/local/lib/python3.7/dist-packages

This is what is returned from the octoprint.log:
2022-01-26 00:12:54,934 - octoprint.plugins.gcodesystemcommands - DEBUG - Command ID=01, Line=/home/pi/test.sh, Args=None
2022-01-26 00:12:54,934 - octoprint.plugins.gcodesystemcommands - INFO - Executing command ID: 01
2022-01-26 00:12:54,990 - octoprint.plugins.gcodesystemcommands - DEBUG - Command ID 01 returned: 127, output=b'/bin/sh: 1: /home/pi/test.sh: not found\n'
2022-01-26 00:12:54,991 - octoprint.plugins.gcodesystemcommands - INFO - Command ID 01 returned: 127

If you go through the ticket I previously linked it explains how to determine the import issue you are facing. It even has the solution.

This is suggesting the wrong path is configured for the script? It is saying /home/pi/test.sh, above you wrote the file name as ADXLrun.sh. so then the error below is saying it can't find that file.