From Serial Monitor to AI Agent: How to Control Any MCU (UART) with ASI Biont

Introduction

For decades, serial communication (UART) has been the backbone of microcontroller interaction. Developers connect an Arduino or ESP32 via USB, open the Arduino IDE Serial Monitor, type commands, and read responses. It works—but it's manual, reactive, and limited to human-in-the-loop control. What if an AI agent could handle the serial conversation for you—reading sensors, triggering actuators, even sending you alerts when thresholds are exceeded—without you writing a single line of integration code?

That's precisely what ASI Biont delivers. By combining its AI agent with a lightweight bridge application, you can connect any MCU (UART) to a cloud AI in seconds. This article walks through a real-world use case: an Arduino Uno with a DHT22 temperature sensor and an LED, controlled entirely through natural language chat with ASI Biont. You'll see the exact wiring, the Arduino sketch, the bridge configuration, and how the AI agent manages the entire data flow—all without a custom dashboard or backend.

Why UART + AI?

UART (Universal Asynchronous Receiver‑Transmitter) is the simplest and most universal protocol for microcontrollers. Every Arduino, ESP32, STM32, and Raspberry Pi Pico exposes one or more UART pins. The protocol is trivial: send bytes, receive bytes. Yet the vast majority of these devices sit disconnected from the cloud, gathering dust because there is no easy way to integrate them into a modern automation pipeline.

ASI Biont fills that gap. The AI agent acts as a smart intermediary: it understands what you want, translates that into the exact bytes to send over the serial line, parses the response, and makes decisions. You don't need to learn pyserial, handle timeouts, or write command parsers. You just describe your goal in plain English.

The Connection Method: Hardware Bridge

ASI Biont does not access your computer's COM ports directly from the cloud—its AI runs on a remote server. To bridge that gap, you run a small Python script called bridge.py on your local machine. The bridge connects to ASI Biont via a single WebSocket (the only communication channel). When the AI agent wants to talk to your MCU, it sends a command through the industrial_command tool, which is routed to your bridge. The bridge writes the bytes to the COM port using pyserial and immediately reads the response.

Key details:
- The bridge uses serial_write_and_read(data=hex_string)—an atomic write‑then‑read operation.
- Data can be passed as a hex string (e.g., "48454c500a" for "HELP\n") or as escape sequences ("BLUE_ON\n").
- On Windows, the bridge handles unreliable writes by calling CancelIoEx, PurgeComm, and falling back to synchronous WriteFile without overlapped I/O.
- You specify the port (e.g., COM3) and baud rate (e.g., 9600).

To start, download bridge.py from the ASI Biont dashboard (Devices → Create API Key → Download bridge). Launch it with:

pip install pyserial requests websockets
python bridge.py --token=YOUR_TOKEN --ports=COM3 --baud 9600

The bridge will automatically discover your device if it responds to the HELP command (more on that below).

Real‑World Use Case: Arduino + DHT22 + LED

Let's build a concrete example. You have:
- An Arduino Uno (or any MCU) connected via USB to COM3.
- A DHT22 temperature/humidity sensor on digital pin 2.
- An LED on digital pin 13 (built‑in) or an external one on pin 9.

You want the AI agent to:
1. Read temperature and humidity every 10 seconds.
2. If temperature exceeds 25°C, turn on the LED (fan indicator).
3. Allow you to manually turn the LED on/off via chat commands.

Step 1: Arduino Firmware with a Simple Command Protocol

The MCU needs a minimal command parser. The bridge expects the device to respond to HELP with a list of commands. Below is a robust Arduino sketch:

#include <DHT.h>

#define DHT_PIN 2
#define DHT_TYPE DHT22
#define LED_PIN 9

DHT dht(DHT_PIN, DHT_TYPE);

String inputBuffer;

void setup() {
  Serial.begin(9600);
  dht.begin();
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    if (c == '\n') {
      String cmd = inputBuffer;
      cmd.trim();
      inputBuffer = "";
      handleCommand(cmd);
    } else {
      inputBuffer += c;
    }
  }
}

void handleCommand(String cmd) {
  if (cmd == "HELP") {
    Serial.println("OK READ_TEMP LED_ON LED_OFF");
  } else if (cmd == "READ_TEMP") {
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    if (isnan(h) || isnan(t)) {
      Serial.println("ERR SENSOR_FAIL");
    } else {
      Serial.print("OK TEMP=");
      Serial.print(t, 1);
      Serial.print(" HUM=");
      Serial.println(h, 1);
    }
  } else if (cmd == "LED_ON") {
    digitalWrite(LED_PIN, HIGH);
    Serial.println("OK LED ON");
  } else if (cmd == "LED_OFF") {
    digitalWrite(LED_PIN, LOW);
    Serial.println("OK LED OFF");
  } else {
    Serial.println("ERR UNKNOWN");
  }
}

Upload this to the Arduino. Open the Serial Monitor (9600 baud) and test: send HELP → should receive OK READ_TEMP LED_ON LED_OFF.

Step 2: Connect ASI Biont to the MCU

