Integrate Environmental Sensors with ASI Biont: ESP32, MQTT, and AI Automation

Environmental sensors—temperature, humidity, CO₂, air quality (PM2.5, VOC)—are the nervous system of any smart building or industrial facility. But raw data is useless without intelligent analysis and action. ASI Biont, the AI agent that connects to any device via natural language, turns sensor streams into automated decisions: adjust HVAC, send Telegram alerts, or log anomalies. This guide shows how to connect an ESP32 with a BME280 sensor (temperature, humidity, pressure) and an SGP30 (eCO₂, TVOC) to ASI Biont using MQTT—no coding required beyond a few sentences in chat.

Why Environmental Sensors + AI?

According to the World Health Organization (WHO), indoor air pollution causes 3.8 million premature deaths annually (source: WHO 2021 report). Real-time monitoring with AI-driven responses can reduce exposure by automating ventilation, filtering, and occupancy-based climate control. ASI Biont eliminates the need to write and debug integration code yourself—the AI writes it on the fly, tailored to your hardware.

Connection Method: MQTT via ESP32

We use MQTT (Message Queuing Telemetry Transport) because it’s lightweight, reliable, and supported by virtually every IoT sensor platform. The ESP32 reads sensors over I²C, publishes data to a broker (e.g., Mosquitto), and ASI Biont subscribes via the paho-mqtt library inside its sandbox execute_python environment. No dashboard buttons—just a chat conversation where you describe your setup.

Wiring Diagram

ESP32 Pin BME280 Pin SGP30 Pin
3.3V VCC VCC
GND GND GND
GPIO21 SDA SDA
GPIO22 SCL SCL

Connect both sensors to the same I²C bus (addresses 0x76 for BME280, 0x58 for SGP30). No external resistors needed—ESP32 internal pull-ups work.

ESP32 MicroPython Code: Sensor Data Publisher

import network
import time
import ujson
from machine import Pin, I2C
import bme280  # micropython-bme280 library
import sgp30

# Wi-Fi credentials
WIFI_SSID = "your_ssid"
WIFI_PASS = "your_password"

# MQTT broker
MQTT_BROKER = "test.mosquitto.org"
TOPIC_PUB = "home/sensor/data"

# I2C setup
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
bme = bme280.BME280(i2c=i2c)
sgp = sgp30.Adafruit_SGP30(i2c)

# Wi-Fi connect
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASS)
while not wlan.isconnected():
    time.sleep(0.5)

# MQTT connect
import umqtt.simple as mqtt
client = mqtt.MQTTClient("esp32", MQTT_BROKER)
client.connect()

# Main loop
while True:
    temp, press, hum = bme.values
    sgp_data = sgp.iaq_measure()
    payload = ujson.dumps({
        "temp": float(temp[:-1]),
        "humidity": float(hum[:-1]),
        "pressure": float(press[:-3]),
        "eco2": sgp_data[0],
        "tvoc": sgp_data[1]
    })
    client.publish(TOPIC_PUB, payload)
    time.sleep(60)  # publish every minute

Upload this to your ESP32 using Thonny or ampy.

ASI Biont Side: AI Subscribes and Acts

In the ASI Biont chat, you write:

"Connect to MQTT broker test.mosquitto.org, subscribe to topic home/sensor/data, and alert me on Telegram when CO₂ exceeds 1000 ppm or temperature goes above 30°C."

ASI Biont’s AI generates and runs the following Python script inside its sandbox:

import paho.mqtt.client as mqtt
import telegram
import json

TELEGRAM_TOKEN = "your_bot_token"
CHAT_ID = "your_chat_id"

def on_message(client, userdata, msg):
    data = json.loads(msg.payload.decode())
    if data["eco2"] > 1000:
        bot = telegram.Bot(token=TELEGRAM_TOKEN)
        bot.send_message(chat_id=CHAT_ID, text=f"CO₂ warning: {data['eco2']} ppm")
    if data["temp"] > 30:
        bot = telegram.Bot(token=TELEGRAM_TOKEN)
        bot.send_message(chat_id=CHAT_ID, text=f"Temperature alert: {data['temp']}°C")

client = mqtt.Client()
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
client.subscribe("home/sensor/data")
client.loop_forever()

The AI runs this script (with a 30-second sandbox timeout, but loop_forever is replaced by a non-blocking loop in practice). You receive alerts instantly.

Real-World Scenarios

1. Automated Climate Control

Tell ASI Biont: "If humidity drops below 40%, turn on the humidifier via the Smart Plug HTTP API at 192.168.1.100." The AI writes a script that subscribes to MQTT, checks humidity, and sends an HTTP POST to the plug.

2. Historical Data Logging

"Log all sensor values to a PostgreSQL database every 5 minutes." The AI uses psycopg2 to insert records. You can later query trends.

3. Multi-Room Dashboard

With multiple ESP32s publishing to topics like home/sensor/kitchen, home/sensor/livingroom, the AI aggregates data and sends a daily summary via email.

Why No-Code Integration Matters

Traditional IoT integration requires writing custom scripts, handling edge cases, and debugging protocol mismatches. ASI Biont’s AI handles all that in seconds. You describe what you want in plain English—the AI writes the code, runs it, and connects to your devices. Supported protocols include MQTT, HTTP, Modbus, OPC UA, and even raw serial via Hardware Bridge (bridge.py).

Getting Started

  1. Flash your ESP32 with the MicroPython code above.
  2. Go to asibiont.com, create an account, and start a chat.
  3. Describe your sensor setup and what you want to automate.
  4. The AI generates and executes the integration script—no manual coding.

Environmental sensors become truly intelligent when paired with an AI agent that understands context, thresholds, and actions. Try it today and transform your space into a responsive, healthy environment.

← All posts

Comments