Introduction
CNC machines running GRBL or Marlin firmware are the workhorses of modern prototyping, small-scale manufacturing, and hobby machining. Yet even the most advanced desktop CNC remains a reactive tool — you send a G-code file, press start, and hope nothing goes wrong. What if your machine could think ahead, monitor its own health, and notify you the moment a tool breaks or the spindle overheats? That’s where ASI Biont comes in. This article is a hands-on guide to connecting a GRBL- or Marlin-based CNC machine to an AI agent, with real code, wiring tips, and automation scenarios that turn your mill into a semi-autonomous production cell.
Why Connect a CNC Machine to an AI Agent?
A standalone CNC controller is essentially a G-code interpreter with motion control. It has no concept of time, temperature, or external events. By integrating with ASI Biont via a COM port (RS-232) through the Hardware Bridge, you give the AI agent the ability to:
- Send and receive G-code commands in real time
- Read spindle temperature, limit switch states, or coolant flow from external sensors attached to an Arduino or ESP32
- Start or stop jobs based on a schedule or a trigger (e.g., material arrived, previous operation finished)
- Detect errors (e.g., stalled motor, missed steps) and automatically stop the machine
- Send you a Telegram message when the job completes or a fault occurs
All of this happens through a single chat conversation — no dashboards, no custom web apps. You describe what you want, and the AI writes the integration code.
Connection Method: COM Port via Hardware Bridge
ASI Biont connects to CNC machines over a serial (RS-232) interface using the Hardware Bridge — a lightweight Python application (bridge.py) that runs on your local PC. The bridge establishes a WebSocket connection to the ASI Biont cloud server. When you issue a command in the chat, the AI uses the industrial_command tool with serial_write_and_read to send data to the bridge, which writes it to the COM port and returns the response.
Why not Wi-Fi or Ethernet? Most GRBL boards (e.g., Arduino Uno with GRBL shield) and Marlin boards (e.g., RAMPS, MKS Gen L) expose a USB-to-serial port. They do not have built-in Ethernet or Wi-Fi without an additional module. The COM port is the universal, low-latency interface that works on any operating system.
Hardware Setup
| Component | Purpose |
|---|---|
| CNC machine with GRBL (v0.9 or v1.1) or Marlin (bugfix-2.0.x or later) | Motion controller |
| USB cable (A-B for Uno, micro USB for MKS) | Connect board to PC |
| PC running Windows / Linux / macOS | Host for bridge.py |
| Optional: Arduino + thermocouple (MAX6675) | Monitor spindle temperature |
| Optional: Relay module | Emergency stop via digital pin |
Wiring Diagram (Optional Spindle Temperature Monitoring)
CNC Board (GRBL) Arduino (Temperature Sensor)
USB Port ----USB----> PC (bridge.py)
Arduino pin 2 <--> MAX6675 SO
Arduino pin 3 <--> MAX6675 CS
Arduino pin 4 <--> MAX6675 SCK
Arduino GND <--> MAX6675 GND
Arduino 5V <--> MAX6675 VCC
Relay Module (E-stop)
Arduino pin 7 <--> Relay IN
Relay COM <--> CNC E-stop loop
The Arduino reads the thermocouple and sends temperature data over its own serial port (e.g., COM5). The bridge.py instance can manage multiple COM ports simultaneously, so you can have one port for CNC commands and another for sensor data.
Step-by-Step Integration
1. Download and Run bridge.py
From the ASI Biont dashboard (Devices → Create API Key → Download bridge), download bridge.py. Install dependencies and run:
pip install pyserial requests websockets
python bridge.py --token=YOUR_API_KEY --ports=COM3,COM5 --baud 115200
This opens COM3 (CNC) and COM5 (Arduino sensor) at 115200 baud. The bridge connects to ASI Biont via WebSocket and waits for commands.
2. Configure the CNC
Ensure your GRBL or Marlin board is configured correctly:
- GRBL: Set baud rate in
config.h(default 115200). Use$$to verify settings like max feed rate and acceleration. - Marlin: In
Configuration.h, set#define BAUDRATE 115200and#define SERIAL_PORT 0.
Connect to the board with a terminal (PuTTY, Arduino Serial Monitor) and test by sending G28 (home all axes). If you get ok back, the serial link works.
3. Tell the AI Agent What to Do
In the ASI Biont chat, you describe your scenario. For example:
"Connect to my CNC on COM3 at 115200 baud. When I say 'start job', send G-code file 'plate.nc' with a 10mm endmill. Monitor spindle temperature from Arduino on COM5. If temperature exceeds 60°C, stop the spindle and send me a Telegram alert."
The AI agent will generate the integration code and execute it. No manual coding required.
Real-World Automation Scenarios
Scenario 1: Scheduled Milling with Temperature Monitoring
Use case: A workshop runs a CNC router for cutting aluminum. The operator wants to start the job at 8:00 AM automatically and monitor spindle temperature to prevent overheating.
How it works: The AI agent uses the serial_write_and_read tool to send G-code commands. It also reads temperature from the Arduino every 30 seconds via a separate serial_write_and_read call. If the temperature exceeds the threshold, the AI calls M5 (spindle off) and sends a Telegram message.
Example Python script (runs in execute_python for scheduling):
import asyncio
import json
async def scheduled_job():
# This script runs in the sandbox; it uses the industrial_command tool via the chat, not direct serial access
# The AI will send commands via industrial_command() in the chat
pass
Note: The actual serial communication happens through the chat using industrial_command(protocol='bridge', command='serial_write_and_read', data='...'). The AI orchestrates this conversation.
Scenario 2: Error Detection and Auto-Stop
Use case: During a long 3D carving job, a bit breaks. The stepper motor stalls, and GRBL enters an alarm state.
How it works: After each G-code line, the AI checks the response. If it receives ALARM:1 (hard limit triggered) or error:9 (G-code lock), the AI immediately sends M0 (pause) and M5 (spindle off), logs the error, and notifies the user.
Bridge command example (AI sends via industrial_command):
industrial_command(
protocol='bridge',
command='serial_write_and_read',
data='4731300a' # hex for "G10\n" (set work offset)
)
The bridge sends G10\n to COM3 and returns the response.
Scenario 3: Telegram Alerts on Job Completion
Use case: An operator runs a batch of parts overnight. They want to know when the last part is done without checking the machine.
How it works: The AI tracks the G-code send progress. When the last line is sent and ok received, it calls the Telegram API (via send_message from the telegram library in execute_python) to send a notification.
Example AI-generated script for notification:
import requests
def send_telegram(bot_token, chat_id, message):
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
data = {"chat_id": chat_id, "text": message}
requests.post(url, data=data)
send_telegram("YOUR_BOT_TOKEN", "YOUR_CHAT_ID", "CNC job finished at 06:34 AM")
Scenario 4: Adaptive Feed Rate Based on Spindle Load
Use case: When cutting variable-density materials (e.g., plywood with knots), the spindle load fluctuates. If load exceeds a threshold, the feed rate should be reduced to prevent breakage.
How it works: An Arduino with a current sensor (ACS712) on the spindle motor sends load data to the AI. The AI parses the value and, if load > 80%, sends F100 (reduce feed) via serial_write_and_read. When load drops below 50%, it restores F300.
Communication flow:
- Arduino sends:
LOAD:75over COM5 - AI reads via:
industrial_command(protocol='bridge', command='serial_write_and_read', data='' , port='COM5') - AI parses the value, decides action, sends:
industrial_command(protocol='bridge', command='serial_write_and_read', data='463330300a', port='COM3')which sendsF300\n
Why ASI Biont Beats Traditional CNC Automation
Traditional CNC automation requires:
- Writing a custom Python/C# program with
pySerialand threading - Setting up a local web server for remote access
- Handling serial port conflicts and error recovery
- Manual deployment of firmware changes
With ASI Biont, you describe the logic in natural language. The AI agent:
- Chooses the correct protocol (bridge → COM)
- Formats G-code and hex data
- Handles timeouts and error responses
- Generates the entire integration in seconds
There is no need to wait for a developer to add a new feature. If you need to read a Modbus register from a PLC and use that value to adjust the CNC feed rate, the AI can create a script that does both – because it has access to pymodbus, pyserial, and any other library in the sandbox.
Connecting Any Device via execute_python
The execute_python method is the universal fallback. If your CNC has an HTTP API (e.g., Duet3D, FluidNC), the AI can write a Python script with aiohttp or requests to send commands. If the machine runs on a Raspberry Pi, the AI can use paramiko to SSH in and run echo "G28" > /dev/ttyAMA0. The sandbox supports over 40 libraries (see the full list in the official documentation).
Example: GRBL with Raspberry Pi via SSH
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.100', username='pi', password='raspberry')
# Send G-code to GRBL via the Pi's UART
stdin, stdout, stderr = ssh.exec_command('echo "G28" > /dev/ttyAMA0')
print(stdout.read().decode())
ssh.close()
The AI writes and executes this script in the sandbox, without you ever touching the terminal.
Practical Considerations
| Challenge | Solution with ASI Biont |
|---|---|
| Serial port conflicts on Windows | bridge.py uses CancelIoEx + PurgeComm for reliable writes |
| Multiple devices on one PC | Single bridge instance can manage multiple ports |
| GRBL alarm recovery | AI detects ALARM: and sends $X to clear, then re-homes |
| Marlin thermal runaway | AI monitors M105 temperature reports and triggers M112 if needed |
Conclusion
Integrating a CNC machine with an AI agent transforms a dumb mill into a smart production node. With ASI Biont and the Hardware Bridge, you gain real-time monitoring, error handling, scheduling, and remote notifications — all configured through a chat conversation. No web dashboards, no custom firmware, no waiting for feature requests. Just describe what you need, and the AI builds the integration for you.
Ready to give your CNC a brain? Try the integration on asibiont.com — start a chat with the AI agent, tell it about your machine, and watch it take control.
Comments