How to Connect a 7-Segment Display (TM1637) to the ASI Biont AI Agent: A Practical IoT Automation Guide

Introduction

In the world of IoT and embedded systems, a 7-segment display (TM1637) is one of the most common output devices for showing numeric data — from temperature readings and countdown timers to machine status codes. But what if you could control that display not by writing firmware every time, but simply by chatting with an AI agent?

ASI Biont is an AI agent that connects to any device — including a TM1637 display — through execute_python, a sandboxed Python environment on the server. The user describes in natural language what to display, and the AI writes the integration code on the fly. No dashboard panels, no 'add device' buttons — just a conversation. This article shows a real use case: displaying sensor data from an ESP32 on a TM1637, controlled entirely by ASI Biont via MQTT.

Why Connect a 7-Segment Display to an AI Agent?

A TM1637 is a cheap, 4-digit 7-segment LED display controlled via I2C-like two-wire protocol (CLK and DIO). It’s widely used in clocks, thermometers, and industrial panels. Normally, you’d write Arduino or MicroPython code, flash it to a microcontroller, and hardcode the data source. With ASI Biont, you can:

  • Update the display remotely without reflashing — just tell the AI what to show.
  • Integrate with cloud data — show stock prices, weather, or server metrics.
  • Automate reactions — flash an alert when a sensor threshold is crossed.

The key advantage: no manual coding. The AI writes the Python script using paho-mqtt (for the cloud side) and MicroPython (for the ESP32 side), and you only need to describe the task.

Connection Architecture

Component Role Protocol
ESP32 Reads sensor (e.g., DHT22) and controls TM1637 I2C (GPIO) + MQTT client
MQTT broker (e.g., Mosquitto) Message relay MQTT (TCP/1883)
ASI Biont AI agent that subscribes to sensor data, decides what to display, publishes commands MQTT via paho-mqtt in execute_python

The flow:
1. ESP32 publishes temperature/humidity to sensor/data.
2. ASI Biont (in execute_python) subscribes to that topic, analyzes data, and if value > threshold, publishes display/show with the message.
3. ESP32 subscribes to display/show and writes to TM1637.

Wiring Diagram

Connect TM1637 to ESP32 as follows:

TM1637 Pin ESP32 GPIO
CLK GPIO 18
DIO GPIO 19
VCC 3.3V
GND GND

For a DHT22 temperature/humidity sensor:

DHT22 Pin ESP32 GPIO
DATA GPIO 4
VCC 3.3V
GND GND

Step 1: MicroPython Firmware for ESP32

The ESP32 runs a MicroPython script that:
- Connects to Wi-Fi and MQTT broker.
- Reads DHT22 every 10 seconds.
- Publishes data to sensor/data.
- Subscribes to display/show and writes to TM1637.

import network
import time
from machine import Pin, I2C
import dht
from tm1637 import TM1637
from umqtt.simple import MQTTClient

# Wi-Fi
WIFI_SSID = "your_ssid"
WIFI_PASS = "your_password"

# MQTT
BROKER = "192.168.1.100"  # your broker IP
CLIENT_ID = "esp32_tm1637"
TOPIC_SENSOR = b"sensor/data"
TOPIC_DISPLAY = b"display/show"

# Setup
i2c = I2C(0, scl=Pin(18), sda=Pin(19))  # TM1637 uses CLK/DIO on GPIO18/19
display = TM1637(clk=Pin(18), dio=Pin(19))
sensor = dht.DHT22(Pin(4))

# Wi-Fi connect
def connect_wifi():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(WIFI_SSID, WIFI_PASS)
    while not wlan.isconnected():
        time.sleep(1)
    print("WiFi connected")

# MQTT callback
def mqtt_callback(topic, msg):
    if topic == TOPIC_DISPLAY:
        text = msg.decode()
        try:
            display.show(text)  # e.g., "HELLO" or "1234"
        except:
            display.show("Err ")

