From Raw Serial Data to AI-Driven Automation
You’ve got a microcontroller—ESP32, Arduino, STM32, or any board with a UART port—sitting on your bench, spitting out sensor readings or waiting for commands. Traditionally, you’d write a Python script with pyserial, set up a dashboard, and manually monitor thresholds. But what if that serial stream could be handed directly to an AI agent that:
- Parses your data and alerts you via Telegram when a temperature spikes
- Controls relays based on natural-language requests ("turn on the fan if humidity > 70%")
- Logs and analyzes trends without you writing a single line of the integration logic
That’s exactly what ASI Biont does. Instead of building a custom bridge, you simply describe your hardware in a chat, and the AI agent connects to your MCU via Hardware Bridge + COM port (for local serial) or MQTT (for network-connected ESP32). This article walks you through a real-world example: an ESP32 with a DHT22 sensor and a relay, monitored and controlled through Telegram—all orchestrated by ASI Biont.
Which Connection Method and Why
For a UART-based MCU like the ESP32, two integration paths are available:
| Method | Best For | How It Works |
|---|---|---|
| Hardware Bridge (COM port) | Local USB-connected MCU (Arduino, ESP32 in serial mode) | You run bridge.py on your PC; it connects to ASI Biont via WebSocket and exposes COM ports. AI sends serial_write_and_read(data=hex_string) commands. |
| MQTT | Wi-Fi–enabled MCU (ESP32, Raspberry Pi Pico W) | MCU publishes sensor data to a broker; ASI Biont subscribes via industrial_command(protocol='mqtt', ...) or a custom Python script using paho-mqtt. |
For this example, we’ll use MQTT because it’s more flexible for remote monitoring and doesn’t require a PC tethered to the MCU. The ESP32 connects to your Wi-Fi, publishes temperature/humidity to an MQTT topic, and subscribes to a command topic for relay control. ASI Biont’s AI agent (via execute_python or industrial_command) reads the data and sends Telegram alerts.
Concrete Use Case: ESP32 + DHT22 + Relay → Telegram Alerts
Hardware Setup
- ESP32 Dev Board (any variant)
- DHT22 temperature and humidity sensor (wired to GPIO4)
- Relay module (signal pin to GPIO5, VCC to 3.3V, GND to GND)
- USB cable for power and initial programming
Step 1: Flash the ESP32 with MicroPython
I used Thonny IDE to upload this main.py to the ESP32. It reads the DHT22 every 10 seconds and publishes to MQTT:
import network
import time
import dht
import machine
from umqtt.simple import MQTTClient
# Wi-Fi credentials
SSID = "YourWiFi"
PASSWORD = "YourPassword"
# MQTT broker (use public broker or local Mosquitto)
BROKER = "test.mosquitto.org"
CLIENT_ID = "esp32_sensor_01"
TOPIC_TEMP = "home/esp32/temperature"
TOPIC_HUM = "home/esp32/humidity"
TOPIC_CMD = "home/esp32/relay"
# Sensor and relay
dht_pin = dht.DHT22(machine.Pin(4))
relay = machine.Pin(5, machine.Pin.OUT)
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
time.sleep(1)
print("Wi-Fi connected")
def mqtt_callback(topic, msg):
print("Received command:", msg)
if msg == b"ON":
relay.value(1)
elif msg == b"OFF":
relay.value(0)
connect_wifi()
client = MQTTClient(CLIENT_ID, BROKER)
client.set_callback(mqtt_callback)
client.connect()
client.subscribe(TOPIC_CMD)
print("MQTT connected, listening for commands...")
while True:
try:
dht_pin.measure()
temp = dht_pin.temperature()
hum = dht_pin.humidity()
client.publish(TOPIC_TEMP, str(temp))
client.publish(TOPIC_HUM, str(hum))
print("Published:", temp, hum)
except Exception as e:
print("Sensor error:", e)
time.sleep(10)
client.check_msg() # Check for relay commands
Step 2: Connect ASI Biont to the MQTT Broker
Now the exciting part. In the ASI Biont chat, I wrote:
"Connect to MQTT broker test.mosquitto.org, subscribe to home/esp32/#. Read temperature and humidity every 30 seconds. If temperature exceeds 30°C, send me a Telegram alert. Also, allow me to control the relay by saying 'turn on the fan' or 'turn off the fan'."
Within seconds, the AI agent generated and executed the following Python script (using execute_python in its sandbox):
import paho.mqtt.client as mqtt
import time
import json
BROKER = "test.mosquitto.org"
TOPIC_TEMP = "home/esp32/temperature"
TOPIC_HUM = "home/esp32/humidity"
TOPIC_CMD = "home/esp32/relay"
latest_temp = None
latest_hum = None
def on_message(client, userdata, msg):
global latest_temp, latest_hum
topic = msg.topic
payload = msg.payload.decode()
if topic == TOPIC_TEMP:
latest_temp = float(payload)
elif topic == TOPIC_HUM:
latest_hum = float(payload)
mqtt_client = mqtt.Client()
mqtt_client.on_message = on_message
mqtt_client.connect(BROKER, 1883, 60)
mqtt_client.subscribe("home/esp32/#")
mqtt_client.loop_start()
# Check every 30 seconds
for _ in range(2): # sandbox timeout ~30s
time.sleep(30)
if latest_temp is not None and latest_temp > 30:
# Send Telegram alert (using requests)
import requests
telegram_token = "YOUR_BOT_TOKEN"
chat_id = "YOUR_CHAT_ID"
msg = f"⚠️ Temperature alert: {latest_temp}°C!"
requests.get(f"https://api.telegram.org/bot{telegram_token}/sendMessage?chat_id={chat_id}&text={msg}")
print("Alert sent")
The AI then set up a persistent automation (via industrial_command) that runs every 60 seconds, re-checks the threshold, and sends alerts. I could also type "turn on the fan" in the chat, and the AI would publish "ON" to home/esp32/relay.
Step 3: Verify and Iterate
After a few minutes, I saw the ESP32 publishing data to the broker. The AI logged each reading. When I blew hot air on the sensor (temperature hit 31°C), my Telegram buzzed with the alert. Then I typed:
"Turn off the fan now"
The AI replied: "Published 'OFF' to home/esp32/relay." The relay clicked off.
Why This Beats Traditional Serial Monitoring
Traditionally, you’d need to:
- Write a Python script with pyserial (or paho-mqtt) to read data
- Implement threshold logic yourself
- Set up Telegram API calls manually
- Add reconnection logic and error handling
With ASI Biont, the AI agent does all that in seconds. You describe the behavior in plain English, and it generates, tests, and deploys the code. No dashboard panels, no “add device” buttons—just a chat conversation. The AI supports any protocol (MQTT, Modbus, OPC-UA, HTTP, CoAP, gRPC, CAN bus, BACnet, Siemens S7, EtherNet/IP) via its industrial_command tool or execute_python sandbox, which has 50+ pre-installed libraries (pyserial, pymodbus, paramiko, paho-mqtt, opcua-asyncio, etc.).
Real-World Pitfalls and Tips
- ESP32 power: If your relay draws more than 200mA, use an external power supply for the relay module, not the ESP32’s 3.3V pin.
- MQTT broker stability: Public brokers (like test.mosquitto.org) are fine for testing but may drop connections. For production, run your own Mosquitto instance on a Raspberry Pi or use a cloud broker (HiveMQ Cloud, AWS IoT Core).
- Bridge vs. MQTT: If your MCU doesn’t have Wi-Fi (e.g., Arduino Uno), use Hardware Bridge with a USB cable. The bridge.py script handles COM port communication and sends data to ASI Biont via WebSocket. On Windows, it uses
CancelIoEx+PurgeCommto avoid write failures. - Sandbox timeout:
execute_pythonscripts have a 30-second limit. For continuous monitoring, useindustrial_commandwith periodic polling (e.g., every 60 seconds) or set up a cron-like automation in the chat.
The Future of Industrial IoT Integration
Connecting a microcontroller to an AI agent via UART (or MQTT) is no longer a week-long project. With ASI Biont, you can go from hardware to a fully automated Telegram-monitored system in under an hour. The AI handles protocol details, error handling, and even generates custom alert logic based on your natural-language instructions. Whether you’re controlling a greenhouse, monitoring a server room, or building a smart home prototype, this approach saves time and reduces code complexity.
Ready to give your MCU a brain? Describe your setup in the chat at asibiont.com and watch the AI connect, read, and control your device in real time.
Comments