Make Your CNC Machine Talk to an AI: Integrating GRBL and Marlin with ASI Biont

Introduction

A CNC machine running GRBL or Marlin is a precision tool that turns digital designs into physical objects. But in a modern workshop, it’s often an island — you walk over, plug in a USB cable, open a G-code sender, press start, and watch. What if your CNC could respond to a voice command, alert you when the spindle temperature spikes, or automatically pause if a vibration sensor detects a bit about to break? That’s where ASI Biont comes in.

ASI Biont is an AI agent that connects to any device through chat-based conversations. You describe your setup, and the AI writes the integration code on the fly — no dashboard, no “add device” button, no waiting for a developer. For CNC machines with GRBL or Marlin firmware, the primary connection method is the COM port (RS-232) via the Hardware Bridge. This article is a practical guide to linking your CNC to ASI Biont, with real code, wiring diagrams, and automation scenarios for a home workshop or small production line.

Why Connect a CNC Machine to an AI Agent?

A standalone CNC is powerful but blind. You have to be physically present to monitor it, and you can’t respond to anomalies mid-job unless you’re watching the screen. By connecting it to ASI Biont, you gain:

  • Remote monitoring: check spindle RPM, feed rate, and axis positions from your phone.
  • Voice or chat control: send a message like “start the engraving job on file ‘logo.nc’” without touching the PC.
  • Safety automation: if a temperature sensor reports the spindle is overheating, the AI can send an emergency stop (M112) immediately.
  • Batch production logic: run a sequence of G-code files, pause between them for part inspection, and resume — all coordinated by the AI.

Connection Method: COM Port via Hardware Bridge

GRBL and Marlin communicate over a serial (COM) port — typically USB-to-serial. ASI Biont does not have direct access to your local COM ports because it runs in the cloud. Instead, you run a lightweight application called bridge.py on your workshop PC (Windows, Linux, or macOS). This bridge connects to ASI Biont via a secure WebSocket (the only communication channel) and acts as a proxy to your COM port.

The AI sends commands using the industrial_command tool with the serial_write_and_read operation. The bridge receives the command, writes a string (converted from hex) to the COM port, and immediately reads the response. This atomic operation is perfect for the request-response protocol of GRBL/Marlin.

How to Set Up bridge.py

  1. Download bridge.py: from the ASI Biont dashboard under Devices → Create API Key → Download bridge. Do not search GitHub — the only official source is the dashboard.
  2. Install dependencies: pip install pyserial requests websockets
  3. Run the bridge:
    bash python bridge.py --token=YOUR_API_TOKEN --ports=COM3 --baud 115200
    Replace COM3 with your CNC’s actual port (on Linux it might be /dev/ttyUSB0). The baud rate for most GRBL setups is 115200; for Marlin it’s often 250000.

Once the bridge is running, you can talk to ASI Biont in the chat interface.

What You Can Automate: Real-World Scenarios

Scenario 1: Remote Status Check

You are away from the workshop and want to know if your CNC is idle or running. Just ask the AI: "Check the CNC status."

The AI sends a serial command ? (GRBL status report request). The bridge writes ? (hex 3F) to the COM port and reads the response — something like `<Idle

|MPos:0.000,0.000,0.000|Bf:15,3|FS:0,0>`. The AI parses this and replies to you in natural language.

Example chat exchange:
- You: "Is my CNC idle?"
- AI: "Let me check." (sends industrial_command)
- AI: "Your CNC is idle at position (0,0,0). Spindle is off. Feed rate 0."

Scenario 2: Start a Job by Voice or Text

You want to start a pre-loaded G-code file (e.g., logo.nc) without walking over to the PC. You tell the AI: "Run the engraving job logo.nc."

The AI first sends $X to unlock the steppers (if GRBL is in alarm state), then sends G90 to set absolute positioning, then M3 S12000 to start the spindle at 12,000 RPM, and finally streams the G-code file line by line. Because the bridge supports serial_write_and_read, the AI can send each line, wait for ok, and send the next — exactly like a traditional G-code sender.

Python code that the AI generates and executes (in the sandbox) for streaming:

# This script runs inside ASI Biont sandbox (execute_python)
# It does NOT have direct COM access — it instructs the bridge via industrial_command
# The AI will call industrial_command for each line

# Example: sending a single G-code command via chat
# The AI does this automatically when you say "spindle on"

Actually, the AI doesn’t need to write a script for simple commands — it uses industrial_command directly. For complex streaming, the AI may generate a script that loops through a file and calls industrial_command multiple times within the 30-second sandbox timeout.

Scenario 3: Emergency Stop on Temperature Spike

Imagine you have a thermocouple attached to your spindle, read by an Arduino that sends data via a second COM port (or MQTT). You tell the AI: "If spindle temperature exceeds 70°C, execute an emergency stop."

