Integrating SPI Devices with ASI Biont AI Agent: Real-World Guide to Automating Data Collection and Control
Introduction
SPI (Serial Peripheral Interface) is one of the most widely used synchronous serial communication protocols in embedded systems. It connects microcontrollers to sensors, ADCs, DACs, displays, and memory chips. But the real challenge begins when you need to collect data from an SPI device, process it, and trigger actions based on insights — all without writing complex glue code from scratch.
Enter ASI Biont – an AI agent that connects to any hardware device through Python-based integration, either via execute_python (sandbox) or via a Hardware Bridge for local COM ports. In this guide, I’ll show you how to integrate an SPI temperature/pressure sensor (like the MCP3008 ADC or MAX31855 thermocouple) with ASI Biont using a Raspberry Pi or ESP32, automate data collection, and receive alerts in Telegram — all by simply describing your setup in a chat.
Why Connect an SPI Device to an AI Agent?
SPI devices are fast and reliable, but they often require manual polling, data parsing, and decision-making logic. By connecting an SPI sensor to ASI Biont, you get:
- Automated data collection – AI reads the sensor on a schedule (e.g., every 5 minutes).
- Intelligent alerts – AI analyzes trends and notifies you via Telegram, email, or Slack when thresholds are exceeded.
- Remote control – AI can write to SPI devices (e.g., set DAC output, control display).
- Zero manual coding – you just describe your hardware in a chat; AI writes and executes the integration Python script.
How ASI Biont Connects to SPI Devices
ASI Biont does not have a built-in SPI driver. Instead, it uses its universal execute_python mechanism. The user describes their device (e.g., “Raspberry Pi with MCP3008 on SPI0, CE0, reading channel 0”), and AI writes a Python script using the spidev library (on Raspberry Pi) or machine.SPI (on MicroPython for ESP32). The script runs in the ASI Biont sandbox, which has network access to the device via SSH (if using a Raspberry Pi) or via MQTT/HTTP (if using an ESP32).
Connection methods relevant for SPI:
| Method | Description | Best for |
|---|---|---|
| SSH + execute_python | AI writes a Python script that uses paramiko to SSH into a Raspberry Pi, runs a local script that reads SPI via spidev. |
Single-board computers (RPi, Orange Pi) |
| MQTT + execute_python | ESP32 publishes SPI sensor data to an MQTT broker; AI subscribes and processes. | ESP32, microcontrollers with WiFi |
| Hardware Bridge (COM port) | If your SPI device is connected via a USB-SPI adapter (e.g., FTDI), AI can talk to it via COM port using pyserial. |
Windows/Linux PCs with USB-SPI dongles |
Real-World Example: ESP32 + MAX31855 Thermocouple → Telegram Alerts
Problem
You have a high-temperature process (e.g., kiln, furnace) monitored by a MAX31855 thermocouple-to-digital converter connected via SPI to an ESP32. You need to:
- Read temperature every 10 seconds.
- Log data to a database.
- Send a Telegram alert if temperature exceeds 800°C.
Solution
- ESP32 firmware: Read MAX31855 via SPI, publish temperature to an MQTT topic (
sensor/kiln/temp). - ASI Biont: User tells the AI: “Connect to MQTT broker at 192.168.1.100, subscribe to
sensor/kiln/temp, parse the temperature, store in a local CSV, and if >800°C, send a Telegram message.” - AI writes a Python script using
paho-mqttandrequests(for Telegram Bot API), executes it in the sandbox, and starts monitoring.
Step-by-Step
1. ESP32 Code (MicroPython)
from machine import Pin, SPI
import time
import ujson
from umqtt.simple import MQTTClient
# SPI setup for MAX31855
spi = SPI(1, baudrate=1000000, polarity=0, phase=0)
cs = Pin(5, Pin.OUT)
def read_temp():
cs.value(0)
data = spi.read(4)
cs.value(1)
raw = (data[0] << 24)
| (data[1] << 16) | (data[2] << 8) | data[3]
if raw & 0x80000000:
return None # fault
temp = ((raw >> 18) & 0x3FFF) * 0.25
return temp
# MQTT setup
client = MQTTClient("esp32", "192.168.1.100")
client.connect()
while True:
t = read_temp()
if t is not None:
payload = ujson.dumps({"temp": t, "unit": "C"})
client.publish("sensor/kiln/temp", payload)
time.sleep(10)
2. ASI Biont Integration – User Describes in Chat
“Connect to MQTT broker at 192.168.1.100:1883, subscribe to
sensor/kiln/temp. Parse JSON. If temperature > 800°C, send Telegram alert to chat ID 123456 using bot tokenABC123. Also log all readings tokiln_log.csv.”
3. AI-Generated Python Script (executed in sandbox)
import paho.mqtt.client as mqtt
import json
import csv
import requests
from datetime import datetime
TELEGRAM_BOT_TOKEN = "ABC123"
TELEGRAM_CHAT_ID = "123456"
CSV_FILE = "kiln_log.csv"
def on_message(client, userdata, msg):
data = json.loads(msg.payload)
temp = data.get("temp")
timestamp = datetime.now().isoformat()
# Log to CSV
with open(CSV_FILE, "a", newline="") as f:
writer = csv.writer(f)
writer.writerow([timestamp, temp])
# Alert if >800
if temp > 800:
text = f"⚠️ Kiln temperature {temp}°C exceeds 800°C at {timestamp}"
requests.post(
f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
json={"chat_id": TELEGRAM_CHAT_ID, "text": text}
)
client = mqtt.Client()
client.on_message = on_message
client.connect("192.168.1.100", 1883, 60)
client.subscribe("sensor/kiln/temp")
client.loop_forever() # runs until stopped manually
Note: The script runs in the ASI Biont sandbox. Because loop_forever() blocks, the AI can manage it with a 30-second timeout, but for long-running tasks, the AI can set up a scheduled execution (e.g., every minute) or use loop_start() in a non-blocking manner.
Result
- The ESP32 sends temperature data every 10 seconds.
- ASI Biont logs every reading to a CSV file (accessible via chat).
- If the kiln overheats, you get an instant Telegram alert.
- You never wrote the integration code – the AI did it for you.
Pitfalls to Avoid
- SPI voltage levels: MAX31855 runs at 3.3V. Connecting directly to a 5V Arduino may damage the sensor. Use level shifters.
- MQTT QoS: For critical alerts, use QoS 1 or 2. The example above uses default QoS 0 (maybe lost). AI can adjust.
- Sandbox timeout:
loop_forever()may hit the 30-second limit. Useloop_start()in a separate thread (but threading is forbidden in ASI Biont sandbox). Instead, use a scheduled approach: the AI runs the script every 60 seconds viaexecute_pythonwith a single poll. - CSV file location: In the sandbox, the file is ephemeral. For persistence, the AI can upload to Google Drive or send via email.
Why This Approach Beats Traditional Coding
Traditionally, you’d need to:
- Write an MQTT subscriber script.
- Implement Telegram bot logic.
- Set up logging.
- Deploy and maintain.
With ASI Biont, you just describe the task. The AI writes, tests, and runs the code in seconds. You can change the logic (e.g., “send SMS instead of Telegram”) by simply updating the chat description.
Conclusion
Integrating an SPI sensor with ASI Biont unlocks a new level of automation. Whether you’re monitoring a kiln, a weather station, or a lab experiment, the AI agent handles all the plumbing. No need to learn MQTT, Telegram APIs, or CSV handling – just connect your hardware and tell the AI what you want.
Ready to try? Go to asibiont.com, create an account, and describe your SPI device in the chat. The AI will connect it in minutes.
Comments