M600 event hook request

What is the problem?

I am using the event hooks to call a custom script to alert me when a print start/stops/fails etc...

I would like to get an event trigger when a M600 command is sent to the printer so I know when it needs a filament change instead of listening for the beeps.

What did you already try to solve it?

Looked in the event documentation and don't see any reference to the M600 command generating an event.

Additional information about your setup (OctoPrint version, OctoPi version, printer, firmware, octoprint.log, serial.log or output on terminal tab, ...)

Not relevant

Thanks

I'm no software developer, but as I had a simular issue and I wrote a small plug-in inspired by the GCODE Systems Command Plugin.

I put a .py file with the following code into my octoprint/plugins folder. Everytime a M600 gcode is sent to the printer a specified shell script gets fired up. The shell script sends out a pushbullet notification to my phone in my case.

Here is my code:

# coding=utf-8
from __future__ import absolute_import

import octoprint.plugin
import time
import os
import sys

class M600ActionPlugin(octoprint.plugin.StartupPlugin):

    def hook_gcode_sent(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwargs):
        script_path= *** insert path to your script here ***
        if gcode and cmd[:4] == "M600":
            self._logger.info("M600 registered")
            try:
                r = os.system(script_path)
            except:
                self._logger.error("Script could not be run")

def __plugin_load__():
    global __plugin_implementation__
    __plugin_implementation__ = M600ActionPlugin()

    global __plugin_hooks__
    __plugin_hooks__ = {
        "octoprint.comm.protocol.gcode.sent": __plugin_implementation__.hook_gcode_sent
    }

__plugin_name__ = "M600 Shell Action"
__plugin_version__ = "0.1.0"
__plugin_description__ = "Plugin fires up a shell script, if a M600 gcode is sent to the printer."

As I said, I am no software developer. If someone wants to take it and make it better, instalable plugin, feel free.

2 Likes

Thanks so much for responding.

I have a print running right now but as soon as its done I will try to install your plugin/code.

If this works no more listening for the printer beeping to let me know its ready to swap filament.
I will get notified via Slack.

Tech-Rat

1 Like

That code worked perfectly. Thanks so much.

Tech-Rat

1 Like

You're welcome. :slight_smile: Glad I could help you.

1 Like

Even if this thread is a bit old - i had the same problem, and solved it with your little script in no time. Very useful!

1 Like

This is exactly what I need but unfortunately I get an error when I run it:

WARNING - Plugin M600 Shell Action (0.1.0) is not compatible to Python 3.7.3 (compatibility string: >=2.7,<3).
| *M600 Shell Action (0.1.0)

Any hope other than rewriting it to be compatible? I'm not skilled enough with Python to even know where to start.

Just add the compatibility flag to the end, like this. I don't see anything glaringly wrong that would be incompatible.

# coding=utf-8
from __future__ import absolute_import

import octoprint.plugin
import time
import os
import sys

class M600ActionPlugin(octoprint.plugin.StartupPlugin):

    def hook_gcode_sent(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwargs):
        script_path= *** insert path to your script here ***
        if gcode and cmd[:4] == "M600":
            self._logger.info("M600 registered")
            try:
                r = os.system(script_path)
            except:
                self._logger.error("Script could not be run")

def __plugin_load__():
    global __plugin_implementation__
    __plugin_implementation__ = M600ActionPlugin()

    global __plugin_hooks__
    __plugin_hooks__ = {
        "octoprint.comm.protocol.gcode.sent": __plugin_implementation__.hook_gcode_sent
    }

__plugin_name__ = "M600 Shell Action"
__plugin_version__ = "0.1.0"
__plugin_pythoncompat__ = ">=2.7,<4"
__plugin_description__ = "Plugin fires up a shell script, if a M600 gcode is sent to the printer."
1 Like

Oh, that's so wonderfully easy, thank you!

There is also the FilamentChange event now as well, so you could use the Event Manager in OctoPrint's settings to make this work now. It fires on M600, M701 or M702. Quite new, in 1.7.0.

https://docs.octoprint.org/en/master/events/index.html#gcode-processing

Wow, I guess I should look at the release notes more than just a quick skim - thank you!

lol...me too. I even contributed the event manager plugin to OctoPrint core and had forgot about that new event.

Big thanks to @tierra for the original plugin, and thanks to @jneilliii for showing how to easily fix it, (and it works!) but huge thanks to @Charlie_Powell for pointing out the new functionality!!

So, if anyone is wanting to use this, you don't need the plugin anymore - Just add this to your config.yaml:

events:
  enabled: true
  subscriptions:
  - command: /path/to/script
    enabled: true
    event: FilamentChange
    type: system

Just like @tech-rat I have it sending alerts to Slack so I don't have to be at the printer when running multi-color prints. This is so cool, thanks all!

1 Like

It's even easier to use the Event Manager in settings... no need to monkey with yaml formatting, etc.

No kidding, will have to check that out, but I'm a linux sysadmin so I went straight to that.