Control OctoPrint via MQTT

I have a plugin idea, but I am not the best programmer yet and this is out of my realm for now.

There a several plugins that let me control various things via MQTT, but I would like to be able to control my OctoPrint itself with MQTT messages.

A few things that I would like to control via MQTT messages include:

  • connect / disconnect from printer
  • select a file to print
  • initiate a print for the selected file.

I mainly want the connect / disconnect. I currently control the power to my printer with a MQTT message. So when I ask google to turn off my printer, it just pulls the plug on the printer and I get a "Printer Reset Detected" error every timer. It would be nice to be able to set up a script that disconnects the print before turning it off.

Any help with this would be greatly appreciated.

I don't think this would be too hard to accomplish actually utilizing the MQTT plugin's helper objects. I've already created an MQTT publish plugin that allows for sending messages to MQTT server. I guess a MQTT Subscribe plugin would be a good companion plugin to that.

there's an open ticket to do that with Octoprint-MQTT, and the code to subscribe and handle events is even listed in the README. So much of the tooling is in place, but .. of course, there are more ideas than time to implement them.

What power device are you controlling @floppystick6? Since it's MQTT my OctoPrint-TasmotaMQTT plugin may be able to do that power off with disconnect options enabled to prevent the "Printer Reset Detected" message.

I just updated the MQTT github listing itself to add the three related OctoPrint plugins, for what it's worth.

1 Like

Speaking of MQTT, does anyone know if there's a free MQTT server (host/broker) other than test.mosquito.org?

Alternatively, does OctoPrint serve up an MQTT host?

I am using a sonoff like device with custom firmware. I had issues with Tasmota not recognizing me pushing the button every time. So I flashed a different arduino code to the ESP8266.

I am not sure if your OctoPrint-TasmotaMQTT plugin will work for my situation. I am using Home Assistant running on a raspberry pi as a home automation server. I often find my self tell my Google Home to turn on/off my 3D printer. Google Home then tells Home Assistant to turn on/off the sonoff that the 3d Printer is plugged into. Then Home Assistant sends out the MQTT message to the sonoff.

What I would like to have happen is my Home Assistant would also tell OctoPrint to connect/disconnect at that time as well. I know there is an OctoPrint component to Home Assistant using the api, but I haven't been able to get that to work.

That was going to be my other recommendation assuming Home Assistant support REST api. You would just have to get your user API key and use that for posting to what ever command you wanted to do to the OctoPrint server. All those commands can be found over here

So I have a plugin that's a little complicated to set-up but it should work for your needs. It basically is a REST API proxy using MQTT subscriptions. Install the MQTT plugin, then install the OctoPrint-MQTTSubscribe plugin using the url https://github.com/jneilliii/OctoPrint-MQTTSubscribe/archive/master.zip in Plugin Manager.

Once both plugins are installed, go into MQTT settings and configure the connection to your MQTT server. Then in the MQTT Subscribe settings configure the index, topic, type and command to base your subscription on. These should match the REST API commands, so in your case you would set it up like the highlighted part in the image below, which is the equivalent of the Example Disconnect Request over here. The API KEY can be obtained from your user settings, from the flyout menu on your name in the top right of OctoPrint and is required for the REST API functionality to work.

Then in Home Automation use the MQTT Topic /octoprint/plugins/mqttsubscribe/connection/1 and data to submit should match the REST API payload, ie {"command":"disconnect"}.

The REST API response status code and text will be returned to the respective mqtt topic/index so you can act on the result of the command that was sent.

Status code gets published to /octoprint/plugins/mqttsubscribe/<topic>/<index>/status.
Response text gets publishd to /octoprint/plugins/mqttsubscribe/<topic>/<index>/response.

In theory any of the API commands could be used, as long as the information matches between the plugin settings and the information necessary to submit to REST API. I'm sure there is something that will have to be tweaked here and there. I think I might remove the match required between REST API payload and the plugin's command settings and just use the topic and index. Would probably be good to add an automatic index generator and distinct index validation.

Please submit issues to the plugin's issue tracker here.

2 Likes

@jneilliii First of all, thank you for the amazing contributions you have made to so many aspects of OctoPrint!

I had been following the progress of your in-development plugin that allows OctoPrint to subscribe to MQTT topics and perform actions and was quite keen to use it on my setup. However, I read on one of your GitHub discussions that you have abandoned this plugin and intend to build this feature directly as part of an the official MQTT plugin in OctoPrint. I do see that MQTTSubscribe is available as a function for scripting, however, I do not see any UI to configure subscriptions as part of that plugin.

Could you please let me know if you there are any plans to implement the UI you have shown above, for that plugin, so that novices like me can still make use of MQTT? I feel it would be a great feature to have, especially when you want to integrate Octoprint with external ESP based switches or even changes in power (from UPSs).

I've been on a kind of development hiatus for a bit. Haven't really felt inclined to do much lately with these plugins. I do think it would be a good addition to the MQTT plugin to have this functionality, just haven't had the time to think about the best approach to implement it. I'd rather make something more user friendly over the way the MQTTSubscribe plugin works. I think it's just too cumbersome for the average non-programmer. We do have it flagged as an enhancement request over here.

I understand. Thanks a lot for your prompt response.

I went ahead and used the REST APIs for my scenario instead. As you said, I found the MQTT Subscribe plugin to work well for scenarios like issuing GCODE commands, but I had a lot of issues to get it to work for simply connecting to the printer.

Cheers!

Hi @jneilliii

Thanks for this! this is exactly what i needed to complete my HA setup.

I am having some trouble however, I have installed MQTT and your plugin, MQTT is wokring as i can see it publishing messages to my broker, however i cannot seem to push the disconnect or connect messages back

{"command": "disconnect"}
AND
 {"command": "connect,"port": "/dev/ttyUSB00","baudrate": AUTO,"printerProfile": "Cocoon_create_maker","save": true,}```

From my broker, NodeRed of MQTT i am trying to issue the command
/octoprint/plugins/mqttsubscribe/connection/2

But i get nothing

This is actually a bug if I remember correctly. I'm currently working with someone else on simplifying this drastically and making it both easier to set-up and configure. Incorporating advanced jsonpath extraction, etc. You may want to take a look over here at the discussion, and if you're brave enough to test you can install the current dev branch using the url below in plugin manager.

https://github.com/jneilliii/OctoPrint-MQTTSubscribe/archive/0.1.2.zip

Happy to work with you on this for sure.

Thanks for the info, glad its not me, spent hours on this today lol!

Installed DEV

So how would one complete this for both connect and disconnect commands

Also, i still only see the debug in my topics?
Imgur

Let's please keep issues in the repository's issue tracker. I see you are already part of the discussion over here. BTW, you can copy your screenshots into the clipboard and then paste them into the github issue tracker comment section in lieu of linking to imgr if you want.

Looks like this plugin has changed and now uses a different topic setup but I just cant get it to publish a connect or disconnect Im trying using mqtt.fx what do I do with the API key?

plugin (API key has been generated)

@jneilliii I raised a similar issue asking for some more doco. Not sure if this is right?

mqtt.fx

OK got this working fo connect and disconnect - not sure if anyones looking but im now looking to issue a load command (think it is select in the API) via MQTT