With the bridge running, you start a chat with the AI agent (e.g., on the ASI Biont web app). You describe what you have:

"I have an Arduino on COM3 at 9600 baud. It supports commands: HELP, READ_TEMP, LED_ON, LED_OFF. READ_TEMP returns 'OK TEMP=xx.x HUM=yy.y'. I want you to read temperature every 10 seconds, and if it exceeds 25°C, turn on the LED. Also let me manually control the LED by saying 'turn LED on' or 'turn LED off' in the chat."

Step 3: AI Agent Takes Over

The AI agent understands the protocol and the goal. It does the following internally:
- Verifies the connection by sending serial_write_and_read(data="48454c500a") (HELP + newline).
- Parses the response to confirm the command set.
- Sets up a periodic task (using the agent's scheduling capability) to send READ_TEMP every 10 seconds.
- When temperature > 25°C, it sends LED_ON; otherwise, it checks if the LED is on and turns it off with LED_OFF.
- Listens for your manual commands and responds appropriately.

You never write a line of Python or install an MQTT broker. The AI handles all the logic. The bridge silently translates chat‑based industrial_command calls into serial writes.

Step 4: What You See (and Don't See)

In the chat, you might see:

AI: Connection verified. Device reports: OK READ_TEMP LED_ON LED_OFF
AI: Starting periodic monitoring every 10 seconds.
AI: Temperature: 23.4°C, Humidity: 55.2% – LED remains off.
You: turn LED on
AI: Sending LED_ON... Response: OK LED ON. LED is now on.
AI: Temperature: 26.1°C – threshold exceeded! Sending LED_ON.

Behind the scenes, the AI agent is calling industrial_command(protocol='serial://', command='serial_write_and_read', data='...'). This goes to the bridge, which writes and reads. The bridge's WebSocket communication adds less than 100 ms latency (local network), so the loop is near real‑time.

Technical Deep Dive: The Bridge's Serial Handling

For those who want the nitty‑gritty: The bridge's _parse_data_field() function converts your command string to bytes. It accepts both hex strings and plain escape sequences. For example:
- data="48454c500a" becomes bytes [0x48, 0x45, 0x4c, 0x50, 0x0a] — "HELP\n".
- data="LED_ON\n" becomes bytes [0x4c, 0x45, 0x44, 0x5f, 0x4f, 0x4e, 0x0a].

The bridge opens the COM port with pyserial using the specified baud rate, 8 data bits, no parity, 1 stop bit (8N1). It sets a small timeout (e.g., 1 second) for reads. After each write, it reads until a newline or a timeout, then returns the response to the AI.

On Windows, pyserial sometimes fails to write when overlapped I/O generates spurious errors. The bridge compensates by: calling CancelIoEx to abort any pending operations, issuing PurgeComm to clear both transmit and receive buffers, then performing a synchronous WriteFile call. This ensures the data always reaches the MCU.

Beyond UART: ASI Biont Connects to Any MCU via execute_python

While the UART + bridge approach is optimal for direct serial connections, ASI Biont's AI can also write Python scripts that connect to microcontrollers via other protocols—all without you leaving the chat. For instance:

  • ESP32 via MQTT: The AI writes a paho-mqtt script that subscribes to esp32/temperature, analyzes the data, and publishes to esp32/led.
  • Raspberry Pi via SSH: The AI uses paramiko to SSH into the Pi, run a script that reads GPIO pins, and return the values.
  • ESP32 via CoAP: The AI uses aiocoap to GET sensor readings from a constrained device.
  • Any device with an HTTP API: The AI writes an aiohttp script to poll a smart plug and control its relay.

All these scripts run inside ASI Biont's sandbox (cloud) on Railway, with a 30‑second timeout—no infinite loops allowed. The AI generates the code, executes it, and returns the result to you. You just need to provide the connection parameters (IP, port, credentials) as part of your conversation.

Why This Matters: No‑Code Industrial IoT

The traditional path to integrating a microcontroller with an AI or automation system is long: write a custom Python script, handle serial errors, build a web dashboard, maintain a server. ASI Biont collapses this to a single chat prompt. The AI agent:
- Understands your hardware (you describe the commands).
- Writes the integration code on the fly.
- Manages the bridge connection.
- Makes decisions based on sensor data.
- Responds to your natural language commands.

There are no dashboards to configure, no 'add device' buttons. The entire integration is conversational. And because the bridge supports HELP protocol discovery, you can even ask the AI to connect to an unknown device first—it will send HELP and learn the command set automatically.

Conclusion: Try It Yourself

UART is everywhere—from hobbyist Arduino projects to industrial PLCs. Connecting it to an AI agent opens up possibilities that were previously the domain of professional DevOps teams: automatic data logging, predictive alerts, voice or chat control, and cloud‑based decision making.

With ASI Biont, you can go from a bare MCU to a fully AI‑managed device in under five minutes. Download bridge.py from your dashboard, start a chat, and tell the AI what you want. No coding required.

Ready to give your microcontroller superpowers? Visit asibiont.com and start your first integration today.

← All posts

Comments