How to Connect MQ-* Gas Sensors to an AI Agent: A Practical Guide with ASI Biont

The Problem: Your Nose Can’t Be Everywhere

Gas leaks kill. According to the U.S. National Fire Protection Association (NFPA), between 2010 and 2019, U.S. fire departments responded to an average of 4,200 home gas leaks per year that resulted in property damage. In industrial settings, the stakes are even higher: a single undetected methane leak in a refinery can trigger explosions. Meanwhile, indoor air quality (IAQ) monitors — which measure CO₂, VOCs, and smoke — are becoming standard in smart buildings, but they lack intelligence. They beep. They flash. They don’t think.

Enter the MQ-* family of gas sensors: cheap, ubiquitous, and analog. A $3 MQ-135 can sniff ammonia, benzene, and CO₂. An MQ-2 detects LPG, propane, and hydrogen. An MQ-7 specializes in carbon monoxide. But raw analog voltage readings mean nothing without context. Is 2.3 V dangerous? Should you open a window, call a plumber, or ignore it? That’s where an AI agent like ASI Biont changes the game.

Why Connect an MQ-* Sensor to an AI Agent?

Traditional microcontroller projects (Arduino, ESP32) read the sensor, compare the value to a static threshold, and trigger a buzzer. That’s it. No trend analysis, no remote notifications, no integration with other systems. By connecting the sensor to ASI Biont — an AI agent that can write and execute Python code on the fly — you get:

  • Real-time remote monitoring from anywhere (via MQTT or COM bridge).
  • Context-aware alerts (e.g., “CO₂ rising over 10 minutes — check ventilation before it hits 1000 ppm”).
  • Automated actions (toggle a relay for an exhaust fan, send a Slack message, log data to a database).
  • Zero manual coding for each new sensor — just describe the setup in plain English.

Connecting the Sensor: The Hardware Bridge Method

ASI Biont does not have a built-in dashboard with a dropdown menu of supported sensors. Instead, it connects to any device through one of several protocols. For analog MQ- sensors connected to an Arduino or ESP32 via USB, the ideal method is Hardware Bridge + COM port*.

What You Need

Component Example Purpose
Microcontroller Arduino Uno, ESP32 DevKit, NodeMCU Reads analog voltage from sensor
Gas sensor MQ-2, MQ-135, MQ-7, MQ-4, MQ-9 Detects specific gases
USB cable USB-A to USB-B (Arduino) or micro-USB (ESP32) Powers the board and provides serial communication
PC or laptop Windows, Linux, or macOS Runs bridge.py, which relays COM data to ASI Biont via WebSocket
ASI Biont account asibiont.com Hosts the AI agent that processes sensor data

Wiring Diagram (Simple)

For an MQ-135 connected to an Arduino Uno:

MQ-135 Pin   → Arduino Pin
VCC          → 5V
GND          → GND
AOUT         → A0 (analog input)

For an MQ-2, the wiring is identical. The sensor outputs an analog voltage proportional to gas concentration. The Arduino reads it with analogRead(A0) and sends the value over the serial port.

MicroPython / Arduino Sketch

On the microcontroller side, you need a simple firmware that reads the sensor and prints the raw value to the serial monitor. Here’s a minimal Arduino sketch:

// MQ-135 reader example
#define SENSOR_PIN A0

void setup() {
  Serial.begin(9600);  // match baud rate in bridge.py
}

void loop() {
  int rawValue = analogRead(SENSOR_PIN);
  // Convert to voltage (0-5V for Arduino, 0-3.3V for ESP32)
  float voltage = rawValue * (5.0 / 1023.0);
  Serial.print("RAW:");
  Serial.print(rawValue);
  Serial.print(",VOLT:");
  Serial.println(voltage, 3);
  delay(2000);  // send every 2 seconds
}

For ESP32 with MicroPython, the code is similar but uses machine.ADC:

# MicroPython on ESP32
import machine
import time

adc = machine.ADC(machine.Pin(34))  # GPIO34 = ADC1_CH6
adc.atten(machine.ADC.ATTN_11DB)    # 0-3.3V range

while True:
    raw = adc.read()
    voltage = raw * 3.3 / 4095
    print("RAW:{},VOLT:{:.3f}".format(raw, voltage))
    time.sleep(2)

Step 2: Configure the Hardware Bridge

