From Microcontroller to Smart Home: How to Integrate ESP32 with the ASI Biont AI Agent

Introduction

The ESP32 is a low-cost, low-power microcontroller with built-in Wi-Fi and Bluetooth, making it the go-to choice for IoT projects — from temperature sensors and motion detectors to smart lighting and motor controls. But the real power of an ESP32 emerges when you connect it to an AI agent that can understand natural language, make decisions, and automate responses.

ASI Biont is an AI agent designed to integrate with any physical device or industrial system through multiple protocols — MQTT, Modbus, SSH, serial, HTTP APIs, and more. Instead of writing hundreds of lines of integration code yourself, you simply describe your device and what you want it to do in a chat. The AI agent selects the right protocol, writes the Python code, and executes it — all within seconds.

In this article, we'll walk through how to connect an ESP32 to ASI Biont, covering real connection methods, concrete code examples, and automation scenarios like controlling lights, reading temperature, and sending alerts via Telegram.

Why Connect ESP32 to an AI Agent?

A standalone ESP32 can read a sensor and toggle a relay, but it lacks the ability to:
- Understand user intent from natural language
- Combine data from multiple sources (weather API, calendar, other sensors)
- Learn from historical patterns and predict failures
- Send alerts through messaging apps like Telegram or Slack

By integrating with ASI Biont, your ESP32 becomes part of an intelligent ecosystem. The AI agent can:
- Parse sensor data, detect anomalies, and trigger actions
- Accept voice or text commands to control actuators
- Generate reports and visualizations automatically
- Adapt automation rules without firmware updates

Connection Methods Available for ESP32

ASI Biont supports multiple ways to communicate with an ESP32. The choice depends on how your ESP32 sends data:

Method When to Use How AI Connects
MQTT ESP32 publishes sensor data to a broker (Mosquitto, HiveMQ) AI writes a Python script using paho-mqtt inside execute_python to subscribe and publish
Serial via Hardware Bridge ESP32 is connected via USB to your PC (COM port) User runs bridge.py on PC; AI sends commands via industrial_command with protocol serial://
HTTP API ESP32 runs a web server (e.g., ESPAsyncWebServer) AI writes an aiohttp script inside execute_python to send GET/POST requests
SSH ESP32 runs a lightweight SSH server (rare, but possible with ESP-IDF) AI uses paramiko inside execute_python to execute commands remotely
CoAP ESP32 uses CoAP for constrained IoT communication AI uses industrial_command with protocol coap://

Most common for ESP32: MQTT or Serial via Hardware Bridge.

Concrete Use Case: ESP32 with DHT22 + MQTT → ASI Biont → Telegram Alerts

Scenario

You have an ESP32 with a DHT22 temperature/humidity sensor that publishes readings every 30 seconds to an MQTT broker (Mosquitto running on a Raspberry Pi). You want ASI Biont to:
- Subscribe to the topic esp32/dht22
- Read temperature and humidity
- If temperature exceeds 30°C, send a Telegram alert to your phone
- Log all readings to a CSV file for later analysis

Step 1: ESP32 Firmware (Arduino IDE)

Your ESP32 code publishes JSON data via MQTT:

#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* mqtt_server = "192.168.1.100";

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(4, DHT22);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  client.setServer(mqtt_server, 1883);
  dht.begin();
}

void loop() {
  if (!client.connected()) client.connect("ESP32DHT");
  client.loop();
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (!isnan(t) && !isnan(h)) {
    String payload = "{\"temp\":" + String(t) + ",\"hum\":" + String(h) + "}";
    client.publish("esp32/dht22", payload.c_str());
  }
  delay(30000);
}

Step 2: Tell ASI Biont to Connect

In the chat on asibiont.com, you write:

Connect to MQTT broker at 192.168.1.100:1883, subscribe to topic "esp32/dht22". Parse JSON with temperature and humidity. If temperature > 30°C, send a Telegram message to my chat ID 123456789 using bot token YOUR_BOT_TOKEN. Also log all readings to a CSV file named sensor_log.csv.

Step 3: AI Generates and Executes the Python Script

ASI Biont uses execute_python to run a sandboxed Python script. It will generate something like:

import paho.mqtt.client as mqtt
import json
import csv
from datetime import datetime
import os

