How to Integrate DS18B20 Temperature Sensor with AI Agent ASI Biont: Step-by-Step MQTT & COM Port Guide

Introduction

Monitoring temperature is critical in industries like cold storage, server rooms, greenhouses, and manufacturing. The DS18B20 digital temperature sensor offers ±0.5°C accuracy over a -55°C to +125°C range and uses a unique 1-Wire interface, making it ideal for long-distance sensing with minimal wiring. However, raw sensor data alone is not enough — you need intelligent analysis, alerts, and automation. ASI Biont, an AI agent for hardware integration, connects directly to your DS18B20 setup via MQTT or COM port, processes readings, and triggers actions — all without writing a single line of integration code yourself. This article walks you through a real-world use case: ESP32 + DS18B20 → ASI Biont via MQTT, with Telegram alerts and relay control.

Why Connect DS18B20 to an AI Agent?

Traditional temperature monitoring requires manual dashboard configuration, threshold setting, and script writing. With ASI Biont, the AI agent handles the entire integration:
- Automatically reads sensor data (every 10 seconds, 1 minute, etc.)
- Analyzes trends (e.g., sudden rise, slow drift)
- Sends notifications via Telegram, email, or SMS
- Controls actuators (relays, fans, heaters) based on logic
- Logs historical data to CSV or database for compliance

All this happens through a chat conversation — you describe what you need, and the AI writes the code.

Connection Methods: MQTT and COM Port

ASI Biont supports multiple connection protocols. For DS18B20, two methods are most practical:

Method When to Use Setup Complexity
MQTT ESP32/ESP8266 with Wi-Fi; remote monitoring Low — just broker address and topic
COM Port Arduino/STM32 connected via USB; local PC Medium — requires bridge.py on PC

MQTT is preferred for wireless IoT setups. ASI Biont uses the paho-mqtt library to publish and subscribe. The AI writes a Python script that runs in the sandbox (execute_python) and communicates with your broker.

COM Port is used when your sensor is connected to a PC via a USB-to-serial adapter (e.g., Arduino Nano). ASI Biont connects through the Hardware Bridge — a lightweight bridge.py application that runs on your local machine, connects to the AI cloud via WebSocket, and forwards commands to the serial port.

Real Use Case: ESP32 + DS18B20 → ASI Biont via MQTT

Hardware Setup

  1. Components: ESP32 development board, DS18B20 sensor (TO-92 package), 4.7 kΩ resistor (pull-up), breadboard, jumper wires.
  2. Wiring:
  3. DS18B20 VDD (red) → ESP32 3.3V
  4. DS18B20 GND (black) → ESP32 GND
  5. DS18B20 DATA (yellow) → ESP32 GPIO 4, with 4.7 kΩ resistor between DATA and VDD
  6. Firmware: Upload MicroPython code that reads temperature and publishes to MQTT topic sensors/room1/temperature.

MicroPython Code for ESP32

import machine, onewire, ds18x20, time
import network
from umqtt.simple import MQTTClient

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

# MQTT broker (run Mosquitto locally or use cloud broker)
MQTT_BROKER = "192.168.1.100"
MQTT_TOPIC = b"sensors/room1/temperature"

# DS18B20 setup
ds_pin = machine.Pin(4)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_sensor.scan()

# Connect 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 client
client = MQTTClient("esp32_ds18b20", MQTT_BROKER)
client.connect()

while True:
    ds_sensor.convert_temp()
    time.sleep_ms(750)
    for rom in roms:
        temp = ds_sensor.read_temp(rom)
        client.publish(MQTT_TOPIC, str(temp))
        print("Published:", temp)
    time.sleep(10)  # publish every 10 seconds

Step 1: Configure MQTT Broker

You can run Mosquitto on a Raspberry Pi or use a public broker. For testing, test.mosquitto.org works, but for production, use a local broker for low latency.

Step 2: Tell ASI Biont to Connect

In the ASI Biont chat, simply type:

"Connect to MQTT broker at 192.168.1.100:1883, subscribe to topic sensors/room1/temperature. Read temperature every 30 seconds. If temperature exceeds 35°C, send a Telegram alert and publish '1' to topic actuators/fan. Log all readings to a CSV file."

The AI agent will generate a Python script using paho-mqtt and execute it in the sandbox. No coding required from you.

Step 3: AI Script Example (Generated by ASI Biont)

import paho.mqtt.client as mqtt
import csv
import time
from datetime import datetime

# Configuration
BROKER = "192.168.1.100"
TOPIC_TEMP = "sensors/room1/temperature"
TOPIC_FAN = "actuators/fan"
THRESHOLD = 35.0
LOG_FILE = "temperature_log.csv"

# Telegram alert function (simplified)
def send_telegram_alert(msg):
    import requests
    token = "YOUR_BOT_TOKEN"
    chat_id = "YOUR_CHAT_ID"
    url = f"https://api.telegram.org/bot{token}/sendMessage"
    requests.post(url, json={"chat_id": chat_id, "text": msg})

# MQTT callbacks
def on_message(client, userdata, msg):
    temp = float(msg.payload.decode())
    print(f"Received temperature: {temp}°C")

    # Log to CSV
    with open(LOG_FILE, 'a', newline='') as f:
        writer = csv.writer(f)
        writer.writerow([datetime.now(), temp])

    # Check threshold
    if temp > THRESHOLD:
        send_telegram_alert(f"⚠️ Temperature {temp}°C exceeds threshold!")
        client.publish(TOPIC_FAN, "1")  # turn on fan

# Connect and subscribe
client = mqtt.Client()
client.on_message = on_message
client.connect(BROKER, 1883, 60)
client.subscribe(TOPIC_TEMP)
print("Listening for temperature data...")
client.loop_forever()  # runs until stopped

This script runs in the ASI Biont sandbox (note: loop_forever is acceptable here because the sandbox timeout is 30 seconds only for execute_python; for long-running MQTT listeners, the AI uses a separate mechanism — the script can run as a persistent task).

Alternative: COM Port Connection

If your DS18B20 is connected to an Arduino that sends data over USB serial, use the Hardware Bridge:
1. Download bridge.py from ASI Biont dashboard (Devices → Create API Key → Download bridge).
2. Run: python bridge.py --token=YOUR_TOKEN --ports=COM3 --baud 9600
3. In chat: "Connect via COM3 at 9600 baud. Read temperature line every 5 seconds. If temperature > 30°C, send alert."

The AI will use serial_write_and_read(data="READ\n") to request data and parse the response.

Why This Integration Is Powerful

  • No coding: You describe the logic in plain English, AI writes the Python code.
  • No dashboard: Everything happens through chat — no need to click buttons or fill forms.
  • Flexible triggers: AI can combine multiple sensors, time schedules, and complex conditions.
  • Extensible: Add more DS18B20 sensors by simply publishing to different topics.

Conclusion

Integrating a DS18B20 temperature sensor with ASI Biont transforms a simple digital thermometer into an intelligent monitoring and automation system. Whether you use MQTT for wireless IoT or COM port for local USB connection, the AI agent handles the communication, analysis, and actions. Stop writing boilerplate code — let ASI Biont do the integration in seconds.

Ready to connect your DS18B20? Go to asibiont.com, create an account, and start chatting with the AI agent. Describe your sensor setup, and watch the AI bring your temperature monitoring to life.

← All posts

Comments