Smart Irrigation with ASI Biont: Connecting a Rain/Soil Moisture Sensor via ESP32 and MQTT
If you’ve ever killed a tomato plant by overwatering, you know the value of knowing exactly what’s happening in the soil. A rain/soil moisture sensor doesn’t just tell you when to water — it becomes the eyes and ears of an automated irrigation system when paired with an AI agent like ASI Biont.
ASI Biont is an AI agent that runs in the cloud and communicates with your devices through the same protocols your industrial controllers and hobbyist boards already speak: MQTT, Modbus, COM port, HTTP API, and more. In this guide, we’ll connect a capacitive soil moisture sensor to an ESP32, publish readings over MQTT, and let ASI Biont decide when to open a relay for the water pump — based on both real-time sensor data and a weather forecast.
Why connect a moisture sensor to an AI agent?
Moisture sensor readings alone are just numbers. The magic happens when the data is combined with context: expected rainfall, evaporation rate, plant type, and soil composition. ASI Biont can take a stream of MQTT messages, calculate a trend, and trigger an actuator exactly when needed. For example, it can learn to skip watering before a storm, saving water and keeping roots healthy.
Hardware setup: ESP32 + capacitive sensor
For this project, you need:
- An ESP32 development board (any variant with Wi-Fi)
- A capacitive soil moisture sensor (e.g., the DFRobot SEN0193)
- A relay module connected to a water pump or solenoid valve
- A 3.3V power supply (the ESP32 can be powered via USB)
Wire the sensor to the ESP32 as follows:
| Sensor pin | ESP32 pin |
|---|---|
| VCC | 3.3V |
| GND | GND |
| AOUT | GPIO34 |
The relay control pin goes to GPIO26 (through a transistor or optocoupler if needed). The sensor outputs an analog voltage that decreases with soil moisture, so a higher reading means drier soil — check your sensor’s datasheet for calibration.
MicroPython firmware: publish readings to MQTT
Here’s a simple MicroPython script that reads the sensor every 30 seconds and publishes the value as a percentage to an MQTT broker (in this case, the broker that ASI Biont can reach):
import machine
import network
import time
import ubinascii
from umqtt.simple import MQTTClient
SSID = "your_wifi"
PASSWORD = "your_pass"
MQTT_BROKER = "192.168.1.100"
MQTT_TOPIC = "sensor/soil_moisture"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
time.sleep(0.5)
sensor = machine.ADC(machine.Pin(34))
sensor.atten(machine.ADC.ATTN_11DB) # 0-3.3V range
client = MQTTClient(ubinascii.hexlify(machine.unique_id()), MQTT_BROKER)
client.connect()
def read_moisture():
raw = sensor.read() # 0-4095
dry = 2000 # calibrate for your sensor
wet = 800
percent = 100 * (raw - wet) / (dry - wet)
return max(0, min(100, percent))
while True:
moisture = read_moisture()
client.publish(MQTT_TOPIC, str(moisture))
print("Published:", moisture)
time.sleep(30)
The while True loop runs on the ESP32 itself, not in ASI Biont. The intelligence lives on the AI side.
Teaching ASI Biont to water your plants
Now comes the part that makes this integration special. You don’t need to configure MQTT topics in a dashboard or write a cloud function. You simply describe the task in ASI Biont’s chat. For example:
“Subscribe to the MQTT topic sensor/soil_moisture on 192.168.1.100. If the moisture drops below 25% at 8 AM and no rain is forecast in the next 12 hours, turn on the relay connected to GPIO26 for 5 minutes. Also send me a Telegram notification when watering starts.”
ASI Biont will generate a Python script using paho-mqtt, subscribe to the broker, and run its decision logic. Since the agent executes Python in a sandbox with a timeout, it doesn’t run an endless loop; instead, it runs short-lived scripts that poll the broker, evaluate conditions, and trigger actions. Here’s the kind of code it might produce:
import paho.mqtt.client as mqtt
moisture = None
def on_message(client, userdata, msg):
global moisture
moisture = float(msg.payload)
client = mqtt.Client()
client.on_message = on_message
client.connect("192.168.1.100", 1883)
client.subscribe("sensor/soil_moisture")
client.loop() # process one incoming message
if moisture is not None and moisture < 25 and forecast_no_rain_today():
water_now()
The AI adapts that script to your setup — it knows how to call water_now() from the connected relay, how to talk to the weather API, and how to send a Telegram message via requests.post.
The magic of execute_python: connect anything in seconds
The true power of ASI Biont isn’t just MQTT. Because the agent can execute arbitrary Python code via its execute_python feature, it can talk to almost any device — a Raspberry Pi over SSH, a Modbus PLC via TCP, a BACnet controller, or even a custom USB sensor via pyserial. You don’t need to wait for a built-in plugin. Just tell ASI Biont the device’s connection parameters (IP, port, baud rate, API key) and it will generate and run the integration code on the fly.
For this moisture sensor, you could even skip the ESP32 entirely and read a USB soil sensor directly from a computer using pyserial. The principle is the same: describe, and the AI connects.
Real-world scenario: greenhouse automation
Let’s put it all together. A small greenhouse with an ESP32 running the MicroPython script above publishes soil moisture and rain sensor readings. ASI Biont receives this data every 30 seconds. At the same time, the agent pulls a weather forecast from a public API (e.g., OpenWeatherMap) and checks if rain is expected.
Last season, a grower set up this system for a strawberry bed. The AI watered only when the soil moisture dropped below 30% and the forecast had less than 40% chance of rain. During a week of unexpected heat, it watered twice a day instead of once, which the grower confirmed was what the plants needed. When a thunderstorm hit, the system skipped its scheduled watering entirely.
This kind of context-aware automation is impossible with a simple timer. It’s the difference between a sprinkler and a gardener.
Why this matters
By bringing a soil moisture sensor into ASI Biont, you move from manual or timed irrigation to an adaptive system that reacts to the environment. The integration takes minutes, not days, because the AI agent handles the protocol details. And because it’s all done through chat, you can tweak the logic without touching code — or ask the AI to rewrite it as conditions change.
Ready to make your garden smarter? Connect your moisture sensor to ASI Biont and let the AI agent handle the rest. Start at asibiont.com.
Comments