How to Integrate a 3D Printer (Marlin, Klipper) with the ASI Biont AI Agent: A Practical Guide to MQTT and COM Port Automation

How to Integrate a 3D Printer (Marlin, Klipper) with the ASI Biont AI Agent

Imagine your 3D printer not just sitting there waiting for a file to be sliced and sent, but actively communicating with an AI that monitors temperatures, detects failures before they happen, and even starts a print based on a calendar event—all through a simple chat interface. This is not science fiction. With the ASI Biont platform, you can connect any 3D printer running Marlin or Klipper firmware to an AI agent in minutes, without writing a single line of code from scratch.

In this article, we will walk through a real-world scenario: connecting a 3D printer to ASI Biont via MQTT (for Klipper) and via COM port through Hardware Bridge (for Marlin). We will show you how the AI agent monitors temperatures, controls print start/stop, and sends you alerts—all through a conversation in the chat. Let’s dive in.

Why Connect a 3D Printer to an AI Agent?

A standalone 3D printer is a powerful tool, but it lacks awareness. It cannot predict a clog, notify you when a print finishes, or automatically pause if the bed temperature drifts. By integrating with an AI agent, you gain:

  • Remote monitoring – check nozzle temperature, bed temperature, and print progress from anywhere.
  • Automated actions – start a print on a schedule, pause on error, or resume after power loss.
  • Intelligent alerts – get a Telegram message when the print is 90% complete or if the temperature exceeds a safe threshold.
  • No-code integration – instead of writing a full integration stack, you simply describe your setup in chat, and the AI writes the Python code for you.

Connection Methods: Marlin vs Klipper

There are two main firmware families for 3D printers, and each requires a slightly different connection approach:

Firmware Connection Method Protocol Recommended ASI Biont Tool
Marlin COM port (USB) Serial G-code industrial_command with serial:// via Hardware Bridge
Klipper MQTT (via Moonraker plugin) JSON-RPC over MQTT execute_python with paho-mqtt

For Marlin, the printer is connected via USB to a computer (Windows, Linux, or macOS). The computer runs the Hardware Bridge (bridge.py), which opens a serial connection to the printer and communicates with the ASI Biont cloud via WebSocket. The AI agent sends G-code commands (e.g., M105 to read temperatures) and receives responses.

For Klipper, the printer is usually controlled by a Raspberry Pi running Moonraker. Moonraker can be configured to publish printer status to an MQTT broker. The AI agent subscribes to that broker, reads the data, and can publish commands back (e.g., to start or stop a print).

Step-by-Step: Marlin + Hardware Bridge

1. Hardware Setup

  • A 3D printer with Marlin firmware (e.g., Creality Ender 3, Prusa i3).
  • A computer connected to the printer via USB.
  • Python 3.8+ installed on that computer.

2. Download and Run Hardware Bridge

From the ASI Biont dashboard (Devices → Create API Key → Download bridge), you get bridge.py. Install dependencies:

pip install pyserial requests websockets

Run the bridge, specifying the COM port (e.g., COM3 on Windows, /dev/ttyUSB0 on Linux) and baud rate (usually 115200 for Marlin):

python bridge.py --token=YOUR_API_KEY --ports=COM3 --baud=115200 --rate=10

The bridge connects to ASI Biont via WebSocket and waits for commands. You will see a log message like Bridge connected to wss://api.asibiont.com/ws.

3. Connect in Chat

Now open a chat with the ASI Biont AI agent. Describe your setup:

"I have a Marlin 3D printer connected on COM3 at 115200 baud. Monitor the nozzle temperature every 10 seconds. If the temperature exceeds 250°C, send me an alert."

The AI agent will use industrial_command with protocol serial:// to send G-code commands. For example, to read the current temperature:

industrial_command(
    protocol='serial://',
    command='serial_write_and_read',
    params={'data': '4d3130350a'}  # hex for "M105\n"
)

The bridge sends M105 to the printer, reads the response (e.g., ok T:210.0 /230.0 B:60.0 /70.0), and returns it to the AI. The AI parses the response and decides if an alert is needed.

4. Full Automation Scenario

You can ask the AI to set up a complete scenario:

"Every morning at 8:00 AM, preheat the bed to 60°C and nozzle to 200°C. Then start printing the file 'calibration_cube.gcode' from the SD card. If the print fails, notify me via Telegram."

The AI will create a script using execute_python that runs on a schedule (using a cron-like mechanism or a simple loop with sleep, respecting the 30-second sandbox limit—for long-running tasks, the script will set up a schedule by writing to a database). It will use industrial_command to send G-code for preheating (M104 S200, M140 S60) and starting the print (M23 calibration_cube.gcode, M24).

Step-by-Step: Klipper + MQTT

