ESP32 Meets AI: Integrate Your Microcontroller with ASI Biont for Voice-Controlled IoT Automation

ESP32 Meets AI: Integrate Your Microcontroller with ASI Biont for Voice-Controlled IoT Automation

Introduction

The ESP32 is the Swiss Army knife of microcontrollers: built-in Wi-Fi, Bluetooth, dual-core processor, and enough GPIO to handle sensors, LEDs, motors, and more. For years, controlling an ESP32 meant writing Custom firmware, setting up dashboards, or wrestling with Node-RED flows. What if you could simply talk to your ESP32 and tell it what to do?

Enter ASI Biont — an AI agent that integrates with hardware through natural language. You describe your device, and the AI writes the integration code on the fly. No need to manually code MQTT clients or serial parsers. This article shows you how to connect an ESP32 to ASI Biont using MQTT, COM port via Hardware Bridge, and the universal execute_python sandbox. You’ll learn to monitor environmental sensors, control LEDs, and set up intelligent alerts — all through a chat interface.

Why Connect ESP32 to an AI Agent?

Traditional IoT setups require you to:
- Write firmware for the MCU (C++/Arduino/MicroPython)
- Build a backend server to handle data
- Create a dashboard or mobile app
- Implement logic for decision-making (thresholds, timers, etc.)

ASI Biont collapses these steps into one conversation. The AI understands your intent (e.g., “turn on the LED when temperature exceeds 30°C”) and automatically generates the Python code that runs in its cloud sandbox or through the Hardware Bridge on your PC. The only thing you do on the ESP32 side is connect it to your network and send/receive messages (e.g., via MQTT or serial).

Connection Methods Available for ESP32

Method Use Case How ASI Biont Connects Code Execution Location
MQTT Cloud-friendly, pub/sub, best for Wi-Fi ESP32 AI writes Python script (paho-mqtt) in execute_python; subscribes to topics, publishes commands ASI Biont cloud sandbox (Railway)
COM Port (RS-232) Direct serial connection via USB, e.g., for debug or low-latency User runs bridge.py on PC; AI uses industrial_command(protocol='serial://', ...) User’s PC (Hardware Bridge)
HTTP API ESP32 runs a simple web server AI writes Python script with aiohttp or requests to call ESP32's REST endpoints ASI Biont cloud sandbox
WebSocket Real-time bidirectional AI writes Python script using websockets library ASI Biont cloud sandbox
Modbus/TCP Industrial ESP32 (e.g., ESP32-PLC) AI uses industrial_command(protocol='modbustcp', ...) ASI Biont cloud (Direct)

For most hobbyist projects, MQTT is the simplest and most reliable. Let’s walk through a concrete example.

Real Scenario: Temperature Monitoring with DHT22 and MQTT

Parts List

  • ESP32 development board (e.g., ESP32-DevKitC)
  • DHT22 temperature/humidity sensor
  • LED with resistor (220Ω)
  • Breadboard and jumper wires
  • Mosquitto MQTT broker (free public or self-hosted)

Step 1: Flash MicroPython Firmware to ESP32

First, you need MicroPython on your ESP32. Download the latest firmware from micropython.org/download/ESP32_GENERIC/ and flash it using esptool:

# Erase flash
esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash

# Flash MicroPython
esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 esp32-20220618-v1.19.1.bin

Step 2: ESP32 MicroPython Code (Upload via ampy or Thonny)

Create main.py on the ESP32 that connects to Wi-Fi and MQTT, reads DHT22 every 5 seconds, publishes to topic esp32/temperature and esp32/humidity, and listens on esp32/led for commands.

import network
import time
import machine
from umqtt.simple import MQTTClient
import dht

# Wi-Fi credentials (store securely – here for demo)
WIFI_SSID = "YourWiFi"
WIFI_PASS = "YourPassword"

# MQTT broker (public test broker – replace with your own)
MQTT_BROKER = "test.mosquitto.org"
MQTT_PORT = 1883
CLIENT_ID = "esp32_dht"
TOPIC_TEMP = b"esp32/temperature"
TOPIC_HUM = b"esp32/humidity"
TOPIC_LED = b"esp32/led"

# Hardware setup
led = machine.Pin(2, machine.Pin.OUT)  # Onboard LED
sensor = dht.DHT22(machine.Pin(4))

def connect_wifi():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('Connecting to WiFi...')
        wlan.connect(WIFI_SSID, WIFI_PASS)
        while not wlan.isconnected():
            time.sleep(1)
    print('WiFi connected:', wlan.ifconfig())

def mqtt_callback(topic, msg):
    print('Received:', topic, msg)
    if topic == TOPIC_LED:
        if msg == b"ON":
            led.on()
            print('LED ON')
        elif msg == b"OFF":
            led.off()
            print('LED OFF')

def main():
    connect_wifi()
    client = MQTTClient(CLIENT_ID, MQTT_BROKER, port=MQTT_PORT)
    client.set_callback(mqtt_callback)
    client.connect()
    client.subscribe(TOPIC_LED)
    print('Connected to MQTT broker')

    while True:
        try:
            sensor.measure()
            temp = sensor.temperature()
            hum = sensor.humidity()
            client.publish(TOPIC_TEMP, str(temp).encode())
            client.publish(TOPIC_HUM, str(hum).encode())
            print('Published temp={}, hum={}'.format(temp, hum))
            client.check_msg()  # Check for LED commands
            time.sleep(5)
        except OSError as e:
            print('Sensor read error:', e)
            time.sleep(2)

