SPI Integration with ASI Biont: Connect Sensors, Displays, and More via AI Agent

SPI Integration with ASI Biont: Connect Sensors, Displays, and More via AI Agent

Introduction

SPI (Serial Peripheral Interface) is one of the most common synchronous serial communication protocols in embedded systems. It’s used by everything from temperature sensors (MAX31855, MCP9808) and accelerometers (ADXL345) to OLED displays (SSD1306) and SD card modules. Traditionally, integrating an SPI device with an AI agent meant writing custom middleware, polling data manually, and building a separate dashboard. With ASI Biont, you can skip the boilerplate: describe your device in a chat, and the AI writes the integration code on the fly. This guide shows you how to connect SPI-based devices to ASI Biont using execute_python (for cloud-based analysis) and Hardware Bridge (for local COM port access). No pre-built drivers, no waiting for SDK updates—just instant automation.

Why Connect SPI Devices to an AI Agent?

SPI devices are fast and low-latency, but they lack built-in networking. Without a host computer or microcontroller, you can’t send their data to the cloud or trigger actions based on AI analysis. ASI Biont acts as the brain: it reads sensor values, detects anomalies, sends Telegram alerts, and even controls actuators—all through natural language conversations. You no longer need to write polling loops or manage timeouts; the AI handles the protocol and timing.

How ASI Biont Connects to SPI Devices

ASI Biont doesn’t have a dedicated “SPI” protocol. Instead, it connects via one of two methods:

  1. Hardware Bridge: For SPI devices attached to a PC (via USB-to-SPI adapter, e.g., FTDI or a microcontroller acting as a bridge). The user runs bridge.py (downloaded from the ASI Biont dashboard) on their local machine. The bridge connects to ASI Biont via WebSocket and exposes the COM port. The AI sends serial commands (e.g., serial_write_and_read(data="...")) to read SPI data encoded as hex or ASCII.

  2. execute_python: For SPI devices connected to a single-board computer like a Raspberry Pi or Orange Pi. The AI writes a Python script that uses the spidev library (or python-periphery) to talk to the SPI bus directly via the board’s GPIO pins. The script runs in the cloud sandbox, but the actual I/O happens via SSH (paramiko) if the board is reachable, or via a local script that sends data to the bridge.

Real-world example: A Raspberry Pi with an SPI temperature sensor (MAX31855 + thermocouple) connected via SSH. The AI writes a Python script that reads the sensor every 10 seconds, logs the temperature, and sends a Telegram message if it exceeds 100°C.

Step-by-Step Integration: ESP32 + SPI OLED Display + ASI Biont

Let’s walk through a concrete scenario: you have an ESP32 connected to an SPI OLED display (SSD1306, 128x64). The ESP32 reads temperature from a built-in sensor or a DHT22, and you want the AI to update the display with custom messages (e.g., “System OK” or “ALERT: High Temp”).

1. Hardware Setup

  • ESP32 dev board (e.g., ESP32-DevKitC)
  • SSD1306 OLED (SPI mode: CS, DC, MOSI, SCK, RST)
  • DHT22 (optional, for temperature)

Wiring (ESP32 → OLED):

ESP32 Pin OLED Pin
GPIO5 CS
GPIO2 DC
GPIO23 MOSI
GPIO18 SCK
GPIO4 RST
3.3V VCC
GND GND

2. MicroPython Firmware on ESP32

Flash MicroPython (from micropython.org) and upload a script that listens on UART (COM port) for commands. Example:

# esp32_spi_oled.py
from machine import Pin, SPI, UART
import ssd1306
import utime

spi = SPI(1, baudrate=1000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23))
dc = Pin(2, Pin.OUT)
cs = Pin(5, Pin.OUT)
rst = Pin(4, Pin.OUT)
rst.value(1)

oled = ssd1306.SSD1306_SPI(128, 64, spi, dc, cs, rst)

uart = UART(2, baudrate=115200)  # TX=GPIO17, RX=GPIO16

while True:
    if uart.any():
        cmd = uart.readline().decode().strip()
        if cmd.startswith("DISPLAY:"):
            text = cmd[8:]
            oled.fill(0)
            oled.text(text, 0, 0)
            oled.show()
        elif cmd == "TEMP":
            # Simulate reading temperature
            temp = 25.3
            uart.write(f"{temp}\n")

3. Connecting to ASI Biont via Hardware Bridge

