Teensy 4.x + ASI Biont: Real-Time Sensor Data and Actuator Control via AI Agent – A Step-by-Step Integration Guide

Introduction

The Teensy 4.x is a powerful microcontroller based on the NXP i.MX RT1062 ARM Cortex-M7 processor, running at 600 MHz. It offers 2 MB Flash, 1 MB RAM, and hardware support for real-time audio processing, high-frequency sampling (up to 2 MSPS), and multiple communication interfaces (UART, SPI, I2C, CAN, USB). While Teensy excels at raw performance, its real potential unlocks when it becomes part of an AI-driven automation system. ASI Biont is an AI agent that connects to any hardware device through natural language chat. Instead of writing complex firmware or dashboards, you simply describe your task, and the AI generates the integration code on the fly. This article shows how to connect Teensy 4.x to ASI Biont using a COM port via Hardware Bridge and MQTT, with concrete code examples for sensor data collection and actuator control. All integration happens in the chat – no manual coding required.

Why Connect Teensy 4.x to an AI Agent?

Teensy is often used in projects requiring low latency and high throughput: audio effects, real-time control loops, data acquisition. However, managing data flows, triggering actions based on conditions, and logging results typically require custom scripts. ASI Biont eliminates that overhead. The AI agent can:
- Read analog and digital sensor values in real time
- Send commands to actuators (servos, LEDs, relays)
- Log data to cloud databases or send alerts via Telegram
- Trigger complex automation based on sensor thresholds or schedules

All this is done by the AI writing the integration code in Python (using pyserial, paho-mqtt, or execute_python) and executing it – you only provide the connection parameters.

Connection Methods for Teensy 4.x

ASI Biont supports multiple protocols. For Teensy, two methods are most relevant:

Method Protocol Use Case
COM port via Hardware Bridge serial:// (RS-232/RS-485) Direct USB connection to PC, real-time read/write, low latency
MQTT via execute_python mqtt:// (paho-mqtt) Wireless control via Wi-Fi (Teensy 4.x + ESP8266/ESP32 shield), cloud messaging

Hardware Bridge is a lightweight bridge.py application that runs on your Windows/Linux/macOS computer, connects to ASI Biont via WebSocket, and provides access to local COM ports. The AI sends commands through the industrial_command tool with serial_write_and_read(data=hex_string). Data is exchanged in hex format (e.g., data="48454c500a" for HELP\n). The bridge handles low-level serial I/O including Windows overlapped I/O fixes (CancelIoEx, PurgeComm).

MQTT uses the pub/sub model. The AI, running in ASI Biont's cloud sandbox, writes a Python script with paho-mqtt that subscribes to a topic and publishes commands. The Teensy (with an ESP8266 or ESP32 co-processor) connects to the same MQTT broker and acts on messages.

Use Case 1: Real-Time Analog Sensor Monitoring via COM Port

Scenario: You have a Teensy 4.x connected to a potentiometer on analog pin A0 and a temperature sensor (LM35) on A1. You want the AI to read these values every second, display them in the chat, and send a Telegram alert if temperature exceeds 30°C.

Step 1: Teensy firmware – simple sketch that listens for commands and returns sensor data:

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (Serial.available()) {
    char cmd = Serial.read();
    if (cmd == 'R') {
      int pot = analogRead(A0);
      float temp = analogRead(A1) * 0.48876; // LM35: 10mV per degree
      char buf[64];
      snprintf(buf, sizeof(buf), "POT:%d,TEMP:%.2f\n", pot, temp);
      Serial.print(buf);
    }
  }
}

Step 2: Connect via Hardware Bridge – the user downloads bridge.py from the ASI Biont dashboard (Devices → Create API Key → Download bridge) and runs:

pip install pyserial requests websockets
python bridge.py --token=YOUR_TOKEN --ports=COM3 --baud 115200 --rate=10

Step 3: In chat, describe the task – the AI generates and executes the integration:

User: Connect to Teensy on COM3 at 115200 baud. Send 'R' every 1 second and parse the response. If TEMP > 30, send a Telegram alert.

The AI responds with the industrial_command sequence (simplified):

