CAN Bus Meets AI: Predictive Maintenance and Real-Time Control with ASI Biont

Introduction: Why Connect a CAN Bus to an AI Agent?

The Controller Area Network (CAN) bus is the backbone of modern industrial automation, automotive electronics, and robotics. Developed by Robert Bosch GmbH in the 1980s, CAN bus is a robust, multi-master serial protocol that allows microcontrollers and devices to communicate without a host computer. Today, it's found in everything from factory PLCs and CNC machines to electric vehicle battery management systems and agricultural equipment. However, extracting actionable insights from CAN bus data traditionally requires custom software, manual scripting, or expensive SCADA systems. That's where ASI Biont — an AI agent that writes and executes Python code on demand — changes the game. Instead of writing a parser for every CAN message ID or building a dashboard from scratch, you simply describe your device and goals in a chat conversation. The AI chooses the correct integration method (MQTT, COM port via Hardware Bridge, or direct SSH to a CAN gateway), writes the code, and runs it. This article shows you exactly how to connect a CAN bus device to ASI Biont, with real code examples and practical scenarios.

Which Device? A CAN-to-USB Adapter with a Temperature Sensor

For this guide, we use a common setup: an ESP32 microcontroller connected to a CAN transceiver (e.g., MCP2551) and a DS18B20 temperature sensor. The ESP32 reads the sensor and broadcasts temperature data over the CAN bus at 500 kbps. This mimics a real industrial node — like a temperature sensor in a factory or a motor temperature monitor in a robot. The goal: connect this CAN bus to ASI Biont, log temperature trends, detect anomalies, and send alerts when readings exceed thresholds.

Connection Method: MQTT via ESP32 + ASI Biont's execute_python

Why MQTT? CAN bus itself is a low-level protocol — ASI Biont's sandbox (execute_python) does not have direct access to a CAN interface (no python-can library is available in the sandbox). However, the ESP32 can bridge CAN to MQTT: it reads CAN frames, extracts temperature values, and publishes them to an MQTT broker (like Mosquitto) on a topic like factory/temperature. ASI Biont then subscribes to that topic using the paho-mqtt library (available in the sandbox). This is the most practical approach for cloud-based AI agents.

Step 1: ESP32 Code (MicroPython)

On the ESP32, flash this firmware using Thonny or esptool:

import network
import time
import ujson
from machine import Pin, UART
import onewire
import ds18x20
from umqtt.simple import MQTTClient

# Wi-Fi credentials
SSID = "YourWiFi"
PASSWORD = "YourPassword"

# MQTT broker
BROKER = "192.168.1.100"  # Your MQTT broker IP
TOPIC = b"factory/temperature"

# DS18B20 sensor on GPIO4
ds_pin = Pin(4)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_sensor.scan()
print('Found DS18B20 devices:', roms)

# CAN setup (example with built-in CAN or external MCP2515)
# For simplicity, we use UART2 for CAN hat (adjust as needed)
# can = CAN(0, baud=500000, pins=('GPIO5','GPIO4'))  # ESP32 internal CAN

# Wi-Fi connection
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
    time.sleep(1)
print('Wi-Fi connected')

client = MQTTClient("esp32_can", BROKER)
client.connect()
print('MQTT connected')

while True:
    ds_sensor.convert_temp()
    time.sleep_ms(750)
    for rom in roms:
        temp = ds_sensor.read_temp(rom)
        payload = ujson.dumps({"sensor": "DS18B20", "temperature": temp, "unit": "C"})
        client.publish(TOPIC, payload)
        print('Published:', payload)
    time.sleep(5)

This code reads temperature every 5 seconds and publishes JSON to MQTT.

Step 2: ASI Biont Integration via Chat

Now, in the ASI Biont chat, you describe the setup:

"Connect to MQTT broker at 192.168.1.100:1883, subscribe to topic 'factory/temperature', read incoming JSON, log temperature values in a list, and if temperature exceeds 40°C, print an alert and also send a Telegram message using the telegram bot token '123456:ABC-DEF' to chat ID 987654321."

ASI Biont's AI interprets this, writes a Python script, and executes it in the sandbox (execute_python). The script uses paho-mqtt for subscription and requests (or aiohttp) for Telegram.

