Control OctoPrint via MQTT


#1

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.


#2

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.


#3

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.


#4

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.


#5

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


#6

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?


#7

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.


#8

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


#9

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.