How to Connect an OLED Display (SSD1306, SH1106) to an AI Agent: A Practical Guide with ASI Biont

Introduction

In the world of IoT and embedded systems, OLED displays like the SSD1306 and SH1106 are ubiquitous. These tiny, low-power screens (128x64 or 128x32 pixels) are used in everything from weather stations to industrial dashboards. But what if you could control them—not with manual code, but by simply chatting with an AI agent? That's exactly what ASI Biont enables.

This article walks you through integrating an OLED display (I2C interface) with ASI Biont's AI agent. You'll learn how to output real-time sensor data, system status, and even simple animations—all driven by natural language commands. No dashboard, no complex setup. Just a conversation.

Why Connect an OLED to an AI Agent?

Traditionally, updating an OLED requires writing and flashing microcontroller firmware. With ASI Biont, you can:

  • Dynamic content: Change text, graphs, or animations on the fly via chat.
  • Remote monitoring: Display sensor readings from anywhere, without logging into a server.
  • Zero coding: The AI writes the integration code for you.

Connection Method: MQTT + ESP32

The most practical way to connect an OLED (SSD1306 or SH1106) to ASI Biont is via MQTT. An ESP32 runs a MicroPython script that subscribes to an MQTT topic. ASI Biont's AI agent publishes commands to that topic using its industrial_command tool with the publish command.

Why MQTT? Because:
- It's lightweight and works over Wi-Fi.
- The ESP32 can also read sensors (DHT22, BME280, etc.) and publish data back.
- ASI Biont's cloud sandbox has paho-mqtt pre-installed.

Step-by-Step Integration

1. Hardware Setup

Connect your OLED to the ESP32 using I2C:

OLED Pin ESP32 Pin
VCC 3.3V
GND GND
SCL GPIO 22
SDA GPIO 21

For SH1106, the wiring is identical, but the driver differs slightly.

2. MicroPython Code for ESP32

Below is a complete MicroPython script. It connects to Wi-Fi, initializes the OLED, subscribes to an MQTT topic, and displays any text or command it receives.

import network
import time
from machine import Pin, I2C
import ssd1306
from umqtt.simple import MQTTClient

# Wi-Fi credentials
WIFI_SSID = "YourWiFi"
WIFI_PASS = "YourPassword"

# MQTT settings
MQTT_BROKER = "test.mosquitto.org"
TOPIC = "asi/oled/display"

# Initialize I2C and OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)

# Connect to Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASS)
while not wlan.isconnected():
    time.sleep(1)

# MQTT callback
def callback(topic, msg):
    oled.fill(0)
    oled.text(msg.decode(), 0, 0)
    oled.show()

# Connect to MQTT broker
client = MQTTClient("esp32_oled", MQTT_BROKER)
client.set_callback(callback)
client.connect()
client.subscribe(TOPIC)

print("OLED ready. Waiting for messages...")
while True:
    client.wait_msg()

3. AI Agent Commands via ASI Biont

Once the ESP32 is running, you tell ASI Biont what to display. For example, in the chat:

"Connect to MQTT broker test.mosquitto.org and publish 'Temperature: 24.5°C' to topic asi/oled/display."

ASI Biont uses its industrial_command tool with:

industrial_command(
    protocol='mqtt',
    command='publish',
    params={
        'broker': 'test.mosquitto.org',
        'topic': 'asi/oled/display',
        'message': 'Temperature: 24.5°C'
    }
)

The display immediately updates. You can also send multi-line text, clear the screen (clear), or draw simple shapes by sending raw pixel commands (e.g., draw rect 10 10 50 30).

Real-World Use Case: IoT Sensor Dashboard

Scenario: A small factory monitors temperature and humidity in a storage room. An ESP32 reads a DHT22 sensor and publishes data to MQTT. The OLED shows the latest readings. ASI Biont analyzes trends and sends alerts via Telegram.

How it works:

  1. The ESP32 publishes {"temp": 24.5, "hum": 60} to asi/sensor/room1 every 10 seconds.
  2. ASI Biont subscribes to that topic via a Python script in execute_python:
import paho.mqtt.client as mqtt
import json

def on_message(client, userdata, msg):
    data = json.loads(msg.payload)
    temp = data['temp']
    hum = data['hum']
    # Publish to OLED display topic
    display_client.publish("asi/oled/display", f"Temp: {temp}C\nHum: {hum}%")
    # Check thresholds
    if temp > 30:
        # Send alert via Telegram (using requests)
        import requests
        requests.post("https://api.telegram.org/bot<TOKEN>/sendMessage",
                      json={"chat_id": "<CHAT_ID>", "text": f"High temperature: {temp}C"})

client = mqtt.Client()
client.on_message = on_message
client.connect("test.mosquitto.org")
client.subscribe("asi/sensor/room1")
client.loop_start()
  1. The OLED updates in real time. The user can also ask: "Show a graph of temperature over the last hour." ASI Biont generates a matplotlib chart, converts it to a bitmap, and sends pixel data to the OLED (for 128x64, it sends a 1024-byte array).

Why This Matters

  • No manual coding: You describe the task; the AI writes and executes the code.
  • Instant changes: Update the display without reflashing the microcontroller.
  • Scalable: Add more OLEDs or sensors by simply describing them in chat.

Other Integration Methods for OLED

While MQTT is the most flexible, ASI Biont supports other protocols for OLED control:

Method Use Case
COM port Direct USB connection to Arduino (e.g., Arduino Uno + OLED). Use Hardware Bridge with serial:// protocol.
SSH Connect to a Raspberry Pi running a Python script that drives an OLED via GPIO.
HTTP API If the OLED is part of a smart display with a REST endpoint (e.g., a commercial e-ink or OLED panel).

Example: COM Port with Arduino

Connect an Arduino Uno with an OLED to your PC. Run bridge.py with --ports=COM3 --baud 115200. Then in ASI Biont chat:

"Send 'Hello from AI' to the OLED on COM3."

The AI uses:

industrial_command(
    protocol='serial',
    command='write_and_read',
    params={
        'data': '48456c6c6f2066726f6d2041490a',  # hex for 'Hello from AI\n'
        'port': 'COM3'
    }
)

The Arduino receives it and displays the text.

Conclusion

Integrating an OLED display (SSD1306 or SH1106) with ASI Biont turns a static peripheral into a dynamic, AI-controlled output device. Whether you're building a home dashboard, an industrial monitor, or a smart notification system, the process is the same: connect the hardware, describe what you want in chat, and let the AI handle the code.

Try it yourself: Go to asibiont.com, create an API key, download the Hardware Bridge, and ask the AI to connect your OLED. No coding required—just a conversation.

← All posts

Comments