Introduction
Managing sensor data and making split-second decisions in embedded systems often feels like a battle against time. You write firmware, set up serial communication, parse data manually, and then code decision logic. When conditions change, you recompile and reflash. This workflow is slow and rigid. What if an AI agent could handle the entire integration—reading your Teensy 4.x sensor data, analyzing it, and controlling actuators—all through a chat conversation? That's exactly what ASI Biont does.
Teensy 4.x is a remarkable microcontroller: 600 MHz ARM Cortex-M7, real-time performance, and the ability to emulate peripherals via FlexIO. It's perfect for audio processing, motor control, and high-speed data acquisition. But its power is often bottlenecked by the manual coding loop. By connecting Teensy 4.x to ASI Biont, you offload decision‑making to an AI agent that can understand context, correlate multiple data streams, and trigger actions instantly.
This article walks through a concrete integration: a Teensy 4.x reading a temperature sensor and controlling a relay (fan), with the AI agent monitoring via chat alerts and automating responses when temperatures exceed thresholds.
Why Connect a Teensy 4.x to an AI Agent?
Traditional firmware has fixed logic: if (temp > threshold) turn on fan. It works, but adapting to new conditions means rewriting code. With an AI agent:
- Adaptive logic: The agent can learn from historical data, adjust thresholds, or even predict failures.
- Zero manual coding for integration: You describe your setup in plain English, and ASI Biont writes the Python code to communicate with the Teensy.
- Real‑time millisecond response: The Hardware Bridge (bridge.py) runs locally on your PC, connected to the Teensy over USB serial. AI commands are routed through WebSocket to the bridge, which writes to the serial port and reads the reply instantly.
- Natural‑language control: “Start logging temperature every 5 seconds and send me an SMS if it exceeds 35°C” – the AI sets up the entire pipeline.
How ASI Biont Connects to Teensy 4.x
Teensy 4.x exposes a USB CDC serial port (e.g., COM3 on Windows, /dev/ttyACM0 on Linux). The correct integration method is Hardware Bridge (item #13 in the official list). You run a small Python application (bridge.py) on your computer, which connects to ASI Biont’s cloud via WebSocket. The AI uses the industrial_command tool with the serial_write_and_read command to send and receive data over the serial port.
Important: The bridge does NOT run an HTTP server. All communication goes through a single WebSocket channel. Commands must be sent via the chat using the special tool.
Architecture Overview
| Component | Role |
|---|---|
| Teensy 4.x | Runs firmware (Arduino/PlatformIO) that reads sensors, controls relays, accepts text commands via Serial. |
| bridge.py | Python script on your PC, connects to ASI Biont via WebSocket, forwards serial_write_and_read commands to the Teensy’s COM port. |
| ASI Biont cloud | AI agent that processes user messages, generates Python scripts (execute_python), and issues industrial_command calls. |
| User (chat) | Describes the integration in natural language, receives alerts, sends commands. |
Step‑by‑Step Integration: Temperature Monitoring with Telegram Alerts
Let's build a system where a Teensy 4.x reads a DS18B20 temperature sensor and controls a relay module (e.g., to turn on a fan). The AI agent will monitor the temperature, warn the user when it exceeds 30°C, and automatically toggle the fan.
1. Teensy 4.x Firmware
We’ll write a simple Arduino sketch that implements the HELP protocol—the standard way to let ASI Biont discover available commands. The firmware listens for HELP and returns a list of commands, then parses incoming commands like FAN_ON, FAN_OFF, GET_TEMP.
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 10
#define RELAY_PIN 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
sensors.begin();
}
void handleCommand(const String& cmd) {
if (cmd == "HELP") {
Serial.println("FAN_ON\nFAN_OFF\nGET_TEMP");
} else if (cmd == "FAN_ON") {
digitalWrite(RELAY_PIN, HIGH);
Serial.println("FAN_ON_OK");
} else if (cmd == "FAN_OFF") {
digitalWrite(RELAY_PIN, LOW);
Serial.println("FAN_OFF_OK");
} else if (cmd == "GET_TEMP") {
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
Serial.println(String(temp, 2));
} else {
Serial.println("UNKNOWN");
}
}
void loop() {
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
handleCommand(cmd);
}
}
2. Running the Hardware Bridge
Download bridge.py from the ASI Biont dashboard (Devices → Create API Key → Download bridge). No GitHub link exists; it's a single-file script. Run it with:
pip install pyserial requests websockets
python bridge.py --token=YOUR_API_KEY --ports=COM3 --baud=115200
Replace COM3 with your Teensy’s port (e.g., /dev/ttyACM0 on Linux). The bridge will connect to ASI Biont and begin listening for commands. You should see a WebSocket connection established and the bridge ready.
3. Connecting the AI Agent – No Dashboard, Just Chat
Now go to the ASI Biont chat interface. You simply describe your setup:
“I have a Teensy 4.x on COM3 at 115200 baud. It has a DS18B20 temperature sensor on pin 10 and a fan relay on pin 2. Commands: GET_TEMP returns temperature, FAN_ON/FAN_OFF control the fan. Monitor temperature every 10 seconds; if it exceeds 30°C, turn on the fan and send me a Telegram message. If it drops below 28°C, turn off the fan and notify me.”
The AI agent will:
- Verify the connection by sending a HELP command via
industrial_command(protocol='serial://', command='serial_write_and_read', data='48454c500a')(HELP in hex). - Parse the response to learn available commands.
- Write a Python script (using
execute_python) that runs periodically in the sandbox, sendingGET_TEMPand making decisions. Becauseexecute_pythonruns in the cloud and cannot access COM ports directly, the script will useindustrial_commandto interact with the bridge.
Example AI‑generated monitoring script (for sandbox):
import asyncio
from asi_biont import industrial_command # available in sandbox
async def monitor():
# Send GET_TEMP command
result = await industrial_command(
protocol='serial://',
command='serial_write_and_read',
data='4745545f54454d500a' # GET_TEMP in hex
)
temperature = float(result.strip())
if temperature > 30.0:
# Turn fan ON
await industrial_command(
protocol='serial://',
command='serial_write_and_read',
data='46414e5f4f4e0a' # FAN_ON in hex
)
# Send Telegram alert
await send_telegram(f"⚠️ Temp {temperature}°C – fan turned ON")
elif temperature < 28.0:
# Turn fan OFF
await industrial_command(
protocol='serial://',
command='serial_write_and_read',
data='46414e5f4f46460a' # FAN_OFF in hex
)
await send_telegram(f"✅ Temp {temperature}°C – fan turned OFF")
asyncio.run(monitor())
The sandbox has a 30‑second timeout, so the script runs, performs one check, and finishes. The AI can set up a cron‑like scheduler in the chat to run this script every 10 seconds.
4. Real‑Time Control Through Chat
You can also send ad‑hoc commands directly in the chat:
“Turn the fan on for 5 minutes”
The AI will call industrial_command to send FAN_ON, then schedule a delayed FAN_OFF using asyncio.sleep within another sandbox execution.
All responses (timeouts, errors) are reported back to you. For example, if the Teensy is unplugged, the bridge will return an error, and the AI will inform you and suggest reconnecting.
Beyond Simple Monitoring: FlexIO, SPI, I2C
Teensy 4.x’s FlexIO can emulate custom peripheral interfaces. If you need to connect, say, an external ADC via SPI, you can extend the firmware to expose SPI read commands over serial. The AI can then request SPI_READ_REG 0x01 and parse the returned raw bytes. Because everything goes through the atomic serial_write_and_read, the AI can handle binary data by sending hex strings.
Pitfalls to Avoid
- Don’t use
while Truein execute_python scripts – sandbox kills scripts after 30 seconds. Use scheduled one‑shot executions or let the AI loop via chat. - Hex encoding – Always encode commands as hex strings (e.g.,
0afor newline). The_parse_data_field()in bridge.py handles both hex and escape sequences, but hex is safest. - Windows overlay I/O – If writes return 0 bytes, bridge.py automatically falls back to synchronous
WriteFile(CancelIoEx + PurgeComm). This is handled automatically, but ensurepyserialis up to date. - HELP protocol – Your Teensy firmware must respond to
HELP(orHELP\n) with a list of commands, otherwise the AI will not know what commands are available and integration will fail.
Why This Changes Everything
With ASI Biont, you don’t write the glue code. You don’t configure MQTT brokers or REST API endpoints for a simple temperature monitor. The AI agent does the heavy lifting:
- It writes the Python code to communicate with the Hardware Bridge.
- It generates the Teensy firmware (you can ask “Give me the Arduino sketch for this setup”).
- It sets up Telegram alerts, data logging, and even anomaly detection with no manual scripting.
You focus on building the hardware and describing what you need. The AI handles the rest.
Try It Yourself
- Grab a Teensy 4.x, a DS18B20, and a relay module.
- Wire them up (Dallas sensor on pin 10, relay on pin 2).
- Upload the sketch above.
- Go to asibiont.com, create an API key, download
bridge.py. - Run the bridge with your port and token.
- Start a chat with the AI and describe your setup – watch it connect and start controlling your hardware in seconds.
No other platform lets you talk to a microcontroller like this. Experience the future of embedded integration today.
Comments