Why Connect Your MQTT Broker to an AI Agent?
If you run a smart home, industrial IoT setup, or a fleet of ESP32 sensors, you already know MQTT is the backbone for lightweight, real-time messaging. But manually monitoring every topic, setting up alert rules, and reacting to anomalies is tedious. That's where ASI Biont changes the game. Instead of writing complex Node-RED flows or cron jobs, you simply describe in chat: "Connect to my Mosquitto broker at 192.168.1.100:1883, subscribe to sensors/temperature, and alert me on Telegram if it exceeds 35°C." The AI agent handles the rest.
How ASI Biont Connects to MQTT Brokers
ASI Biont uses paho-mqtt inside its execute_python sandbox (a secure cloud environment). You provide the broker IP, port, and optional credentials. The AI writes a Python script that subscribes to topics, processes incoming messages, and publishes commands when needed. For quick one-off actions (e.g., publish a command to a smart plug), you can also use the industrial_command tool with the publish command.
Key Libraries Used
- paho-mqtt — MQTT client (subscribe, publish, callbacks)
- json — parse payloads
- asyncio — for non-blocking loops (though sandbox timeout is 30s, so long-lived subscriptions run via
industrial_command)
Concrete Example: ESP32 Temperature Sensor → Mosquitto → Telegram Alerts
Scenario: You have an ESP32 with a DHT22 sensor publishing temperature to sensors/temperature every 30 seconds. You want the AI to monitor it and send a Telegram message when temp > 35°C.
Step-by-Step
- Set up ESP32 code (MicroPython or Arduino) to publish MQTT messages. Example snippet:
```python
import time, ujson, network
from umqtt.simple import MQTTClient
def read_temp():
# read DHT22
return 28.5
client = MQTTClient("esp32", "192.168.1.100")
client.connect()
while True:
temp = read_temp()
client.publish(b"sensors/temperature", ujson.dumps({"temp": temp}).encode())
time.sleep(30)
```
-
In ASI Biont chat, tell the AI:
"Connect to MQTT broker at 192.168.1.100:1883, subscribe to sensors/temperature, parse the JSON payload, and if temperature exceeds 35°C, send a Telegram alert to @myuser."
-
AI generates and runs this script (simplified):
```python
import paho.mqtt.client as mqtt
import json
import requests
TELEGRAM_BOT_TOKEN = "your_bot_token"
TELEGRAM_CHAT_ID = "@myuser"
def on_message(client, userdata, msg):
data = json.loads(msg.payload.decode())
temp = data.get("temp", 0)
if temp > 35:
requests.post(
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
json={"chat_id": TELEGRAM_CHAT_ID, "text": f"⚠️ Temperature alert: {temp}°C"}
)
client = mqtt.Client()
client.on_message = on_message
client.connect("192.168.1.100", 1883, 60)
client.subscribe("sensors/temperature")
client.loop_forever() # runs until timeout (30s)
```
- AI keeps the script running in the sandbox (with timeout handling) and reports any alerts to you in chat. You can also ask: "What's the average temperature over the last 5 messages?" and the AI will analyze the data.
Beyond Monitoring: Control via Chat
MQTT isn't just for reading data — you can also control devices. For example:
- Smart plug: Publish to cmnd/plug1/POWER with payload ON when temperature exceeds a threshold.
- LED strip: Publish color commands to led/strip/set.
The AI handles this with the same paho-mqtt client. Just say: "If temperature > 35°C, turn off the AC by publishing {'state':'off'} to ac/command. "
Why This Matters for IoT Automation
- Zero manual coding: You don't write the integration — the AI does it from your description.
- Flexible: Change topics, thresholds, or actions instantly via chat.
- No additional infrastructure: The AI runs in the cloud; you only need a running MQTT broker.
Pitfalls to Avoid
- Sandbox timeout: Long-running subscriptions (like
loop_forever) will be killed after 30 seconds. For continuous monitoring, useindustrial_commandwith thepublishcommand in a loop (the AI manages this). - Credentials: Never hardcode tokens in chat. Use environment variables or ask the AI to read them from
os.environ. - Payload format: Ensure your ESP32 publishes JSON. The AI can handle plain text, but JSON is easier to parse.
Real-World Use Cases
| Scenario | Broker | Topic | AI Action |
|---|---|---|---|
| Industrial temperature monitoring | Mosquitto | factory/oven1/temp |
Log to Google Sheets if >200°C |
| Smart home energy management | EMQX | home/power/total |
Turn off non-essential loads via MQTT when >5kW |
| Fleet GPS tracking | Mosquitto | vehicle/+/gps |
Plot route on map and send ETA via Telegram |
Conclusion
MQTT is the universal language of IoT, and ASI Biont makes it conversational. No more juggling Node-RED flows or writing custom bridges. Describe your integration in plain English, and the AI handles the rest. Ready to connect your Mosquitto or EMQX broker?
👉 Try it now at asibiont.com — just tell the AI your broker details and what you want to do.
Comments