Introduction
Imagine your DS18B20 temperature sensor not only reading values but also texting you when the freezer door is left open, predicting when your greenhouse needs ventilation, or whispering the temperature in your ear through a Telegram bot. That’s exactly what happens when you connect a cheap digital temperature sensor to an AI agent like ASI Biont.
In this guide, I’ll walk you through a real, practical integration: an ESP32 with a DS18B20 sensor talking to ASI Biont over MQTT. The AI agent handles everything — from reading the sensor via MQTT, to analyzing trends, to sending alerts through Telegram. No coding a dashboard, no manual cron jobs. Just chat with the AI, describe your setup, and let it write the integration code in seconds.
Why DS18B20 + AI?
The DS18B20 is one of the most popular digital temperature sensors for IoT projects. It’s cheap ($1-2), accurate (±0.5°C), waterproof, and uses the 1-Wire protocol — meaning you can string multiple sensors on a single GPIO pin. It’s used everywhere: from homebrew fermentation chambers to industrial cold storage monitoring.
But raw sensor data is just numbers. The real value comes from connecting it to an AI agent that can:
- Continuously monitor trends and detect anomalies (e.g., unexpected temperature spikes)
- Control actuators (relays, fans, heaters) based on rules or predictions
- Send alerts via chat apps (Telegram, Slack) or voice assistants
- Provide predictive analytics: “Your freezer will reach 0°C in about 20 minutes if you don’t close the door.”
ASI Biont makes this trivial because you don’t have to write any integration code yourself. You describe your hardware and goals in plain English, and the AI writes and runs the Python scripts using real libraries (pyserial, paho-mqtt, aiohttp, etc.) inside its sandbox environment.
Connection Method: MQTT
For this guide, we’ll use MQTT — the de facto standard for IoT messaging. Here’s why:
| Factor | Why MQTT fits DS18B20 + AI |
|---|---|
| Lightweight | A single DS18B20 reading is a few bytes; MQTT overhead is minimal |
| Pub/Sub | ESP32 publishes temperature; AI subscribes; multiple clients can listen |
| Reliable | QoS levels ensure no message loss even over flaky Wi-Fi |
| Widely supported | Every ESP32 library, Mosquitto broker, and ASI Biont supports MQTT |
Alternative methods could be:
- COM port (Hardware Bridge) if your sensor is connected to a PC via USB-UART
- SSH if you’re using a Raspberry Pi with the sensor on GPIO
- Modbus/TCP for industrial PLC environments
But for a standalone ESP32, MQTT is the simplest and most scalable.
Step-by-Step Integration
1. Hardware Setup
You’ll need:
- ESP32 board (e.g., ESP32 DevKit v1)
- DS18B20 sensor (any variant: TO-92, waterproof probe)
- 4.7 kΩ resistor (pull-up for 1-Wire)
- Breadboard and jumper wires
Wiring:
| DS18B20 Pin | ESP32 Pin |
|-------------|-----------|
| Red (VCC) | 3.3V |
| Black (GND) | GND |
| Yellow (DATA) | GPIO4 |
| Pull-up resistor between DATA and VCC | |
2. ESP32 Code (MicroPython)
Flash your ESP32 with MicroPython. Then upload this script that reads temperature and publishes to an MQTT broker every 30 seconds:
import network
import time
import onewire
import ds18x20
from machine import Pin
from umqtt.simple import MQTTClient
# Wi-Fi credentials
WIFI_SSID = "your_ssid"
WIFI_PASS = "your_password"
# MQTT broker (use a public test broker or your own)
MQTT_BROKER = "test.mosquitto.org"
MQTT_TOPIC = b"ds18b20/temperature"
CLIENT_ID = "esp32_01"
# DS18B20 on GPIO4
ds_pin = Pin(4)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = ds_sensor.scan()
print(f"Found DS18B20 sensors: {len(roms)}")
# 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)
print("Wi-Fi connected")
# MQTT client
client = MQTTClient(CLIENT_ID, MQTT_BROKER)
client.connect()
print("MQTT connected")
# Main loop
while True:
ds_sensor.convert_temp()
time.sleep_ms(750)
for rom in roms:
temp = ds_sensor.read_temp(rom)
msg = f"{temp:.2f}".encode()
client.publish(MQTT_TOPIC, msg)
print(f"Published: {temp:.2f}°C")
time.sleep(30)
3. Connect ASI Biont to the MQTT Broker
Now, open ASI Biont chat and describe your setup. For example:
“Connect to MQTT broker test.mosquitto.org, subscribe to topic ds18b20/temperature, and read the temperature values. If the temperature exceeds 30°C, send me a Telegram alert. Also, log all readings to a CSV file every hour.”
The AI will generate and execute a Python script using paho-mqtt inside its sandbox. Here’s a simplified version of what it writes:
import paho.mqtt.client as mqtt
import csv
from datetime import datetime
import json
TELEGRAM_BOT_TOKEN = "your_bot_token"
TELEGRAM_CHAT_ID = "your_chat_id"
# Callback when message received
def on_message(client, userdata, msg):
temp = float(msg.payload.decode())
timestamp = datetime.now().isoformat()
# Log to CSV
with open("temperature_log.csv", "a", newline="") as f:
writer = csv.writer(f)
writer.writerow([timestamp, temp])
# Alert if > 30°C
if temp > 30:
import requests
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
requests.post(url, json={"chat_id": TELEGRAM_CHAT_ID, "text": f"🔥 Temperature exceeded 30°C: {temp}°C"})
print(f"Received: {temp}°C at {timestamp}")
client = mqtt.Client()
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
client.subscribe("ds18b20/temperature")
client.loop_forever()
Important: The AI runs this script in the sandbox with a 30-second timeout for execute_python calls. For long-running MQTT subscriptions, the AI uses industrial_command with the MQTT protocol, which supports persistent listeners. The code above is just for illustration — in practice, you’d describe the task and ASI Biont handles the subscription lifecycle.
4. Automate Control via Chat
You can also control actuators. For example, say:
“If temperature drops below 5°C, send an MQTT command to ESP32 topic ‘heater/control’ with payload ‘ON’.”
The AI will add a rule to the MQTT callback that publishes a message back to the ESP32, which can then toggle a relay.
Why This Approach Beats Manual Coding
| Aspect | Traditional | With ASI Biont |
|---|---|---|
| Time to first reading | Hours (wiring + code + debugging) | Minutes (describe setup in chat) |
| Adding notifications | Write custom Telegram bot, handle auth | One sentence: “Send alerts to Telegram” |
| Changing logic | Edit code, reflash ESP32 | Just tell AI: “Now log to Google Sheets” |
| Predictive analytics | Requires ML expertise | Ask: “Predict when temp will hit 40°C” |
Real-World Use Cases
- Smart greenhouse: Monitor temperature and humidity, control fans via MQTT relay, get daily summaries.
- Cold chain monitoring: Log temperature of vaccine refrigerators, alert if out of range for more than 10 minutes.
- Home automation: Integrate with home assistant — if DS18B20 in bedroom reads < 18°C, turn on smart heater via HTTP API.
Potential Pitfalls & How to Avoid Them
- MQTT broker not reachable: Use a public broker (test.mosquitto.org) for testing, but switch to a local broker (Mosquitto on Raspberry Pi) for production. ASI Biont can connect to any broker — just provide the IP.
- 1-Wire timing issues: If using multiple DS18B20s on one wire, ensure total cable length < 100m and use a stronger pull-up (2.2kΩ). The AI can help diagnose if you describe symptoms.
- Sandbox timeout: For long-running tasks, use
industrial_commandwith MQTT protocol instead ofexecute_python. The AI will automatically choose the right tool. - Security: Never hardcode credentials in posts to public repos. Use environment variables in the AI chat (e.g., “use env var TELEGRAM_BOT_TOKEN”).
Conclusion
Connecting a DS18B20 temperature sensor to ASI Biont turns a simple $2 component into a smart, proactive monitoring system. You get real-time alerts, historical logging, and predictive insights — all without writing a single line of boilerplate. The AI handles the integration, so you can focus on what matters: your data and your decisions.
Ready to try it? Head over to asibiont.com, open the chat, and describe your sensor setup. In under a minute, you’ll have your DS18B20 feeding data into an AI that watches over your environment 24/7.
Comments