Running a Python script with the click of a button

What is the problem?

I need to run a script that toggles a fan and led strip on and off. The command runs like this:
control.py fan true

I wanted to add two buttons (possibly the ON/OFF type GPIO Control uses) to toggle the strip and the fan.

What did you already try to solve it?

So far, I tried:

  • GCODE System Commands, creating a GCODE command that both runs the script directly (command = control.py fan true) and one that runs a wrapper bash file that runs the python file (command = run_script.sh). Both times, running from the terminal returns error.
  • Custom control editor, which does not work for running a Python script directly. Running the custom GCODE, unsurprisingly, does not work either, because the command does not work either.
  • System command editor: I managed to make it work by adding a button to the system control interface using System command editor, but this would require 4 entries (led on/off, fan on/off).

Have you tried running in safe mode?

I don't think it would make any difference.

Did running in safe mode solve the problem?

See above

Systeminfo Bundle

You can download this in OctoPrint's System Information dialog ... no bundle, no support!)
octoprint-systeminfo-20230815160101.zip (137.8 KB)

WRITE HERE

Additional information about your setup

OctoPrint version, OctoPi version, printer, firmware, browser, operating system, ... as much data as possible

Octopi 0.18.0, Octoprint 1.9.2, Python 3.7.3

This one seems to be what you are looking for.

It works without issue, but only allows the addition of one button with one command only, as far as I can tell.

Maybe adjust the script so it works as a toggle.

Yes, that's what I planned to do anyway. But I still can't have separate toggles for fan and led.

Combination of GCODE System Commands and Custom Controls would allow this.

After rereading your original post I see you tried this. Probably wasn't working because you didn't use full path to the python file, it wasn't chmod +x to be executable and didn't have the python interpreter set as first line in file (#!/usr/bin/env python3).

And now that I think about it, what about OctoRelay plugin?

As I said in the OP, I cannot get it to work for some reason. The custom GCODE always returns error.

They're not connected to the RPI GPIO, they are a whole separate device.

Just edited my response to why that may not have worked for you...

Now seems a good time to add what was in the shell file, I forgot to add it in the OP:

#!usr/bin/python3
/usr/bin/python3 ~/scripts/control.py fan true

While I did put the wrong "header" in the file, isn't this the full path to the script?
Also, I did chmod a+x the .sh script, should I just have done +x?

Wondering what this device is. Is it a custom device or something off the shelf ?

Custom device based off an ESP8266, controlling two MOSFETs that control a 12V LED Strip and fan.
The communication is based on the Firebase RTDB, because it's quite easy to integrate with all the systems I'm using (MIT App inventor, ESP8266 and Python).

I made all of this so I could control the fan and strip:

  • remotely, with the MIT App;
  • from within OctoPrint, with the custom buttons I'm trying to make;
  • automatically, as in turning the fan on and off automatically when a print starts/ends.

I edited the file, now it reads like this:

#!/bin/bash

python3 ~/scripts/control.py led true

When run from the CLI, it works just fine. I also ran chmod +x on the .sh file without success.

I'd try with

#!/bin/bash

/usr/bin/python3 /home/pi/scripts/control.py led true

Of course adjusting paths appropriately.

#!/bin/bash

/usr/bin/python3 /home/pi/scripts/control.py led true

This is what is in the .sh file that GCODE System Commands tries to run.

Return(GCodeSystemCommands): error

This is the error I've been getting since I started working with the plugin.

This is the GCODE System Commands plugin settings page.

I tried to restart many times, nothing changed.

(Sorry for the messy formatting, I had uploaded three images but being a new user I can only upload one).

try without the ~, use full path /home/pi/scripts/run_script.sh

It works! Specifying the full path EVERYWHERE fixed the issue. Thanks a lot for the help, it seems that this might have fixed the issue so far.

Cool, I thought it might. When these run there is no knowledge of user environment I don't think, which is why ~ was causing issues.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.