industrial_command(protocol='serial', command='serial_write_and_read', data='520a')  # 'R' + newline in hex

But the AI runs a continuous loop via execute_python (with 30-second timeout, no infinite loops) – it writes a Python script that sends the command and processes the response.

Step 4: Results – the chat shows live sensor values and alerts:

AI: POT:512, TEMP:25.3°C
AI: POT:520, TEMP:25.5°C
AI: POT:515, TEMP:30.2°C — Alert: Temperature exceeded 30°C. Sending Telegram...

Metrics improved:
- Response time: < 50 ms per read cycle
- Zero manual coding: integration took 30 seconds
- Alert latency: < 2 seconds from threshold crossing

Use Case 2: Wireless Actuator Control via MQTT

Scenario: You have a Teensy 4.x with an ESP8266 module (or ESP32) connected via UART. You want to control an RGB LED strip (on/off, color) and a servo motor from the chat.

Step 1: Teensy firmware (ESP8266 handles MQTT, Teensy controls hardware):

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* mqtt_server = "test.mosquitto.org";
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
  WiFi.begin("SSID", "password");
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void callback(char* topic, byte* payload, unsigned int length) {
  String msg = String((char*)payload).substring(0, length);
  if (strcmp(topic, "led") == 0) {
    if (msg == "ON") digitalWrite(LED_BUILTIN, HIGH);
    else digitalWrite(LED_BUILTIN, LOW);
  }
  if (strcmp(topic, "servo") == 0) {
    int angle = msg.toInt();
    // write to servo via PWM
  }
}

void loop() {
  if (!client.connected()) reconnect();
  client.loop();
}

Step 2: In chat, describe the task – the AI generates a Python script that runs in the sandbox:

User: Connect to MQTT broker test.mosquitto.org:1883. Subscribe to topic 'teensy/status'. Publish to 'led' and 'servo' when I say 'turn LED on' or 'set servo to 90'.

The AI writes:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    client.subscribe("teensy/status")

def on_message(client, userdata, msg):
    print(f"Status: {msg.payload.decode()}")

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
client.loop_start()

# AI reads user command and publishes:
client.publish("led", "ON")
client.publish("servo", "90")

Step 3: Results – the LED turns on, servo moves to 90°, and the Teensy publishes status back.

Metrics improved:
- Integration time: < 1 minute
- No MQTT client code written manually
- Reliable message delivery (QoS 1)

Why This Matters: The AI Handles Everything

Traditional integration requires writing firmware, debugging serial protocols, setting up MQTT brokers, and coding conditionals. With ASI Biont, you only need a working firmware on the Teensy. The AI agent:
- Automatically selects the correct protocol (COM port or MQTT)
- Generates the bridge configuration or Python script
- Handles error recovery (e.g., reconnection on MQTT failure)
- Lets you control devices through natural language

There is no dashboard, no "add device" button – everything happens in the chat. The AI uses execute_python to run any Python code in a sandbox with libraries for industrial protocols (pyserial, paho-mqtt, pymodbus, snap7, etc.). For COM port access, it uses the Hardware Bridge as a local relay.

Advanced Scenarios

  • Audio processing: Teensy 4.x has a dedicated audio shield. The AI can read FFT data via COM port and trigger actions based on frequency peaks.
  • High-frequency measurements: Teensy can sample at 2 MSPS. The AI sends a start command, collects a burst of data via USB, and analyzes it in the cloud.
  • Multi-device orchestration: With MQTT, the AI can coordinate multiple Teensy boards – one reads sensors, another controls actuators, all through a single chat session.

Conclusion

Teensy 4.x is a high-performance microcontroller, and ASI Biont makes it intelligent. Whether you need real-time sensor monitoring, wireless actuator control, or complex automation, the AI agent connects to your Teensy in seconds. No manual coding, no dashboards – just describe your task in the chat, and the integration happens automatically.

Try it yourself: Go to asibiont.com, create an API key, download bridge.py, and connect your Teensy. Start by saying: "Connect to Teensy on COM3 at 115200 baud and read analog pin A0 every second." The AI will handle the rest.

← All posts

Comments