# Main
connect_wifi()
client = MQTTClient(CLIENT_ID, BROKER)
client.set_callback(mqtt_callback)
client.connect()
client.subscribe(TOPIC_DISPLAY)
print("MQTT connected")

while True:
    try:
        sensor.measure()
        temp = sensor.temperature()
        hum = sensor.humidity()
        msg = f"{temp:.0f}C {hum:.0f}%"
        client.publish(TOPIC_SENSOR, msg)
        print(f"Published: {msg}")
        time.sleep(10)
    except OSError as e:
        print("Sensor error", e)
        time.sleep(5)
    client.check_msg()  # non-blocking check for display commands

Step 2: ASI Biont AI Agent Script (Cloud Side)

In the ASI Biont chat, the user says:

"Connect to MQTT broker at 192.168.1.100:1883. Subscribe to topic 'sensor/data'. If temperature exceeds 30°C, publish 'ALERT' to 'display/show' to make the TM1637 flash. Otherwise, publish the current temperature."

The AI writes and runs this Python script inside execute_python:

import paho.mqtt.client as mqtt
import json

BROKER = "192.168.1.100"
PORT = 1883
TOPIC_SENSOR = "sensor/data"
TOPIC_DISPLAY = "display/show"

latest_temp = None

def on_connect(client, userdata, flags, rc):
    print("Connected to broker")
    client.subscribe(TOPIC_SENSOR)

def on_message(client, userdata, msg):
    global latest_temp
    payload = msg.payload.decode()
    print(f"Received: {payload}")
    # Parse temperature: e.g., "25C 60%"
    try:
        temp_str = payload.split("C")[0]
        temp = float(temp_str)
        latest_temp = temp
    except:
        return

    if temp > 30:
        # Send alert to TM1637
        client.publish(TOPIC_DISPLAY, "ALERT")
        print("Published: ALERT")
    else:
        # Show temperature
        msg = f"{temp:.0f}C  "
        client.publish(TOPIC_DISPLAY, f"{temp:.0f}")
        print(f"Published: {msg}")

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(BROKER, PORT, 60)
client.loop_start()  # non-blocking loop; note: sandbox timeout is 30 seconds

# Keep script alive for 25 seconds to receive messages
import time
time.sleep(25)
print("Script finished. Latest temp:", latest_temp)

Important: The sandbox has a 30-second timeout, so the script uses loop_start() (non-blocking) and sleeps for 25 seconds. For long-running automation, you'd use the industrial_command tool with MQTT publish instead.

Real-World Scenario: Industrial Temperature Monitoring

A small manufacturing workshop uses an ESP32 with DHT22 and TM1637 to show ambient temperature. Before ASI Biont, the display was hardcoded. The owner wanted:
- Show temperature normally.
- Flash "HOT" if temperature exceeds 35°C.
- Send a Telegram alert to the manager.

With ASI Biont, they simply described this in the chat. The AI wrote the MQTT subscription script, added a Telegram notification via telegram-send (using the requests library), and configured the display/show topic. The whole integration took 2 minutes — no firmware update needed.

Benefits of Using ASI Biont for TM1637 Integration

  • Zero coding required — describe what you want, and the AI writes the integration.
  • Any protocol — MQTT, Modbus, HTTP, SSH — ASI Biont supports them all via execute_python.
  • Instant updates — change what the display shows without reflashing the microcontroller.
  • Cloud intelligence — combine sensor data with external APIs (weather, stock prices, server load).

Conclusion

Connecting a simple 7-segment display (TM1637) to an AI agent like ASI Biont transforms it from a fixed-purpose indicator into a dynamic, cloud-driven output device. The combination of an ESP32, MQTT, and ASI Biont's execute_python lets you automate data display, alerts, and remote control — all through natural language.

Try it yourself: Go to asibiont.com, start a chat, and say: "Connect to my MQTT broker at 192.168.1.100, subscribe to sensor/data, and publish the temperature to my TM1637 display. If it's too hot, flash an alert." The AI will handle the rest.

No dashboards. No buttons. Just conversation and automation.

← All posts

Comments