Why Connect Smart City Sensors to an AI Agent?
Urban environments are drowning in data. A single smart city sensor array — measuring PM2.5, CO₂, noise levels, ambient light, and temperature — can pump out thousands of readings per hour. But raw data is useless without action. That’s where an AI agent like ASI Biont changes the game. Instead of staring at dashboards, you get a proactive assistant that monitors, analyzes, and responds in real time.
ASI Biont connects to your sensor network using the same protocols your hardware already speaks: MQTT, Modbus/TCP, COM ports, or HTTP APIs. The result? A no-code integration that turns your sensor data into Telegram alerts, automated HVAC adjustments, or live Grafana dashboards — all through a simple chat conversation.
Which Connection Method Works Best for Smart City Sensors?
Most smart city deployments use one of two topologies:
| Sensor Type | Typical Interface | Recommended ASI Biont Method |
|---|---|---|
| Air quality (PMS5003, SDS011) | UART → ESP32 → MQTT | MQTT via paho-mqtt in execute_python |
| Noise (ICS-43434) | I²S → ESP32 → MQTT | MQTT via paho-mqtt |
| Light (BH1750) | I²C → ESP32 → MQTT | MQTT via paho-mqtt |
| Temperature/humidity (DHT22) | GPIO → ESP32 → MQTT | MQTT via paho-mqtt |
| Legacy industrial sensors | RS-485 Modbus RTU | Hardware Bridge + industrial_command with modbus_rtu |
For this guide, we’ll focus on MQTT because it’s the universal glue for IoT. Your ESP32 publishes sensor readings to a broker (e.g., Mosquitto), and ASI Biont subscribes to that topic via a Python script that runs in its sandbox.
Real Use Case: ESP32 + DHT22 + BH1750 → Telegram Alerts
Step 1: Hardware Setup
- ESP32 DevKit (any variant)
- DHT22 temperature/humidity sensor
- BH1750 light intensity sensor
- Breadboard and jumper wires
Wiring:
| ESP32 Pin | DHT22 | BH1750 |
|---|---|---|
| 3.3V | VCC | VCC |
| GND | GND | GND |
| GPIO4 | DATA | — |
| GPIO21 (SDA) | — | SDA |
| GPIO22 (SCL) | — | SCL |
Step 2: ESP32 Firmware (MicroPython)
Flash this code to your ESP32 using Thonny or esptool. It reads sensors every 30 seconds and publishes JSON to MQTT.
import network
import time
import json
from machine import Pin, I2C
import dht
from umqtt.simple import MQTTClient
# Wi-Fi credentials
WIFI_SSID = "YourWiFi"
WIFI_PASS = "YourPassword"
# MQTT broker
MQTT_BROKER = "broker.hivemq.com"
MQTT_TOPIC = b"city/sensor/esp32_01"
# Connect Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASS)
while not wlan.isconnected():
time.sleep(0.5)
# Sensor init
dht_sensor = dht.DHT22(Pin(4))
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# BH1750 address = 0x23
def read_light():
data = i2c.readfrom(0x23, 2)
return ((data[0] << 8) | data[1]) / 1.2
client = MQTTClient("esp32_01", MQTT_BROKER)
client.connect()
while True:
dht_sensor.measure()
payload = {
"temp_c": dht_sensor.temperature(),
"humidity": dht_sensor.humidity(),
"light_lux": read_light(),
"timestamp": time.time()
}
client.publish(MQTT_TOPIC, json.dumps(payload))
time.sleep(30)
Step 3: Connect ASI Biont via Chat
Open a chat with your ASI Biont AI agent. Describe what you need:
“Subscribe to MQTT topic
city/sensor/esp32_01on brokerbroker.hivemq.com. Parse the JSON payload and send me a Telegram alert if temperature exceeds 35°C or light level drops below 10 lux. Also log all readings to a local CSV file every hour.”
The AI will generate and execute a Python script using paho-mqtt inside execute_python. Here’s what it produces:
import paho.mqtt.client as mqtt
import json
import csv
from datetime import datetime
import os
TELEGRAM_BOT_TOKEN = "your_bot_token"
TELEGRAM_CHAT_ID = "your_chat_id"
CSV_FILE = "sensor_log.csv"
def on_message(client, userdata, msg):
data = json.loads(msg.payload.decode())
temp = data.get("temp_c")
light = data.get("light_lux")
# Alert logic
if temp and temp > 35:
requests.post(f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
json={"chat_id": TELEGRAM_CHAT_ID, "text": f"🔥 High temp: {temp}°C"})
if light and light < 10:
requests.post(f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
json={"chat_id": TELEGRAM_CHAT_ID, "text": f"🌑 Low light: {light} lux"})
# Log to CSV
with open(CSV_FILE, "a", newline="") as f:
writer = csv.writer(f)
writer.writerow([datetime.now(), temp, data.get("humidity"), light])
mqtt_client = mqtt.Client()
mqtt_client.on_message = on_message
mqtt_client.connect("broker.hivemq.com", 1883, 60)
mqtt_client.subscribe("city/sensor/esp32_01")
mqtt_client.loop_forever()
Important: The sandbox has a 30-second timeout, so for persistent subscriptions, the AI uses a background task or schedules periodic checks instead of loop_forever(). In practice, the AI will set up a cron-like loop that polls MQTT every 30 seconds.
Step 4: Automate Control Actions
You can also ask the AI to trigger actuators. For example:
“If noise level on topic
city/sensor/noise_01exceeds 70 dB for more than 5 minutes, publish{"cmd": "close_window"}to topiccity/actuator/window_01.”
The AI writes a second script that subscribes to the noise topic, counts consecutive high readings, and publishes a command when threshold is met.
Why ASI Biont Beats Traditional Dashboards
- Zero code from you: Describe what you want in plain English. The AI writes the integration Python script using
paho-mqtt,requests, or any of the 50+ sandbox libraries. - Connect anything: MQTT, Modbus TCP, OPC-UA, SSH, serial, HTTP API — ASI Biont supports them all via
execute_pythonorindustrial_command. - Real-time alerts: Push notifications to Telegram, Slack, email — no need to stare at a dashboard.
- Self-healing logic: The AI can monitor its own scripts and restart them if they crash.
Pitfalls to Avoid
- Don’t use infinite loops in
execute_python— the sandbox kills scripts after 30 seconds. Instead, use a polling loop withtime.sleep(30)or set up a scheduled task. - MQTT QoS matters — for critical alerts, use QoS 1 or 2. The AI defaults to 0 unless you specify otherwise.
- Secure your broker — if using a public broker like HiveMQ, any client can publish. Consider a password-protected broker for production.
- Watch the CSV size — logging every 30 seconds creates ~2,880 rows/day. The AI can rotate files or push to a database like PostgreSQL (supported via
psycopg2in sandbox).
Conclusion
Smart city sensors are only as smart as the actions they trigger. By connecting them to ASI Biont, you get an AI agent that monitors, alerts, and controls your urban environment — all through a chat conversation. No need to wait for a developer to write custom glue code. Describe your integration, and the AI does the rest.
Ready to make your city smarter? Try the integration today at asibiont.com — start a chat and say: “Connect my ESP32 with DHT22 to MQTT and alert me on Telegram when temperature is too high.”
Comments