BROKER = "192.168.1.100"
PORT = 1883
TOPIC = "esp32/dht22"
TELEGRAM_BOT_TOKEN = "YOUR_BOT_TOKEN"
TELEGRAM_CHAT_ID = "123456789"
CSV_FILE = "sensor_log.csv"

def on_message(client, userdata, msg):
    try:
        data = json.loads(msg.payload.decode())
        temp = data["temp"]
        hum = data["hum"]
        timestamp = datetime.now().isoformat()
        # Log to CSV
        with open(CSV_FILE, "a", newline="") as f:
            writer = csv.writer(f)
            writer.writerow([timestamp, temp, hum])
        # Alert if temperature > 30
        if temp > 30:
            import requests
            text = f"⚠️ Alert: Temperature {temp}°C exceeded 30°C at {timestamp}"
            url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
            requests.post(url, json={"chat_id": TELEGRAM_CHAT_ID, "text": text})
        print(f"Logged: {temp}°C, {hum}%")
    except Exception as e:
        print(f"Error: {e}")

client = mqtt.Client()
client.on_message = on_message
client.connect(BROKER, PORT, 60)
client.subscribe(TOPIC)
client.loop_forever()

The AI runs this script in the sandbox. It connects to your broker, subscribes, and begins processing data in real time.

Step 4: Automation Runs

Now every 30 seconds, when a new MQTT message arrives, the AI:
1. Parses the JSON
2. Appends a row to sensor_log.csv
3. Checks the temperature threshold
4. If exceeded, sends a Telegram alert

You can also ask the AI in the chat: "Show me the latest 10 readings" — it will read the CSV and present them in a table.

Alternative Connection: ESP32 via Serial (Hardware Bridge)

If your ESP32 is connected to your PC via USB and prints sensor data to the Serial Monitor, you can use the Hardware Bridge method.

Step 1: Download and Run bridge.py

From the ASI Biont dashboard (Devices → Create API Key → Download bridge), you get bridge.py. Run it on your PC:

pip install pyserial requests websockets
python bridge.py --token=YOUR_API_KEY --ports=COM3 --baud 115200

The bridge connects to ASI Biont via WebSocket and opens COM3 at 115200 baud.

Step 2: Chat with AI

Tell the AI:

Read from serial port COM3 at 115200 baud. The device sends lines like "TEMP:23.5 HUM:60". Parse them. If temperature > 30, send a Telegram alert.

The AI uses industrial_command(protocol='serial://', command='serial_write_and_read', data='HELP\n') to verify the connection, then continuously reads and parses data.

Other ESP32 Scenarios with ASI Biont

Scenario Connection Method What AI Does
Smart light control MQTT User says "turn on the living room light" → AI publishes cmnd/living_light/POWER ON to MQTT broker; ESP32 subscribed to that topic toggles relay
Motion detector + camera MQTT + HTTP ESP32 with PIR sensor publishes motion event; AI receives it, then sends HTTP request to an IP camera to take a snapshot, saves image, sends to Telegram
Climate control MQTT AI reads temperature/humidity from ESP32, compares with setpoint, publishes commands to an IR blaster (ESP32) to turn AC on/off
Data logger with analytics Serial bridge ESP32 sends sensor readings every minute; AI stores them in a database (DuckDB), calculates daily averages, and generates a weekly report
Voice-controlled blinds MQTT User speaks to Telegram bot; AI interprets intent, publishes MQTT command to ESP32 controlling a servo motor

Why It Matters: No Manual Coding Required

With ASI Biont, you don't need to write a single line of integration code. The AI agent:
- Selects the correct protocol (MQTT, serial, HTTP, etc.)
- Generates a working Python script using libraries like paho-mqtt, pyserial, aiohttp, or paramiko
- Executes it in the sandbox environment
- Adapts to your device's data format (JSON, CSV, plain text)

You can change the logic anytime by just describing what you want. For example:

"Instead of Telegram, now send alerts to Slack channel #iot-alerts."

The AI rewrites the script, replaces the notification library, and continues running.

Conclusion

ESP32 is a versatile microcontroller that becomes truly powerful when paired with an AI agent like ASI Biont. Whether you want to monitor temperature, control lights, automate climate, or detect motion, the integration takes only minutes — not days.

Try it yourself: Go to asibiont.com, create an account, connect your ESP32 via MQTT or serial bridge, and describe your automation scenario in the chat. The AI will handle the rest — no coding, no waiting for firmware updates, no complex dashboards.

Start building your AI-powered smart home today.

← All posts

Comments