From Any MCU to AI: How to Connect UART Devices to ASI Biont (ESP32 Example)

If you've ever wanted to let an AI agent talk directly to your microcontroller via UART—without writing a single line of boilerplate—this guide is for you. I’ll show you how ASI Biont connects to any MCU over a COM port using the Hardware Bridge, with a real-world example: an ESP32 reading a DHT22 temperature sensor and controlling an LED via Telegram.

Why Connect a UART Device to an AI Agent?

UART (Universal Asynchronous Receiver/Transmitter) is the simplest serial protocol—two wires, no clock, no complex stack. Every MCU (ESP32, Arduino, STM32) has at least one UART. By connecting it to ASI Biont, you get:
- Remote control: Send commands from anywhere via chat (Telegram, web).
- Automated monitoring: AI reads sensor data, logs trends, and alerts you when values exceed thresholds.
- Zero manual integration: You just describe what you want—AI writes the code and handles the connection.

How ASI Biont Connects to UART Devices

ASI Biont uses Hardware Bridge—a small Python script (bridge.py) you run on your local PC (Windows/Linux/macOS). The bridge connects to the cloud via WebSocket (the only communication channel). You specify the COM port (e.g., COM3) and baud rate (e.g., 115200). Then, in the chat, you tell the AI agent what to do. The AI sends commands through the industrial_command tool, which routes them to your bridge. The bridge writes bytes to the UART and reads the response.

Key points:
- Data is passed as hex strings (e.g., "48454c500a" for HELP+\n).
- The bridge uses pyserial for communication.
- On Windows, if a write fails, the bridge automatically calls CancelIoEx + PurgeComm + synchronous WriteFile for reliability.
- To verify a connection, send HELP—the device should respond with a list of supported commands.

Real-World Example: ESP32 + DHT22 + LED via Telegram

Scenario: You have an ESP32 with a DHT22 temperature/humidity sensor and an LED on GPIO2. You want to:
- Read temperature and humidity every 5 minutes.
- Turn the LED on/off via Telegram.
- Get an alert if temperature exceeds 30°C.

Step 1: Hardware Setup
- Connect DHT22 data pin to ESP32 GPIO4 (with 10kΩ pull-up).
- Connect LED anode to GPIO2 (with 220Ω resistor), cathode to GND.
- Connect ESP32 UART0 (TX=GPIO1, RX=GPIO3) to your PC via USB-serial converter (e.g., CP2102).

Step 2: ESP32 Firmware (C++/Arduino)
Upload this minimal sketch that responds to commands:

#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
#define LEDPIN 2
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  pinMode(LEDPIN, OUTPUT);
  dht.begin();
}

void loop() {
  if (Serial.available()) {
    String cmd = Serial.readStringUntil('\n');
    cmd.trim();
    if (cmd == "HELP") {
      Serial.println("HELP\nTEMP?\nHUM?\nLED_ON\nLED_OFF");
    } else if (cmd == "TEMP?") {
      float t = dht.readTemperature();
      Serial.println(String("TEMP:") + t);
    } else if (cmd == "HUM?") {
      float h = dht.readHumidity();
      Serial.println(String("HUM:") + h);
    } else if (cmd == "LED_ON") {
      digitalWrite(LEDPIN, HIGH);
      Serial.println("LED:ON");
    } else if (cmd == "LED_OFF") {
      digitalWrite(LEDPIN, LOW);
      Serial.println("LED:OFF");
    } else {
      Serial.println("ERROR: Unknown command");
    }
  }
}

Step 3: Set Up Hardware Bridge
1. Download bridge.py from the ASI Biont dashboard (Devices → Create API Key → Download bridge).
2. Install dependencies: pip install pyserial requests websockets.
3. Run the bridge:

python bridge.py --token=YOUR_API_TOKEN --ports=COM3 --baud=115200

The bridge connects to ASI Biont and listens for commands.

Step 4: Command the AI in Chat
In the ASI Biont chat, type:

"Connect to my ESP32 on COM3 at 115200 baud. Read temperature and humidity every 5 minutes. Turn LED on when temperature > 30°C. Send me a Telegram alert. Also allow me to turn LED on/off via Telegram commands."

The AI agent:
- Verifies the connection by sending HELP and parsing the response.
- Creates a Python script that runs in the sandbox (via execute_python) with a loop using paho-mqtt or aiohttp to poll data and send Telegram messages.
- Configures industrial_command for on-demand LED control.

Step 5: Result
- You receive a Telegram message: "ESP32 connected. Current temp: 25.3°C, humidity: 60%."
- Every 5 minutes, a new reading arrives.
- When temp hits 31°C, you get: "Alert! Temperature 31.2°C. LED turned ON."
- You reply: "Turn LED off" → AI sends LED_OFF via serial, and you get confirmation.

Pitfalls to Avoid

  1. Wrong baud rate: Double-check the ESP32 firmware baud (115200). If mismatch, bridge will report timeout.
  2. Hex vs. plain text: The bridge’s _parse_data_field() accepts both. Use HELP\n for readability, or hex 48454c500a for binary data.
  3. Windows overlapped I/O: If you see written: 0 errors, the bridge automatically applies CancelIoEx + PurgeComm + synchronous WriteFile. But ensure your USB-serial driver is up to date.
  4. Buffer overflow: ESP32 serial buffer is 256 bytes by default. If you send long commands, increase SERIAL_RX_BUFFER_SIZE in Arduino IDE.
  5. Sandbox timeout: execute_python scripts have a 30-second timeout. Don't write while True loops—use asyncio.sleep and handle timeouts.

Why This Matters

You don’t need to write complex server code or learn MQTT. Just plug your MCU into a USB port, run one Python script, and let the AI handle the rest. The same approach works for:
- Arduino Nano reading analog sensors
- STM32 controlling motors
- GPS modules sending NMEA sentences
- Any device with a serial port

ASI Biont connects to any device through execute_python—the AI writes custom integration code on the fly. No waiting for SDK updates. Describe your setup in chat, and the AI does the rest.

Try It Yourself

Go to asibiont.com, create an API key, download the bridge, and connect your MCU in under 10 minutes. Start with a simple LED blink—then scale to full sensor networks.

← All posts

Comments