if __name__ == '__main__':
    main()

Upload this to your ESP32 (e.g., using ampy --port /dev/ttyUSB0 put main.py). The board will now publish sensor data every 5 seconds to esp32/temperature and esp32/humidity, and listen for ON/OFF messages on esp32/led.

Step 3: Connect ASI Biont to the MQTT Broker

Now, open a chat with ASI Biont (e.g., on asibiont.com). Describe what you want in plain English:

“Connect to MQTT broker test.mosquitto.org:1883, subscribe to topic esp32/temperature and esp32/humidity, and monitor the values. If temperature exceeds 30°C, publish ‘ON’ to esp32/led to turn on the LED. Also, whenever humidity drops below 40%, send me a chat message with a warning.”

The AI will generate and execute a Python script in its sandbox. Here’s an example of what the AI might produce (simplified):

import paho.mqtt.client as mqtt
import time
import json

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("esp32/temperature")
    client.subscribe("esp32/humidity")

def on_message(client, userdata, msg):
    topic = msg.topic
    payload = msg.payload.decode()
    print(f"Received {topic}: {payload}")

    if topic == "esp32/temperature":
        temp = float(payload)
        if temp > 30:
            client.publish("esp32/led", "ON")
            print("Temperature >30°C, LED turned ON")
            # Send alert to user (simulate chat message)
            print("ALERT: High temperature detected!")
    elif topic == "esp32/humidity":
        hum = float(payload)
        if hum < 40:
            print("ALERT: Low humidity!")

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
client.loop_forever()  # Note: ASI Biont sandbox will limit run time; for persistent monitoring use a scheduled task

Important: The sandbox timeout is 30 seconds, so long-running loops are not allowed. For continuous monitoring, ASI Biont can be configured to run this script periodically (e.g., via cron-like scheduling in the AI’s execution environment). But for a quick test, the AI can run a one-shot version that reads a few messages and exits.

Step 4: Interact Through Chat

You can now command your ESP32 from the same chat:

  • “Turn on the LED” → AI publishes ON to esp32/led.
  • “What is the current temperature?” → AI reads the last published value from its own logs or by subscribing temporarily.
  • “Set a schedule: turn on the LED every hour for 10 seconds” → AI creates a timer-based script (using time.sleep and loop, but limited by sandbox).

The beauty is that you don’t need to write any MQTT client code – you just talk.

Alternative Connection: COM Port via Hardware Bridge

If your ESP32 is connected via USB (e.g., for programming or a serial terminal), you can have ASI Biont talk directly to it through the Hardware Bridge. This is useful for real-time control without network dependency.

Setup on ESP32 (Arduino Sketch)

Upload a simple serial sketch to ESP32 that interprets text commands:

void setup() {
  Serial.begin(115200);
  pinMode(2, OUTPUT); // Built-in LED
}

void loop() {
  if (Serial.available()) {
    String cmd = Serial.readStringUntil('\n');
    cmd.trim();
    if (cmd.equalsIgnoreCase("LED ON")) {
      digitalWrite(2, HIGH);
      Serial.println("LED ON");
    } else if (cmd.equalsIgnoreCase("LED OFF")) {
      digitalWrite(2, LOW);
      Serial.println("LED OFF");
    } else if (cmd.equalsIgnoreCase("HELP")) {
      Serial.println("Commands: LED ON, LED OFF, HELP");
    } else {
      Serial.println("Unknown command");
    }
  }
}

Run Hardware Bridge on PC

Download bridge.py from the ASI Biont dashboard (Devices → Create API Key → Download bridge). Launch:

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

Now, in the ASI Biont chat, you can send:

“Send command ‘LED ON’ to ESP32 on serial port.”

The AI will use industrial_command(protocol='serial://', command='serial_write_and_read', data='4c4544204f4e0a') (hex for "LED ON\n"). The bridge writes the command and returns the response, which the AI displays in the chat.

Advanced: Universal execute_python for Any Custom Integration

ASI Biont’s execute_python sandbox contains dozens of pre-installed libraries (requests, httpx, paho-mqtt, pymodbus, paramiko, etc.). For example, if your ESP32 runs a custom HTTP server, just tell the AI:

“Connect to ESP32 running HTTP server at 192.168.1.100, send GET /temp to read temperature, and if it’s > 30°C, send POST /led with body ‘ON’.”

The AI will generate a script using requests and execute it. No need to pre-define any dashboards – the code is generated on the fly.

Why This Approach Changes IoT Development

  • Zero coding for the backend: The AI writes and runs the integration logic.
  • Natural language interface: You don’t need a custom dashboard; the chat becomes your control panel.
  • Adaptable: If you change the ESP32 firmware to use a different protocol, just tell the AI to switch to CoAP or WebSocket.
  • Real-time feedback: The AI can notify you in chat when thresholds are crossed.

Conclusion

ESP32 is an incredibly versatile microcontroller, and ASI Biont makes it truly intelligent. Whether you use MQTT, serial, or HTTP, the AI agent connects in seconds and lets you control and monitor your hardware through simple conversation. No more writing boilerplate integration code – just describe what you want, and the AI does the rest.

Ready to give your ESP32 a brain? Head over to asibiont.com and start chatting with your microcontroller today.

← All posts

Comments