Your ESP32 is a powerful IoT workhorse — Wi-Fi, Bluetooth, dual-core processor, plenty of GPIOs. But managing multiple sensors, controlling relays, and handling alerts often means writing custom firmware, setting up dashboards, and wrestling with MQTT brokers. What if you could skip all that and let an AI agent handle the logic? That's exactly what ASI Biont does: it connects to your ESP32 via MQTT or HTTP, reads data, executes commands, and sends Telegram notifications — all through natural language chat.
Why Connect ESP32 to an AI Agent?
Instead of hardcoding thresholds and alerts in C++ or MicroPython, you describe what you want in plain English: "Turn on the relay if temperature exceeds 30°C and send me a Telegram message." The AI writes the integration code on the fly, using your existing MQTT broker or HTTP endpoints. No dashboard configuration, no firmware updates — just a conversation.
Connection Method: MQTT via execute_python
ASI Biont connects to ESP32 through MQTT using the paho-mqtt library. The AI runs a Python script in its sandbox environment (execute_python) that subscribes to a topic (e.g., esp32/sensor) and publishes commands to another topic (e.g., esp32/relay). For quick one-shot commands, the AI can also use the industrial_command tool with protocol='mqtt'. But for continuous monitoring and complex logic, execute_python is the way to go.
Why MQTT? It's lightweight, widely supported on ESP32 (with libraries like PubSubClient or umqtt.simple), and works over unreliable networks. The broker can run locally on a Raspberry Pi or in the cloud (HiveMQ, Mosquitto).
Real-World Scenario: Temperature Monitoring + Relay Control + Telegram Alerts
Here's a concrete setup: an ESP32 with a DHT22 sensor and a relay module. The ESP32 publishes temperature and humidity to esp32/dht. ASI Biont subscribes to that topic, analyzes trends, and publishes to esp32/relay to switch a fan or heater. If values exceed a threshold, the AI sends an alert via Telegram.
ESP32 Side (MicroPython):
import network
import time
from machine import Pin
import dht
from umqtt.simple import MQTTClient
# Wi-Fi and MQTT config
WIFI_SSID = "YourWiFi"
WIFI_PASS = "YourPass"
MQTT_BROKER = "192.168.1.100" # local broker or HiveMQ cloud
CLIENT_ID = "esp32_sensor"
PUB_TOPIC = b"esp32/dht"
SUB_TOPIC = b"esp32/relay"
# Setup sensor and relay
dht_pin = Pin(4, Pin.IN)
sensor = dht.DHT22(dht_pin)
relay = Pin(5, Pin.OUT)
relay.value(0)
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASS)
while not wlan.isconnected():
time.sleep(1)
print("WiFi connected")
def mqtt_callback(topic, msg):
if msg == b"ON":
relay.value(1)
print("Relay ON")
elif msg == b"OFF":
relay.value(0)
print("Relay OFF")
connect_wifi()
client = MQTTClient(CLIENT_ID, MQTT_BROKER)
client.set_callback(mqtt_callback)
client.connect()
client.subscribe(SUB_TOPIC)
print("Subscribed to relay commands")
while True:
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
payload = f"{{\"temp\":{temp},\"hum\":{hum}}}"
client.publish(PUB_TOPIC, payload)
print(f"Published: {payload}")
client.check_msg() # non-blocking check for relay commands
except Exception as e:
print("Error:", e)
time.sleep(5)
ASI Biont Side (AI writes this script in execute_python):
import paho.mqtt.client as mqtt
import time
BROKER = "192.168.1.100"
SENSOR_TOPIC = "esp32/dht"
RELAY_TOPIC = "esp32/relay"
def on_message(client, userdata, msg):
import json
data = json.loads(msg.payload)
temp = data['temp']
hum = data['hum']
print(f"Received: temp={temp}, hum={hum}")
if temp > 30:
print("Temperature above 30°C — turning relay ON and alerting")
client.publish(RELAY_TOPIC, "ON")
# Telegram alert code omitted for brevity
else:
client.publish(RELAY_TOPIC, "OFF")
client = mqtt.Client()
client.on_message = on_message
client.connect(BROKER, 1883, 60)
client.subscribe(SENSOR_TOPIC)
client.loop_start()
time.sleep(30) # sandbox runs for 30 seconds max
Note: The AI sandbox has a 30-second timeout, so for long-running monitoring, you'd use the industrial_command tool with publish command or set up a persistent bridge. But for prototyping and one-off tasks, execute_python is perfect.
How to Connect: Describe, Don't Code
You don't need to write any of this code yourself. In the ASI Biont chat, just say:
"Connect to my ESP32 via MQTT at 192.168.1.100. Subscribe to esp32/dht. If temperature exceeds 30°C, publish ON to esp32/relay and send me a Telegram alert."
The AI will generate the Python script, run it in the sandbox, and start monitoring. You can also ask the AI to check the current relay state or manually toggle it.
Why This Matters
- Zero coding: The AI handles MQTT subscriptions, JSON parsing, conditional logic, and Telegram API calls.
- Flexible: Change thresholds, add new sensors, or switch to HTTP in seconds — just describe the new behavior.
- No vendor lock-in: Use any MQTT broker, any ESP32 firmware. ASI Biont adapts to your existing setup.
Real-World Scenario: Smart Plant Watering
An ESP32 with a soil moisture sensor and a water pump relay. The ESP32 publishes moisture readings to esp32/soil. ASI Biont subscribes, and if moisture drops below 20%, it publishes to esp32/pump to water the plant for 5 seconds. It also logs watering events to a Google Sheet (via API) and sends a daily summary to Telegram.
Wiring Diagram (Simplified)
| ESP32 Pin | Component |
|---|---|
| GPIO4 | DHT22 Data |
| GPIO5 | Relay IN |
| GPIO16 | Soil Sensor AOUT |
| 3.3V | DHT22 VCC |
| GND | Common Ground |
The relay controls a 12V pump via an external power supply. The DHT22 and soil sensor are powered from the ESP32's 3.3V rail.
Conclusion
Integrating an ESP32 with ASI Biont turns your microcontroller into a conversational IoT node. No need to learn MQTT libraries or write boilerplate — just explain what you want, and the AI connects, monitors, and controls. Whether you're building a smart home, a greenhouse monitor, or an industrial alert system, this approach saves hours of development time.
Try it yourself: connect your ESP32 to ASI Biont at asibiont.com and start automating with AI.
Comments