Introduction
Imagine a garden that waters itself based on real-time weather data and soil conditions — without you lifting a finger. This is now possible by integrating a rain sensor and a soil moisture sensor with the AI agent ASI Biont. Traditional IoT setups require custom firmware, a cloud dashboard, and manual rule programming. With ASI Biont, you simply describe your hardware in a chat, and the AI writes the integration code, connects to the sensors via MQTT or UART (through Hardware Bridge), and creates intelligent automation rules. In this article, we’ll walk through a complete use case: an ESP32-based sensor station that monitors rain and soil moisture, sends data to ASI Biont, and triggers automatic irrigation and Telegram alerts.
Why Connect Rain / Soil Moisture Sensors to an AI Agent?
- Real-time decision making: AI can analyze moisture trends, weather forecasts (via API), and historical data to decide when to water.
- No manual scripting: You don’t need to learn MQTT or serial protocols — ASI Biont writes the Python/MicroPython code for you.
- Flexible automation: Create rules like “if soil moisture < 30% and no rain in last 6 hours → turn on relay for 10 minutes → send Telegram alert”.
- Edge + cloud hybrid: Sensors connect via local bridge (UART) or MQTT broker; AI runs in the cloud and sends commands back.
Connection Methods: Which One and Why
For this scenario, we use two methods in parallel:
| Sensor | Protocol | Reason |
|---|---|---|
| Soil moisture (analog) | UART via Hardware Bridge | Direct reading from ESP32 ADC → serial → bridge → AI |
| Rain sensor (digital) | MQTT | ESP32 publishes rain status to Mosquitto broker; AI subscribes via paho-mqtt |
The Hardware Bridge (bridge.py) runs on your PC, connects to ASI Biont via WebSocket, and exposes local COM ports. The AI uses the industrial_command tool with serial_write_and_read to send requests and read sensor values.
Step-by-Step Use Case: ESP32 + Rain + Soil Moisture → ASI Biont
Hardware Setup
- ESP32 DevKit (or NodeMCU)
- Capacitive soil moisture sensor (e.g., v1.2)
- Rain sensor module (digital output: HIGH = dry, LOW = rain)
- Relay module (for pump/valve control)
- Jumper wires, breadboard
Wiring Diagram (Simplified)
ESP32 Sensor
------ ------
GPIO34 (ADC) ← Soil moisture (analog out)
GPIO15 ← Rain sensor (digital out)
GPIO2 → Relay (IN pin)
GND → GND (both sensors)
3.3V → VCC (both sensors)
Step 1: Flash ESP32 with MicroPython
Use Thonny or esptool to flash MicroPython firmware. Then upload the following script that reads sensors and publishes via MQTT:
# main.py - ESP32 MicroPython
import network
import time
from machine import Pin, ADC
from umqtt.simple import MQTTClient
# Wi-Fi credentials
SSID = "YourWiFi"
PASSWORD = "YourPass"
# MQTT broker (Mosquitto on Raspberry Pi or cloud)
BROKER = "192.168.1.100"
TOPIC_SOIL = "garden/soil_moisture"
TOPIC_RAIN = "garden/rain"
TOPIC_RELAY = "garden/relay"
# Pins
soil = ADC(Pin(34))
soil.atten(ADC.ATTN_11DB) # 0-3.6V range
rain = Pin(15, Pin.IN, Pin.PULL_UP)
relay = Pin(2, Pin.OUT)
relay.off()
# Connect Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
time.sleep(1)
# MQTT callback for relay control
def mqtt_callback(topic, msg):
if msg == b"ON":
relay.on()
elif msg == b"OFF":
relay.off()
client = MQTTClient("esp32_garden", BROKER)
client.set_callback(mqtt_callback)
client.connect()
client.subscribe(TOPIC_RELAY)
# Main loop
while True:
# Read sensors
soil_val = soil.read() # 0-4095 (dry = low, wet = high)
rain_val = rain.value() # 0 = rain detected, 1 = dry
# Publish
client.publish(TOPIC_SOIL, str(soil_val))
client.publish(TOPIC_RAIN, str(rain_val))
# Check for relay commands
client.check_msg()
time.sleep(30) # Send every 30 seconds
Step 2: Connect ASI Biont to MQTT Broker
In the ASI Biont chat, you describe:
“Connect to MQTT broker at 192.168.1.100:1883, subscribe to garden/soil_moisture and garden/rain. When soil moisture drops below 1500 and rain sensor shows dry (value=1), publish ‘ON’ to garden/relay for 10 minutes, then publish ‘OFF’. Also send a Telegram alert.”
ASI Biont writes and runs a Python script using execute_python:
import paho.mqtt.client as mqtt
import time
import requests
BROKER = "192.168.1.100"
TOPIC_SOIL = "garden/soil_moisture"
TOPIC_RAIN = "garden/rain"
TOPIC_RELAY = "garden/relay"
TELEGRAM_BOT_TOKEN = "YOUR_BOT_TOKEN"
TELEGRAM_CHAT_ID = "YOUR_CHAT_ID"
soil_value = None
rain_value = None
relay_timer = 0
def on_message(client, userdata, msg):
global soil_value, rain_value, relay_timer
topic = msg.topic
payload = msg.payload.decode()
if topic == TOPIC_SOIL:
soil_value = int(payload)
elif topic == TOPIC_RAIN:
rain_value = int(payload)
# Decision logic
if soil_value is not None and rain_value is not None:
if soil_value < 1500 and rain_value == 1 and relay_timer == 0:
client.publish(TOPIC_RELAY, "ON")
relay_timer = 10 * 60 # 10 minutes in seconds
# Send Telegram alert
msg_text = f"🌱 Watering started! Soil={soil_value}, Rain={rain_value}"
requests.post(f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
json={"chat_id": TELEGRAM_CHAT_ID, "text": msg_text})
client = mqtt.Client()
client.on_message = on_message
client.connect(BROKER, 1883, 60)
client.subscribe([(TOPIC_SOIL, 0), (TOPIC_RAIN, 0)])
client.loop_start()
# Timer loop (30s check, no while True to avoid timeout)
for _ in range(30): # runs ~15 minutes
time.sleep(30)
if relay_timer > 0:
relay_timer -= 30
if relay_timer <= 0:
client.publish(TOPIC_RELAY, "OFF")
requests.post(f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
json={"chat_id": TELEGRAM_CHAT_ID, "text": "💧 Watering finished."})
client.loop_stop()
Step 3: Alternative UART Connection (without MQTT)
If you prefer direct UART connection via Hardware Bridge, you connect ESP32 to PC via USB. In bridge.py you specify --ports=COM3 --baud 115200. Then in chat:
“Read soil moisture from COM3 using serial_write_and_read with command GET_SOIL. If value < 1500, send command RELAY_ON for 10 minutes, then RELAY_OFF. Send Telegram alert.”
The AI uses the industrial_command tool:
industrial_command(
protocol="serial://",
command="serial_write_and_read",
data="4745545f534f494c0a", # hex for "GET_SOIL\n"
port="COM3",
baud_rate=115200
)
Why This Integration Matters
- Zero-code setup: No need to write MQTT clients or serial parsers. ASI Biont generates production-ready code from natural language.
- Adaptive rules: You can later ask: “Also check weather API and skip watering if rain is forecast in next 3 hours.” AI updates the script instantly.
- Multi-protocol support: The same agent can simultaneously talk to MQTT devices, Modbus PLCs, and HTTP APIs — all in one conversation.
Conclusion
Connecting a rain and soil moisture sensor to ASI Biont transforms a simple sensor into an intelligent irrigation controller. Whether you use MQTT for cloud flexibility or UART for local control, the AI agent handles all the integration code, decision logic, and notifications. Try it yourself: describe your sensor setup in the chat at asibiont.com and watch your garden become self-aware.
Ready to automate your garden? Go to asibiont.com, create an API key, download bridge.py, and start chatting with your AI agent today.
Comments