Rain / Soil Moisture Sensor + ASI Biont: AI-Powered Smart Irrigation Without a Single Button

The problem: hands, hoses, and wasted water

If you've ever killed a plant by overwatering or watched a garden dry out while you were away, you know the struggle. Manual irrigation is reactive, imprecise, and wasteful. A rain / soil moisture sensor can fix that — but raw sensor data alone doesn't make decisions. You need an AI agent that reads the soil, checks whether it's raining, and controls the valve. That's exactly what ASI Biont does.

In this guide, I'll show you how to connect a rain / soil moisture sensor to ASI Biont through an ESP32, how to get Telegram alerts, and how to automate watering. I built this setup in my own greenhouse, and it cut my water usage by about a third during the hottest months. The total hardware cost is around $21, and the AI writes almost all the integration code for you.

What you need: hardware list

Component Purpose Approx. price
ESP32 DevKit Wi-Fi + GPIO brain $6
Capacitive soil moisture sensor (e.g., DFRobot SEN0193) Measures volumetric water content $10
Rain sensor module (e.g., FC-37) Detects droplets/rain $3
5V relay module + solenoid valve Turns irrigation on/off $2
Jumper wires, breadboard Wiring $1

Why capacitive? Resistive sensors corrode after a few weeks because of ion exchange in the soil. Capacitive sensors are more durable and don't need a separate electrode. I learned this the hard way when my cheap resistive probe read 0% after one season.

Wiring the ESP32

Connect the sensors to the ESP32 as shown below. Use a level shifter if your relay expects 5V logic, because the ESP32 GPIO outputs only 3.3V.

ESP32 pin Sensor / module
3V3 Soil sensor VCC, rain sensor VCC
GND Soil sensor GND, rain sensor GND, relay GND
GPIO34 Soil sensor analog output (AO)
GPIO35 Rain sensor digital output (DO)
GPIO26 Relay IN1

ESP32 firmware: read and publish

I use MicroPython because it's quick to write and easy to flash. Install MicroPython on your ESP32 (esptool or Thonny), then upload this script. It reads the soil moisture ADC value and the rain sensor state every 60 seconds, then publishes both to an MQTT broker.

import machine, time
from umqtt.simple import MQTTClient

soil = machine.ADC(machine.Pin(34), atten=machine.ADC.ATTN_11DB)  # 0-3.6V
rain = machine.Pin(35, machine.Pin.IN, machine.Pin.PULL_UP)
relay = machine.Pin(26, machine.Pin.OUT)

def mqtt_connect():
    client = MQTTClient('esp32_garden', '192.168.1.100', 1883)
    client.connect()
    return client

client = mqtt_connect()
while True:
    moisture = soil.read()      # ~1000 in water, ~3000 in dry air
    raining = rain.value()      # 1 = dry, 0 = rain
    client.publish('garden/soil', str(moisture).encode())
    client.publish('garden/rain', str(raining).encode())
    time.sleep(60)

This is a standard while True loop on the device — that's fine. The restriction only applies to ASI Biont's execute_python sandbox, which has a 30-second timeout. More on that later.

Connecting ASI Biont via MQTT

ASI Biont doesn't need a custom dashboard or a plugin. You simply describe your device in the chat:

Connect to MQTT broker at 192.168.1.100:1883. Topic garden/soil has the raw soil moisture ADC value (0-4095). Topic garden/rain is 1 when dry, 0 when raining. If soil moisture is above 2500 and rain is 1, publish 1 to garden/valve to open the relay, then send a Telegram message.

The AI agent understands the context and writes a Python script using paho-mqtt and requests. Here's what it generated for me:

import paho.mqtt.client as mqtt
import requests

BROKER = '192.168.1.100'
THRESHOLD_DRY = 2500
TELEGRAM_TOKEN = '123456:ABC_YOUR_TOKEN'
CHAT_ID = '987654321'

def send_telegram(text):
    requests.post(
        f'https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage',
        data={'chat_id': CHAT_ID, 'text': text}
    )

def on_message(client, userdata, msg):
    if msg.topic == 'garden/soil':
        moisture = int(msg.payload)
        if moisture > THRESHOLD_DRY:
            client.publish('garden/valve', '1')
            send_telegram('Soil is dry, watering for 30 seconds.')
    elif msg.topic == 'garden/rain':
        if msg.payload.decode() == '0':
            send_telegram('Rain detected, skipping irrigation.')

client = mqtt.Client()
client.on_message = on_message
client.connect(BROKER)
client.subscribe([('garden/soil', 0), ('garden/rain', 0)])
client.loop_forever()

Notice the script uses requests.post to the Telegram Bot API — there is no magic send_telegram() function. ASI Biont knows that Telegram access is just a standard HTTP request.

How the AI keeps the integration alive

A script that runs loop_forever() won't work in a 30-second sandbox. So ASI Biont treats this as a scheduled or event-driven task. You can tell the AI: “Run this analysis every 5 minutes” or “trigger it whenever a new MQTT message arrives.” The AI will wrap the logic appropriately. If you ever write raw code in execute_python yourself, remember: no while True, no long sleeps.

Alternative: wired serial connection with Hardware Bridge

If you prefer a wired setup, skip MQTT and connect the ESP32 to your PC over USB. On the device, replace the MQTT lines with a simple serial print:

print('soil=' + str(moisture) + ' rain=' + str(raining))

Then download bridge.py from the ASI Biont dashboard — it's not on GitHub, you get it from the dashboard after login. Launch it with:

python bridge.py --token=XXX --ports=COM3 --baud 115200 --rate=10

The bridge streams serial data into ASI Biont every 10 seconds. To send commands back to the relay, the AI uses the built-in industrial_command() function:

industrial_command(protocol='serial', command='RELAY_ON', port='COM3')

The bridge has no HTTP API, so industrial_command() is the only way to talk to it. This is a clean solution for
This is a clean solution for people who want a stable, low-latency link without extra network configuration. The bridge also handles reconnects and buffering, so you don't need to write any serial handling code yourself.

Choosing your integration path

Approach Best when
MQTT You already run a broker, or you want to control devices from anywhere on your network
Serial bridge You want a direct USB link, and your PC stays physically connected to the ESP32

Both paths expose the same sensor data to ASI Biont, and both support two-way communication. The only difference is the transport layer. If you're prototyping, start with the serial bridge — it's fewer moving parts. When your setup grows, you can migrate to MQTT without touching the AI logic.

The AI is the glue

What makes ASI Biont different is not the MQTT or serial libraries — those are standard. It's that the AI weaves them into a complete system. You give it a goal, like *

← All posts

Comments