On your PC, download bridge.py from the ASI Biont dashboard (Devices → Create API Key → Download bridge). Run it:

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

The bridge connects to ASI Biont via WebSocket and opens COM3 at 115200 baud.

4. Chat with AI to Control the Display

In the ASI Biont chat, you describe the task:

“Connect to COM3 at 115200 baud. Send the command ‘DISPLAY:Hello World’ to the OLED display. Then read the temperature by sending ‘TEMP’ and log it.”

The AI interprets this and uses the industrial_command tool:

# AI-generated code (runs in execute_python sandbox)
# This is a demonstration — actual command sent via industrial_command
# The AI uses the 'serial_write_and_read' function on the bridge

Behind the scenes, the AI calls:
- serial_write_and_read(data="444953504c41593a48656c6c6f20576f726c640a") (hex for "DISPLAY:Hello World\n")
- serial_write_and_read(data="54454d500a") (hex for "TEMP\n")

The bridge sends these to COM3, receives the response (e.g., "25.3"), and returns it to the AI.

5. Automate Alerts

You can set up a recurring check: “Every 5 minutes, read temperature. If over 30°C, send ‘DISPLAY:ALERT High Temp’ to the OLED and notify me on Telegram.” The AI writes a loop (using time.sleep) that polls the sensor and acts conditionally. Since bridge.py handles the serial I/O, the AI can run the script in the cloud indefinitely.

Advanced Use Case: SPI Accelerometer + Real-Time Monitoring

An ADXL345 accelerometer connected to a Raspberry Pi via SPI. The AI connects via SSH (paramiko), reads acceleration data, and detects unusual vibrations. Example AI-generated script:

import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0)  # bus 0, device 0
spi.max_speed_hz = 5000000

# Read acceleration from ADXL345 registers
def read_accel():
    raw = spi.xfer2([0x80 | 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
    x = (raw[1] << 8 | raw[2]) & 0x3FF
    y = (raw[3] << 8 | raw[4]) & 0x3FF
    z = (raw[5] << 8 | raw[6]) & 0x3FF
    return x, y, z

while True:
    x, y, z = read_accel()
    print(f"X:{x} Y:{y} Z:{z}")
    time.sleep(1)

The AI runs this via execute_python with an SSH connection to the Pi. If acceleration exceeds a threshold (e.g., >2g), the AI sends a Telegram alert. No manual SSH setup—just describe the Pi’s IP and credentials in the chat.

Why This Matters: No More Manual Coding

Traditional integration requires:
- Writing a polling script in C/Python
- Setting up a database for logs
- Building a dashboard or notification system
- Handling edge cases (timeouts, reconnection)

With ASI Biont, you simply say: “Connect to my Raspberry Pi at 192.168.1.100 via SSH, read the ADXL345 SPI accelerometer every 5 seconds, and tell me if vibration exceeds 3g.” The AI generates the code, runs it, and responds with the first reading. You can then ask: “Send a Telegram message when vibration is high.” The AI adds the notification logic in the same script.

Key takeaway: ASI Biont connects to any SPI device through execute_python—the AI writes the integration code for each device on the fly. No need to wait for developers to add support; connect anything right now. Just describe the device, connection parameters (port, IP, baud rate, API key), and the AI does the rest.

Pitfalls to Avoid

  • Baud rate mismatch: Ensure the ESP32/Arduino UART baud rate matches the bridge’s --baud flag. Common values: 115200, 9600.
  • Hex encoding: When sending commands via serial_write_and_read, make sure the hex string is properly encoded (no spaces). Use an online hex converter or Python’s bytes.hex().
  • SSH key authentication: For Raspberry Pi, use key-based auth and pass the key path in the chat. The AI uses paramiko with ssh_key_filename.
  • Sandbox timeout: execute_python scripts have a 30-second timeout. For long-running monitoring, use the Hardware Bridge or a cron job on the device.

Conclusion

SPI devices are the backbone of embedded systems, but they don’t have to be isolated. By connecting them to ASI Biont, you unlock AI-powered monitoring, alerts, and control—all through natural language. Whether it’s an OLED display on an ESP32 or an accelerometer on a Raspberry Pi, the integration is just a chat away.

Ready to try it? Go to asibiont.com, create an API key, download the bridge, and start controlling your SPI devices with AI. No coding required—just describe what you want.

← All posts

Comments