1. Hardware Setup

  • A 3D printer with Klipper firmware and Moonraker.
  • A Raspberry Pi (or similar) running Moonraker and an MQTT broker (e.g., Mosquitto).

2. Configure Moonraker MQTT Plugin

In moonraker.conf, add:

[mqtt]
address: localhost
port: 1883
username: printer_user
password: secure_pass
default_qos: 1
status_objects: toolhead, heater_bed, extruder

Restart Moonraker. Now the printer publishes JSON status messages to topics like printer/{printer_name}/status.

3. Connect via execute_python

In the ASI Biont chat, describe your MQTT setup:

"Connect to MQTT broker at 192.168.1.100:1883, subscribe to topic 'printer/ender3/status', and log the bed temperature. If the bed temperature drops below 50°C during a print, pause the print."

The AI writes a Python script using paho-mqtt and runs it via execute_python. Here is a simplified example of the kind of code the AI might generate:

import paho.mqtt.client as mqtt
import json

def on_message(client, userdata, msg):
    payload = json.loads(msg.payload.decode())
    # payload contains e.g. {"toolhead": {"homed_axes": "xyz"}, "extruder": {"temperature": 210.0, "target": 230.0}}
    bed_temp = payload.get("heater_bed", {}).get("temperature", 0)
    if bed_temp < 50:
        # Pause print via Moonraker HTTP API
        import requests
        requests.post("http://192.168.1.100:7125/printer/print/pause")
        print("Bed temperature too low, print paused.")

client = mqtt.Client()
client.username_pw_set("printer_user", "secure_pass")
client.connect("192.168.1.100", 1883, 60)
client.subscribe("printer/ender3/status")
client.on_message = on_message
client.loop_start()

Important: The script will run in the cloud sandbox. It cannot run indefinitely (30-second limit). For continuous monitoring, the AI will set up a schedule or use the industrial_command MQTT publish mechanism for quick commands. For long-term monitoring, you would run a persistent script on your local network (e.g., on the Raspberry Pi) that communicates with ASI Biont via webhooks or a separate bridge.

4. Control Print Start/Stop via Chat

You can also control the printer directly from chat:

"Start printing 'vase.gcode' on the Klipper printer."

The AI will publish a command to Moonraker's MQTT topic (e.g., printer/ender3/command) with a JSON payload like:

{"command": "start_print", "filename": "vase.gcode"}

Moonraker receives it and begins the print. No manual slicing or SD card handling needed.

Why ASI Biont Eliminates Manual Coding

Traditional integration of a 3D printer with an AI or automation system requires:

  1. Writing a serial communication library (pyserial).
  2. Parsing G-code responses.
  3. Setting up an MQTT client.
  4. Writing alerting logic.
  5. Handling errors and reconnections.

With ASI Biont, you bypass all of that. The AI agent has built-in knowledge of Marlin G-code (e.g., M105 for temperature, M104 for nozzle target, M140 for bed target) and Klipper's Moonraker API. You simply describe your goal in natural language, and the AI writes the Python code using pyserial, paho-mqtt, requests, or other sandbox libraries. The code runs either locally (via Hardware Bridge) or in the cloud (via execute_python).

Real-World Use Cases

1. Remote Print Queue Management

You can ask the AI to maintain a print queue. For example:

"When the current print finishes, wait 5 minutes for cooling, then start the next file from a list in a Google Sheet."

The AI reads the sheet via requests (Google Sheets API), monitors printer status via MQTT, and triggers the next print automatically.

2. Predictive Maintenance

The AI can log temperatures over time and detect anomalies:

"Monitor the nozzle temperature variation during prints. If the standard deviation exceeds 3°C over a 10-minute window, warn me of a possible thermistor failure."

The AI collects data via repeated MQTT subscriptions, computes statistics using numpy, and sends alerts via Telegram (using the send_message tool or a custom HTTP call to Telegram API).

3. Integration with Home Assistant

If you have Home Assistant running MQTT, you can bridge your printer data:

"Publish printer status to Home Assistant MQTT topic 'homeassistant/sensor/printer/temperature' every 30 seconds."

The AI writes a script that subscribes to the printer's MQTT topic and republishes to Home Assistant's topic.

Conclusion

Integrating a 3D printer with an AI agent like ASI Biont unlocks a new level of automation and monitoring. Whether you are using Marlin via a USB serial connection or Klipper via MQTT, the process is straightforward: describe your hardware and goals in a chat, and the AI handles the rest. No more writing boilerplate serial code or debugging MQTT connection issues—the AI does it for you in seconds.

Ready to transform your 3D printing workflow? Head over to asibiont.com, create your account, and start a chat with the AI agent. Tell it what printer you have, and watch it connect and control your device instantly.

← All posts

Comments