Smart Gas Safety: Integrating MQ-* Sensors (MQ-2, MQ-135) with AI Agent ASI Biont

Introduction

Gas leaks are silent threats. In smart homes and industrial environments, MQ-series sensors (MQ-2 for LPG/CH4, MQ-135 for NH3/CO2/benzene) are widely used for detection, but they often require manual monitoring or complex scripts to trigger actions. Without intelligent automation, a detected gas spike may go unnoticed until it's too late.

ASI Biont changes this by acting as an AI agent that connects to your MQ-* sensors through standard IoT protocols, analyzes data in real time, and automatically controls ventilation, gas valves, and sends Telegram alerts — all without you writing a single line of boilerplate code. Just describe your setup in plain English, and the AI handles the integration.

How ASI Biont Connects to MQ-* Sensors

The most practical connection method for MQ sensors is MQTT. MQTT is lightweight, works over Wi-Fi, and is natively supported by ESP32/Arduino boards reading analog outputs from MQ-2 or MQ-135. ASI Biont uses the paho-mqtt library inside its execute_python sandbox to subscribe to sensor topics and publish commands.

Alternative methods:
- Hardware Bridge (COM port): If your MQ sensor is connected to an Arduino via USB, ASI Biont can talk to it through bridge.py (running on your PC) using serial_write_and_read().
- HTTP API: Some ESP32 firmware exposes sensor data via REST endpoints; ASI Biont can poll them with aiohttp.

Connection Pros Cons Best for
MQTT Bidirectional, low latency, cloud-ready Requires MQTT broker Wi-Fi enabled ESP32 sensors
Hardware Bridge (COM) Direct USB access, no network setup Requires PC running bridge.py Arduino + MQ sensor on USB
HTTP API Simple, stateless Polling overhead, no real-time push Sensors with built-in web server

Real-World Scenario: ESP32 + MQ-135 → ASI Biont

Step 1: Hardware Setup

Connect the MQ-135 analog output (A0) to an ESP32 (GPIO 34). Power with 5V. The sensor outputs a voltage proportional to gas concentration. In the Arduino IDE, flash this firmware:

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "your_wifi";
const char* password = "your_pass";
const char* mqtt_server = "broker.hivemq.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) client.connect("ESP32_MQ135");
  client.loop();
  int raw = analogRead(34);
  float voltage = raw * (3.3 / 4095.0);
  float ppm = map(raw, 0, 4095, 0, 1000); // simplified
  client.publish("home/gas/raw", String(raw).c_str());
  client.publish("home/gas/ppm", String(ppm).c_str());
  delay(2000);
}

void callback(char* topic, byte* payload, unsigned int length) {
  // handle commands like "home/gas/set/vent"
}

Step 2: AI Agent Integration via Chat

In the ASI Biont dashboard, you start a chat with the AI agent and describe:

"Connect to MQTT broker broker.hivemq.com:1883, subscribe to home/gas/#, and if ppm exceeds 300, publish to home/gas/vent with payload 'ON' and send me a Telegram alert."

ASI Biont generates and executes the following Python script in its sandbox:

import paho.mqtt.client as mqtt
import requests

BROKER = "broker.hivemq.com"
PORT = 1883
THRESHOLD = 300
TELEGRAM_BOT_TOKEN = "YOUR_BOT_TOKEN"
TELEGRAM_CHAT_ID = "YOUR_CHAT_ID"

def on_message(client, userdata, msg):
    topic = msg.topic
    payload = msg.payload.decode()
    if "ppm" in topic:
        try:
            value = float(payload)
            print(f"Gas PPM: {value}")
            if value > THRESHOLD:
                client.publish("home/gas/vent", "ON")
                requests.post(f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
                              json={"chat_id": TELEGRAM_CHAT_ID, "text": f"⚠️ Gas high: {value} ppm! Vent ON."})
        except:
            pass

client = mqtt.Client()
client.on_message = on_message
client.connect(BROKER, PORT, 60)
client.subscribe("home/gas/#")
client.loop_forever()

The AI runs this script (with a 30‑second sandbox timeout — for persistent loops you’d use your own server). After execution, every 2 seconds the sensor publishes data, and the AI processes it instantly.

Step 3: Automated Safety Actions

Once connected, ASI Biont enables scenarios like:
- Threshold alerting: Telegram message when PPM > 300.
- Ventilation control: Publish home/gas/vent = ON to an ESP32 relay module.
- Gas valve shutdown: If PPM > 500, publish home/gas/valve = OFF.
- Dashboard logging: The AI can store readings in a local CSV or send to InfluxDB via HTTP.

Why ASI Biont Beats Manual Scripting

Aspect Traditional Approach ASI Biont Approach
Setup time Hours of coding & debugging Minutes of chat description
Flexibility Rewrite code for each sensor AI adapts to any MQ-* model
Maintenance Manual updates, bug fixes AI handles logic changes
Learning curve Requires Python/MQTT knowledge Natural language interface

Conclusion

Integrating MQ-* gas sensors with an AI agent like ASI Biont transforms a simple analog sensor into an intelligent safety system. Using MQTT as the communication backbone, the AI subscribes to sensor data, analyzes it, and triggers automated responses — all through a conversational setup. No dashboards, no heavy frameworks. Just describe your hardware and rules, and let the AI do the rest.

Ready to make your home or factory smarter? Go to asibiont.com, create your AI agent, and connect your MQ-135 or MQ-2 sensor today.

← All posts

Comments