New plugin idea

Hey all you plugin developers... you guys are awesome and I super appreciate all the plugins that are out there. Really love it. I have an idea for a new one though...

I would love it if there was a plugin that could respond to a button press - a physical button I connect to the Pi's GPIO - and run a gcode command or set of commands when it's triggered. This seems decently simple and infinitely useful to me but I don't have the skills yet to make an octoprint plugin. If anybody's itchin' for a plugin idea, have at it.

Of course, if my dream plugin already exists, please alert me to my ignorance :slight_smile:

Thanks and have a nice day!

The Enclosure will do this (and a lot of other nifty stuff):

You could also tie your button(s) into a WiFI-happy microcontroller (say, a $4 ESP32) that responds to the buttons and sends commands to the OctoPi via its REST API.

2 Likes

Or you could just write a tiny python script to run on your raspi that watches the GPIO pin and when it triggers sends the GCODE you want to OctoPrint's API.
Below is untested, has no error handling and just sketches the idea:

#!/usr/bin/python3
####################
# if you get   ModuleNotFoundError:     No module named 'RPi'
# you need to install that module by    sudo apt-get install python3-dev python3-rpi.gpio


import RPi.GPIO as GPIO
import time
import os

input_pin=18
GCODE_line="M117 hello world"
api_key="API KEY FROM OCTOPRINT"
octopi_url="http://octopi.local/api/printer/command"

cmd='{"command":"'+GCODE_line+'"}'
call='curl -X POST  -H "Content-Type: application/json" -H "X-Api-Key":"{}" -d \'{}\' {}'.format(api_key,cmd,octopi_url)



def button_action():
    print('Button Pressed')
    os.system(call)


GPIO.setmode(GPIO.BCM)
GPIO.setup(input_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

print("buttoner on {} for {}".format(input_pin,call))

while True:
    input_state = GPIO.input(input_pin)
    if input_state == False:
        button_action()
        time.sleep(0.2)

You'd run that manually on your raspi and once things work as intended you can make it a daemon by setting up a systemd service file for it.

1 Like

this script could be configured to automatically start with OctoPrint by using the event subscriptions available in config.yaml.

events:
  subscriptions:
    - event: Startup
      command: "/full/path/to/python/script"
      type: system
1 Like

These are all great ideas, I really appreciate it. I think I’m going to go with @Adam_Klein ‘s second idea because I’m already developing an esp32-based smart watch and I just implemented fetching the job status through the API. I hadn’t thought about it before but it should be pretty simple to send a single terminal command over the API and it would be even cooler to do it from my watch. I might also make that script so I can do it at the printer without my watch.

Thanks!

PS just in case anyone’s using the same hardware, it’s a TTGO T-Watch

I am currently developing a plugin that just does what you described :slight_smile:

It's not officially released yet but I'm testing it for some time now, so it should be safe to use. Here is the link if you are interested in trying it out: PhysicalButton

Nice plugin. I look forward to reviewing it when you submit it to the official plugin repository.

1 Like