Example 48: Modbus Soil Sensor
Example 48: Modbus Soil Sensor
Experiment Overview
Reads RS485 soil sensor data via Modbus RTU protocol, monitoring soil temperature, moisture, and electrical conductivity (EC). Suitable for precision irrigation in smart agriculture, greenhouse cultivation, flower care, and similar scenarios.
Hardware Wiring
RS485 Communication Wiring
| ESP32 Pin | TTL-to-RS485 Module | Description |
|---|---|---|
| GPIO 16 | TXD | Serial transmit |
| GPIO 17 | RXD | Serial receive |
| GPIO 4 (optional) | DE/RE | Direction control |
| 3.3V | VCC | Power |
| GND | GND | Ground |
RS485 to Soil Sensor
| RS485 Module | Soil Sensor | Description |
|---|---|---|
| A+ | Yellow (A) | Differential signal positive |
| B- | Blue (B) | Differential signal negative |
Soil sensors typically require 12-24V DC power (red=VCC, black=GND); the probe portion is waterproof and can be buried in soil.
JSON Configuration Example
3-in-1 Soil Sensor (Temperature + Moisture + EC)
{
"id": "modbus_soil",
"name": "Soil Sensor",
"type": 1,
"enabled": false,
"pins": [16, 17],
"params": {
"baudRate": 4800,
"dataBits": 8,
"parity": "none",
"stopBits": 1,
"dePin": 4,
"protocol": "modbus_rtu",
"slaveId": 1,
"function": 3,
"startRegister": 0,
"registerCount": 3,
"interval": 10000,
"dataMap": [
{ "register": 0, "name": "soil_moisture", "scale": 0.1, "unit": "%" },
{ "register": 1, "name": "soil_temperature", "scale": 0.1, "unit": "°C" },
{ "register": 2, "name": "soil_ec", "scale": 1, "unit": "μS/cm" }
]
}
}5-in-1 Soil Sensor (Temperature + Moisture + EC + pH + NPK)
{
"id": "modbus_soil5",
"name": "Soil 5-in-1",
"type": 1,
"enabled": false,
"pins": [16, 17],
"params": {
"baudRate": 4800,
"dataBits": 8,
"parity": "none",
"stopBits": 1,
"dePin": 4,
"protocol": "modbus_rtu",
"slaveId": 2,
"function": 3,
"startRegister": 0,
"registerCount": 7,
"interval": 10000,
"dataMap": [
{ "register": 0, "name": "moisture", "scale": 0.1, "unit": "%" },
{ "register": 1, "name": "temperature", "scale": 0.1, "unit": "°C" },
{ "register": 2, "name": "ec", "scale": 1, "unit": "μS/cm" },
{ "register": 3, "name": "ph", "scale": 0.01, "unit": "" },
{ "register": 4, "name": "nitrogen", "scale": 1, "unit": "mg/kg" },
{ "register": 5, "name": "phosphorus", "scale": 1, "unit": "mg/kg" },
{ "register": 6, "name": "potassium", "scale": 1, "unit": "mg/kg" }
]
}
}Parameter Description
| Parameter | Description |
|---|---|
| baudRate | Common baud rate for soil sensors is 4800 (some use 9600) |
| soil_moisture | Soil volumetric moisture content (0-100%) |
| soil_temperature | Soil temperature (-40~80°C) |
| soil_ec | Soil electrical conductivity (0-20000 μS/cm) |
| ph | Acidity/alkalinity (3.0-9.0) |
Data Reporting
Sensor data is reported via MQTT:
[{"id": "modbus_soil", "value": "soil_moisture:45.3,soil_temperature:22.1,soil_ec:120"}]Peripheral Execution Linkage
Scenario 1: Auto Irrigation on Low Soil Moisture (Polling Trigger)
Feature: Automatically irrigate for 1 minute when soil moisture drops below 30%
Web UI Configuration Steps
Step 1: Ensure Peripherals are Configured
- Soil sensor:
modbus_soil - Modbus relay:
modbus_relay(controlling irrigation valve)
Step 2: Create Rule
- Click Peripheral Config in the left menu → Switch to Peripheral Execution tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Auto Irrigation - Report Data: ✅ Enabled
- Enable: ✅ Enabled
- Rule Name:
Step 3: Configure Trigger (Polling Trigger)
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Polling Trigger Periodic condition check Target Peripheral Select modbus_soilSoil sensor Data Field soil_moistureSoil moisture Operator <Less than Threshold 3030% Polling Interval 1000010 seconds
Step 4: Configure Actions (3 actions required)
Action 1: Open Irrigation Valve
- Click Add Action button
- Fill in:
- Action Type: Select Modbus Write
- Target Peripheral: Select
modbus_relay - Register:
1(CH2) - Value:
65280(Close)
Action 2: Delay 1 Minute
- Click Add Action button again
- Fill in:
- Action Type: Select Delay
- Delay Time:
60000(60000ms = 1 minute)
Action 3: Close Irrigation Valve
Click Add Action button a third time
Fill in:
- Action Type: Select Modbus Write
- Target Peripheral: Select
modbus_relay - Register:
1(CH2) - Value:
0(Open)
Click Save button
Scenario 2: Soil Data Display (Timer Trigger)
Feature: Periodically display soil data on OLED
Configuration Steps:
- Create rule, name:
Display Soil Data - Trigger configuration:
- Trigger Type: Select Timer Trigger
- Interval:
10000(10 seconds)
- Action configuration (2 actions required):
- Action 1: Select Sensor Read, target
modbus_soil - Action 2: Select Display Control, target
oled1, template:Soil Monitor M:{modbus_soil.soil_moisture}% T:{modbus_soil.soil_temperature}C EC:{modbus_soil.soil_ec}
- Action 1: Select Sensor Read, target
- Click Save
Notes
- Baud Rate: Soil sensors commonly use 4800bps; must be unified when sharing bus with other 9600 devices
- Burial Depth: Probe burial depth affects readings; generally 10-20cm (crop root zone) recommended
- Waterproofing: Probe is waterproof but connector end is not; waterproofing treatment needed for outdoor installation
- Calibration: Factory calibrated, but accuracy degrades over long-term use; periodic comparison calibration recommended
- Sampling Interval: Soil parameters change slowly; interval of 10000-30000ms is sufficient
- Multi-Point Monitoring: For large-area cultivation, deploy multiple sensors (different slave addresses) for comprehensive data
