How to Integrate Energy Meters with ASI Biont AI Agent: MQTT/Modbus Automation Without Code

Why Connect Energy Meters to an AI Agent?

Energy meters are everywhere—from factory floors to smart homes. They measure kWh, voltage, current, power factor, and more. But raw data is useless without analysis. Traditionally, you’d need a SCADA system, a dedicated server, and a developer to write custom scripts. With ASI Biont, you skip all that. The AI agent connects directly to your energy meters via MQTT, Modbus TCP, or COM port (through Hardware Bridge), reads the data, and automates alerts, trending, and even control actions—all through a simple chat conversation.

How ASI Biont Connects to Energy Meters

ASI Biont uses execute_python—a sandboxed Python environment—to run integration code. You don’t install anything on your meter. The AI writes the code for you. For energy meters, the most common protocols are:

Protocol Typical Use Example Devices
MQTT Smart home meters, cloud-connected Shelly EM, Iammeter, Eastron SDM630-Modbus + MQTT gateway
Modbus TCP Industrial meters, PLC-connected Siemens PAC, Schneider PM5000, ABB M4M
Modbus RTU (COM) Older meters, RS-485 bus Eastron SDM230, Wibeee, generic Modbus energy meters
HTTP API Cloud-enabled meters Sense Energy Monitor, Emporia Vue

You simply tell the AI in the chat: "Connect to my Shelly EM via MQTT at 192.168.1.100:1883, topic 'shellies/em/...'" and the AI does the rest.

Real Use Case: ESP32 + Modbus Energy Meter → Telegram Alerts

Scenario

You have an Eastron SDM230 single-phase energy meter connected to an ESP32 via RS-485 (using a MAX485 module). The ESP32 reads Modbus registers (voltage, current, power, total kWh) and publishes them via MQTT to a broker. ASI Biont subscribes to that MQTT topic, analyzes the data, and sends a Telegram message when power exceeds 2000W.

Step 1: ESP32 Code (Arduino IDE)

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

#define RXD2 16
#define TXD2 17
#define MAX485_DE 4

ModbusMaster node;
WiFiClient espClient;
PubSubClient client(espClient);

const char* ssid = "your_SSID";
const char* password = "your_PASS";
const char* mqtt_server = "192.168.1.100";

void preTransmission() {
  digitalWrite(MAX485_DE, 1);
}

void postTransmission() {
  digitalWrite(MAX485_DE, 0);
}

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8E1, RXD2, TXD2);
  pinMode(MAX485_DE, OUTPUT);
  digitalWrite(MAX485_DE, 0);

  node.begin(1, Serial2);
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);

  client.setServer(mqtt_server, 1883);
}

void loop() {
  if (!client.connected()) client.connect("ESP32_EnergyMeter");
  client.loop();

  uint8_t result;
  result = node.readInputRegisters(0x0000, 6); // voltage, current, power, etc.
  if (result == node.ku8MBSuccess) {
    float voltage = node.getResponseBuffer(0) / 10.0;
    float current = (node.getResponseBuffer(1) + (node.getResponseBuffer(2) << 16)) / 1000.0;
    float power = (node.getResponseBuffer(3) + (node.getResponseBuffer(4) << 16)) / 10.0;
    float total_kWh = (node.getResponseBuffer(5) + (node.getResponseBuffer(6) << 16)) / 1000.0;

    char msg[128];
    snprintf(msg, 128, "{\"voltage\":%.1f,\"current\":%.2f,\"power\":%.1f,\"total_kWh\":%.3f}", voltage, current, power, total_kWh);
    client.publish("energy/esp32/sdm230", msg);
  }
  delay(5000);
}

Step 2: ASI Biont Integration (Chat)

You open the chat with ASI Biont and write:

"Subscribe to MQTT topic 'energy/esp32/sdm230' on broker 192.168.1.100:1883. Parse the JSON. If power > 2000W, send a Telegram alert to my chat ID 123456789 using bot token 'YOUR_BOT_TOKEN'. Also log all data to a CSV every hour."

The AI generates and runs this script in execute_python:

import paho.mqtt.client as mqtt
import json
import requests
from datetime import datetime

TELEGRAM_BOT_TOKEN = "YOUR_BOT_TOKEN"
TELEGRAM_CHAT_ID = "123456789"

def on_message(client, userdata, msg):
    data = json.loads(msg.payload.decode())
    power = data['power']
    voltage = data['voltage']
    current = data['current']
    total_kWh = data['total_kWh']

    # Alert if power > 2000W
    if power > 2000:
        text = f"⚡ High power alert! {power:.1f}W (V={voltage:.1f}, I={current:.2f}A)"
        requests.post(f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
                      json={"chat_id": TELEGRAM_CHAT_ID, "text": text})

    # Log to file (appends)
    with open("energy_log.csv", "a") as f:
        f.write(f"{datetime.now().isoformat()},{voltage},{current},{power},{total_kWh}\n")

client = mqtt.Client()
client.on_message = on_message
client.connect("192.168.1.100", 1883, 60)
client.subscribe("energy/esp32/sdm230")
client.loop_forever()  # runs until timeout (30s in sandbox, but keeps connection alive)

The sandbox timeout is 30 seconds, but for continuous monitoring, you’d run this script on your own server (or use the bridge’s persistent MQTT connection). In practice, you can tell the AI: "Keep this script running forever" and it will generate a version that uses a loop with reconnect.

Alternative: Direct Modbus TCP from ASI Biont

If your energy meter supports Modbus TCP (e.g., Eastron SDM630-Modbus with Ethernet), you don’t need an ESP32. Just provide the IP address and register map:

"Connect to Modbus TCP device at 192.168.1.50:502, unit ID 1. Read input registers 0x0000 to 0x0006. Parse as float32. If total power > 3000W, send me an email via SendGrid."

ASI Biont uses the industrial_command tool with Modbus protocol:

industrial_command(
    protocol='modbus',
    command='read_registers',
    params={
        'host': '192.168.1.50',
        'port': 502,
        'unit_id': 1,
        'start_address': 0,
        'count': 6
    }
)

The AI then parses the response (two 16-bit registers per 32-bit float) and acts on it.

Why This Approach Wins

  1. No coding required from you — you just describe what you want in plain English.
  2. Universal — any energy meter that speaks MQTT, Modbus, or HTTP can be integrated.
  3. Real-time — alerts arrive in seconds, not minutes.
  4. Scalable — add more meters by simply telling the AI the new topic or IP.

Pitfalls to Avoid

  • Baud rate mismatch: ESP32 with Modbus RTU often uses 9600 8E1. Double-check your meter’s manual.
  • Register mapping: Different meters use different register addresses. Always verify with the datasheet.
  • MQTT QoS: For critical alerts, use QoS 1 to avoid message loss.
  • Sandbox timeout: execute_python scripts have a 30-second limit. For long-running tasks, use Hardware Bridge or run the script externally.

Conclusion

Integrating energy meters with ASI Biont transforms raw power data into actionable intelligence—without writing a single line of code yourself. Whether you’re monitoring a home solar system or an industrial plant, the AI agent connects, analyzes, and alerts. Try it now: go to asibiont.com, create a free account, and tell the AI: "Connect to my Eastron SDM630 Modbus meter at 192.168.1.50 and send me a daily report of total kWh."

← All posts

Comments