You’ve got an SPI temperature sensor, an OLED display, or an SD card module sitting on your desk. You want it to talk to an AI agent — log data, send alerts, or even control actuators via chat. But SPI isn’t Ethernet or USB. It’s a four-wire bus (SCK, MOSI, MISO, CS) that needs direct GPIO control from a microcontroller. How do you bridge that gap to a cloud AI? This guide shows exactly how to connect any SPI device to ASI Biont — using a Raspberry Pi or an ESP32 as a relay, and letting the AI handle the integration code. No dashboard, no waiting for developers. Just chat.
Why Connect SPI Devices to an AI Agent?
SPI is everywhere in embedded systems: ADCs, DACs, temperature sensors (MAX31855, MAX6675), pressure sensors (MPS20N0040D), RFID readers (MFRC522), OLED displays (SSD1306), and SD card modules. These devices produce raw data or need direct commands. An AI agent can:
- Read sensor data and analyze trends
- Automatically adjust parameters based on thresholds
- Log data to cloud storage or send reports via email/Telegram
- Control actuators (e.g., turn on a relay when temperature exceeds a limit)
But SPI doesn’t plug into a cloud directly. You need a host — a Raspberry Pi or ESP32 — that talks SPI and then talks to ASI Biont via SSH, MQTT, or COM port through the Hardware Bridge.
How ASI Biont Connects to SPI Devices
ASI Biont uses execute_python — its sandbox environment — to generate and run Python code that connects to your SPI host. The host can be:
- Raspberry Pi (or any Linux SBC) — AI connects via SSH, runs a Python script using spidev or RPi.GPIO to read/write SPI, and returns data.
- ESP32 / Arduino — AI connects via MQTT (ESP32 publishes sensor data) or via COM port through Hardware Bridge (Arduino sends and receives SPI commands).
The user simply describes the setup in chat: "Connect to Raspberry Pi at 192.168.1.100 via SSH, read MAX31855 thermocouple data from SPI0.0 every 10 seconds, and send an alert if temperature exceeds 50°C." The AI writes the code, runs it in the sandbox, and starts the integration.
Concrete Use Case: Raspberry Pi + MAX31855 Thermocouple + ASI Biont
Hardware Setup
- Raspberry Pi 4 (Raspberry Pi OS)
- MAX31855 thermocouple amplifier + K-type thermocouple
- Connections:
- VCC → 3.3V
- GND → GND
- SCK → GPIO11 (SCLK)
- CS → GPIO8 (CE0)
- SO → GPIO9 (MISO)
Enable SPI on Raspberry Pi: sudo raspi-config → Interface Options → SPI → Enable. Reboot.
AI Integration via SSH
In the chat with ASI Biont, the user types:
"Connect to my Raspberry Pi at 192.168.1.100, user pi, password raspberry. Read the MAX31855 temperature using SPI (device /dev/spidev0.0). Continuously monitor and send a Telegram alert if temperature > 50°C. Also log every reading to a CSV file on the Pi."
ASI Biont generates a Python script using paramiko, executes it in the sandbox, and returns the result. Here’s what the AI writes (you’d see this in the chat response):
import paramiko
import time
import csv
from datetime import datetime
# SSH connection
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.100', username='pi', password='raspberry')
# Read temperature function
stdin, stdout, stderr = ssh.exec_command('''
import spidev
import time
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 5000000
def read_temp():
data = spi.xfer2([0x00, 0x00, 0x00, 0x00])
raw = ((data[0] << 8) | data[1]) >> 2
if raw & 0x2000:
raw = raw - 4096
return raw * 0.25
print(read_temp())
''')
print(stdout.read().decode())
The AI then sets up a monitoring loop (using a non-blocking approach with time.sleep in the sandbox, respecting the 30-second timeout) and sends the result to the user. For continuous monitoring, the AI uses the industrial_command tool to schedule periodic checks.
Result
- Every 10 seconds, the AI reads temperature from the Pi via SSH.
- If temperature exceeds 50°C, it sends a Telegram message: "⚠️ Temperature alert: 52.3°C"
- All readings are logged to
/home/pi/temperature_log.csvwith timestamps.
Alternative: ESP32 + SPI Sensor + MQTT
If you prefer an ESP32 as the host:
Hardware Setup
- ESP32 DevKit
- BME280 temperature/humidity/pressure sensor (SPI)
- Connections:
- VCC → 3.3V
- GND → GND
- SCK → GPIO18
- MOSI → GPIO23
- MISO → GPIO19
- CS → GPIO5
ESP32 Code (MicroPython)
The ESP32 reads the sensor and publishes to MQTT:
import network
import time
import ujson
from machine import Pin, SPI
import bme280_spi
# Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('SSID', 'PASSWORD')
# SPI setup
spi = SPI(1, baudrate=1000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
cs = Pin(5, Pin.OUT)
bme = bme280_spi.BME280(spi=spi, cs=cs)
# MQTT
import umqtt.simple as mqtt
client = mqtt.MQTTClient('esp32', 'broker.hivemq.com')
client.connect()
while True:
temp, pressure, humidity = bme.read_compensated_data()
payload = ujson.dumps({'temperature': temp/100, 'humidity': humidity/1024, 'pressure': pressure/25600})
client.publish('sensors/esp32/bme280', payload)
time.sleep(10)
AI Integration via MQTT
In the ASI Biont chat:
"Connect to MQTT broker at broker.hivemq.com, subscribe to topic 'sensors/esp32/bme280'. Parse the JSON data, check if temperature > 30°C, and if so, send an email alert to admin@example.com. Also plot a chart of temperature over the last 24 hours."
ASI Biont generates a script using paho-mqtt:
import paho.mqtt.client as mqtt
import json
import smtplib
from email.message import EmailMessage
def on_message(client, userdata, msg):
data = json.loads(msg.payload.decode())
temp = data['temperature']
if temp > 30:
send_email(temp)
def send_email(temp):
msg = EmailMessage()
msg.set_content(f'Temperature alert: {temp}°C')
msg['Subject'] = 'ESP32 Temperature Alert'
msg['From'] = 'alerts@example.com'
msg['To'] = 'admin@example.com'
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login('user', 'password')
server.send_message(msg)
client = mqtt.Client()
client.on_message = on_message
client.connect('broker.hivemq.com', 1883, 60)
client.subscribe('sensors/esp32/bme280')
client.loop_forever(timeout=1.0)
The AI runs this in the sandbox (with timeout=1.0 to avoid blocking) and monitors the data. You can also ask the AI to publish commands back to the ESP32 (e.g., to turn on a relay) via MQTT.
Why This Approach Wins
- No manual coding — You describe the task, AI writes the code.
- Any SPI device — As long as you have a host (Pi, ESP32, Arduino), you can connect any SPI sensor or actuator.
- Real-time monitoring — AI can check data every few seconds and trigger actions.
- Multi-platform — Use SSH for Linux boards, MQTT for ESP32, COM port for Arduino.
Pitfalls to Avoid
- SPI bus conflicts — If you have multiple SPI devices, ensure each has a unique CS pin. AI can help you write a multiplexer script.
- Baud rate mismatches — Some sensors require specific SPI speeds. The AI script should set
max_speed_hzaccordingly. - MQTT topic collisions — Use unique topic prefixes per device. The AI can generate UUID-based topics.
- Sandbox timeout —
execute_pythonhas a 30-second limit. For long-running monitoring, useindustrial_commandwith scheduling or MQTT callbacks. - Security — Never hardcode passwords in scripts. Use environment variables or ask AI to store them securely.
Conclusion
SPI devices are the backbone of embedded sensing and display. With ASI Biont, you don’t need to write integration code from scratch — just describe your hardware and what you want it to do. The AI agent connects via SSH, MQTT, or COM port, generates the Python script, and runs it. Whether you’re monitoring a thermocouple on a Raspberry Pi, reading a BME280 on ESP32, or controlling an OLED display, the process is the same: chat, describe, automate.
Ready to give your SPI devices an AI brain? Go to asibiont.com and start a conversation with the agent. No dashboard, no buttons — just pure integration through dialogue.
Comments