The AI writes a Python script that subscribes to the temperature topic via MQTT (using paho-mqtt) and checks the value. If above 70°C, it calls industrial_command to send M112 (emergency stop) to the CNC’s COM port. The bridge writes M112 and the machine halts.

MQTT + CNC integration script (AI-generated):

import paho.mqtt.client as mqtt
import time
from asibiont import industrial_command  # conceptual

def on_message(client, userdata, msg):
    temp = float(msg.payload.decode())
    if temp > 70.0:
        # Send emergency stop to CNC via bridge
        industrial_command(
            protocol='serial',
            command='serial_write_and_read',
            params={'data': '4d3131320a'},  # hex for "M112\n"
            bridge_token='YOUR_TOKEN'
        )
        print("Emergency stop sent!")

client = mqtt.Client()
client.on_message = on_message
client.connect("192.168.1.100", 1883)
client.subscribe("workshop/spindle/temperature")
client.loop_forever()

Note: In the real ASI Biont, industrial_command is a tool the AI calls from chat, not a Python library. The AI generates the MQTT subscription script and uses chat tools to trigger the CNC stop.

Scenario 4: Automatic Tool Change Assistant

On a manual tool-change CNC, the AI can guide you step by step. For example, after finishing a roughing pass, the AI sends M5 (spindle off), G0 Z50 (raise Z), and then asks you: "Please change to a 6mm endmill and type 'done'." Once you confirm, it sends the next G-code block.

Wiring Diagram: Connecting CNC to bridge.py

Below is a simple ASCII diagram of the typical setup:

[CNC Controller (GRBL on Arduino)]
  |
  | USB cable (virtual COM port)
  |
[Workshop PC (Windows/Linux)]
  |
  |-- runs bridge.py (WebSocket to ASI Biont cloud)
  |-- COM port COM3 (115200 baud)
  |
[ASI Biont AI Agent (cloud)]
  |
  |-- receives chat messages from you
  |-- sends industrial_command via WebSocket to bridge
  |-- bridge writes to COM3, reads response, sends back

No additional hardware is needed — just your CNC, a PC, and an internet connection. If you want to add sensors (spindle temp, vibration), connect them via an ESP32 or Arduino with a second COM port or MQTT.

Code Examples for Common CNC Interactions

Example 1: Unlock GRBL from Alarm State

# AI sends this via industrial_command
industrial_command(
    protocol='serial',
    command='serial_write_and_read',
    params={'data': '24580a'},  # hex for "$X\n"
    port='COM3',
    baud=115200
)
# Response: "ok" or "error:..."

Example 2: Query Current Position

industrial_command(
    protocol='serial',
    command='serial_write_and_read',
    params={'data': '3f0a'},  # hex for "?\n"
    port='COM3',
    baud=115200
)
# Response: "<Idle

|MPos:10.5,20.3,0.0|Bf:15,3|FS:0,0>"

Example 3: Start Spindle and Move to Position

# Send multiple commands sequentially
industrial_command(protocol='serial', command='serial_write_and_read', params={'data': '4d330a'})  # "M3\n"
industrial_command(protocol='serial', command='serial_write_and_read', params={'data': '4730300a'})  # "G00\n"

Why This Beats Traditional G-Code Senders

Traditional senders (Candle, UGS, bCNC) require you to be at the PC. With ASI Biont, you control the CNC from anywhere via chat. The AI also adds intelligence:
- It can check if a G-code file is safe before running (e.g., no excessive Z-negative moves).
- It can log all commands and responses for later analysis.
- It can integrate with other systems: send a Slack message when a job finishes, or start a coolant pump via a smart plug when the spindle starts.

Advanced: Integrating Multiple CNC Machines

In a small production shop with three CNC routers, you can run one bridge.py per machine (each with a different COM port). The AI can manage all three: e.g., "Start the job on CNC3, but pause CNC1 because the bit is dull." The AI keeps context — it knows which bridge token corresponds to which machine.

Safety First: AI-Controlled Emergency Stops

GRBL and Marlin both support M112 (emergency stop). The AI can send this instantly, but you should always have a physical E-stop button as the primary safety mechanism. The AI-driven stop is a secondary, remote safety feature.

Conclusion: From Manual Control to AI-Powered Workshop

Connecting your CNC machine to ASI Biont transforms it from a standalone tool into a connected, intelligent asset. You don’t need to write a single line of integration code — just describe your setup in the chat, and the AI generates the connection, tests it, and begins controlling your machine.

The bridge.py application handles all the COM port complexity, while the AI handles the logic, error handling, and multi-device orchestration. Whether you’re a hobbyist running a Shapeoko or a small business with a Tormach, this integration saves you time, adds remote control, and opens up new automation possibilities.

Ready to make your CNC smarter? Go to asibiont.com, create an API key, download bridge.py, and start chatting with your machine.

← All posts

Comments