Example of the code ASI Biont generates (you don't need to write this — AI does it):

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

TELEGRAM_TOKEN = "123456:ABC-DEF"
CHAT_ID = "987654321"
temperatures = []

def on_message(client, userdata, msg):
    data = json.loads(msg.payload.decode())
    temp = data.get('temperature')
    if temp is not None:
        temperatures.append(temp)
        print(f"Received temperature: {temp}°C")
        if temp > 40:
            alert_text = f"⚠️ High temperature alert: {temp}°C"
            print(alert_text)
            requests.post(
                f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage",
                data={"chat_id": CHAT_ID, "text": alert_text}
            )

client = mqtt.Client()
client.on_message = on_message
client.connect("192.168.1.100", 1883, 60)
client.subscribe("factory/temperature")
client.loop_start()  # non-blocking loop

# Keep script alive for 25 seconds (sandbox timeout is 30s)
import time
time.sleep(25)
client.loop_stop()
print(f"Final temperature history: {temperatures}")

Step 3: Run and Observe

The AI runs the script, subscribes to the MQTT topic, receives 5 temperature readings, and if any exceed 40°C, sends a Telegram message. All within seconds. No manual coding, no MQTT client configuration — just describe what you want.

Real-World Scenario: Predictive Maintenance on a CNC Machine

Consider a factory with a CNC spindle motor that broadcasts its temperature and vibration over CAN bus. Traditionally, maintenance is scheduled every 1000 hours, regardless of actual condition. With ASI Biont connected via MQTT bridge, the AI can:

  • Collect temperature and vibration data every minute.
  • Build a simple statistical baseline (mean, standard deviation) after 24 hours.
  • Detect anomalies: if temperature deviates more than 2 standard deviations from the baseline, flag it.
  • Send a Slack/Telegram alert with a recommendation: "Check spindle bearing — temperature 55°C exceeds normal range (45±3°C)."

This reduces unplanned downtime by up to 30% (source: McKinsey, 2023, "Smart Maintenance in Manufacturing").

Why Use ASI Biont? No Coding Required — AI Writes the Integration

The key advantage: ASI Biont connects to ANY device using execute_python — the AI itself writes the integration code tailored to your device. You don't wait for developers to add support. You just describe in the chat:

  • Connection method: MQTT, COM port (via Hardware Bridge), SSH, Modbus/TCP, HTTP API, OPC-UA, etc.
  • Parameters: IP address, port, baud rate, API key, topic names.
  • What to do: read data, analyze, send alerts, control outputs.

The AI uses the correct libraries (paho-mqtt, pyserial, pymodbus, paramiko, aiohttp, opcua-asyncio) and runs the code in a secure sandbox. For COM port access, you run bridge.py locally (download from ASI Biont), which connects to the cloud via long polling — AI sends commands through industrial_command tool, and the bridge reads/writes to the serial port.

Limitations and Best Practices

  • Sandbox timeout: execute_python scripts are limited to 30 seconds. For long-running monitoring, use MQTT with loop_start() and a short sleep, or use Hardware Bridge with industrial_command for continuous COM port reads.
  • No direct CAN interface: ASI Biont cannot directly access a CAN bus controller. Use an intermediate gateway (ESP32, Raspberry Pi + MCP2515) that bridges CAN to MQTT, HTTP, or serial.
  • Security: MQTT over TLS (port 8883) is recommended for production. Provide the CA certificate in the chat description if needed.
  • Data volume: CAN bus can generate thousands of frames per second. Filter on the gateway — only publish relevant signals to MQTT to avoid overwhelming the broker.

Conclusion

Integrating a CAN bus with an AI agent like ASI Biont unlocks predictive maintenance, real-time monitoring, and automated control without writing complex software. By bridging CAN to MQTT via an ESP32, you can leverage the full power of ASI Biont's execute_python to analyze data, detect anomalies, and send alerts — all through a simple chat conversation. Whether you're in automotive, factory automation, or robotics, this approach saves hours of development time and makes industrial data accessible to anyone.

Ready to connect your CAN bus to AI? Try it now at asibiont.com — just describe your device and goals in the chat.

← All posts

Comments