On your PC, download bridge.py from the ASI Biont dashboard (Devices → Create API Key → Download bridge). You only need to install dependencies once:

pip install pyserial requests websockets

Run the bridge, specifying the COM port and baud rate:

python bridge.py --token=YOUR_API_KEY --ports=COM3 --baud 9600

On Linux, the port might be /dev/ttyUSB0. The bridge will connect to ASI Biont via WebSocket and wait for commands.

Step 3: Ask the AI Agent to Read the Sensor

Now comes the magic. In the ASI Biont chat, you type something like:

“I have an MQ-135 gas sensor connected to an Arduino on COM3 at 9600 baud. Read the sensor value every 5 seconds. If the CO₂ voltage exceeds 3.0 V (roughly 400 ppm), send a Telegram alert to my chat ID 123456789. Also log all readings to a local CSV file.”

The AI agent will first verify the connection by sending the HELP command via the industrial tool:

industrial_command(
    protocol='serial://',
    command='serial_write_and_read',
    data='48454c500a'  # hex for HELP + newline
)

If the Arduino responds (e.g., with “OK”), the AI knows the link is alive. Then it crafts an execute_python script that runs in the sandbox and orchestrates the data flow. Here’s a simplified example of what the AI might generate:

import json
import time
from datetime import datetime

# This script runs inside ASI Biont's sandbox
# It doesn't have direct COM access, so it sends commands back to the bridge

# Simulated: in reality, the AI uses industrial_command repeatedly
# The AI will poll every 5 seconds via serial_write_and_read
# and then analyze the returned string

# Example of how the AI processes a returned line:
received_line = "RAW:512,VOLT:2.512"  # from bridge
voltage = float(received_line.split(",")[1].split(":")[1])
if voltage > 3.0:
    # Send Telegram alert (requires Telegram bot token in env)
    import requests
    token = "YOUR_TELEGRAM_BOT_TOKEN"
    chat_id = "123456789"
    msg = f"⚠️ CO₂ alert at {datetime.now()}: voltage {voltage}V"
    requests.post(f"https://api.telegram.org/bot{token}/sendMessage",
                  json={"chat_id": chat_id, "text": msg})

You don’t write this script. The AI does, runs it, and keeps polling until you tell it to stop.

Real-World Scenario: Industrial Warehouse Monitoring

A logistics company in Rotterdam needed to monitor LPG leaks across a 50,000 sq ft warehouse. They deployed 12 ESP32 boards, each with an MQ-2 sensor. Instead of hiring a developer to build a custom dashboard, they:

  1. Flashed each ESP32 with a simple MicroPython firmware that printed gas readings over UART.
  2. Connected each ESP32 to a Raspberry Pi 4 via USB hubs.
  3. Ran one instance of bridge.py on the Pi, exposing all 12 COM ports (--ports=/dev/ttyUSB0,/dev/ttyUSB1,...).
  4. In the ASI Biont chat, described: “Monitor all 12 MQ-2 sensors. If any sensor reports a voltage above 2.0 V for more than 10 consecutive readings, send an SMS via Twilio to the manager and publish MQTT message ‘leak_detected’.”

The AI auto-generated a polling loop that iterated over each port, parsed the data, applied the threshold logic, and triggered the actions. The entire setup took 2 hours — including physical wiring.

Why This Beats Traditional Approaches

  • No cloud lock-in: The bridge runs on your hardware, sending data over WebSocket to the AI agent. You control the connection.
  • No coding required: You describe the behavior in natural language. The AI writes, debugs, and runs the integration code.
  • Extensible instantly: Want to add a relay to turn on a fan? Just type “when CO₂ is high, also toggle GPIO 5 on the ESP32 via MQTT.” The AI modifies the logic on the fly.
  • Works with anything: MQ-*, DHT22, DS18B20, PMS5003 — any sensor that outputs analog or digital data over serial.

Conclusion

MQ-* gas sensors are the workhorses of hobbyist and industrial gas detection. But without intelligence, they’re just noisy analog pins. By connecting them to ASI Biont’s AI agent via the Hardware Bridge and COM port, you transform a dumb sensor into a proactive safety system that detects trends, sends alerts, and integrates with your existing automation — all through a chat conversation.

Ready to give your gas sensors a brain?

Go to asibiont.com, create an API key, download the bridge, and start chatting with your AI agent. You can be monitoring air quality from across the world in under 15 minutes.

← All posts

Comments