Skip to content

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 PinTTL-to-RS485 ModuleDescription
GPIO 16TXDSerial transmit
GPIO 17RXDSerial receive
GPIO 4 (optional)DE/REDirection control
3.3VVCCPower
GNDGNDGround

RS485 to Soil Sensor ​

RS485 ModuleSoil SensorDescription
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) ​

json
{
  "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) ​

json
{
  "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 ​

ParameterDescription
baudRateCommon baud rate for soil sensors is 4800 (some use 9600)
soil_moistureSoil volumetric moisture content (0-100%)
soil_temperatureSoil temperature (-40~80°C)
soil_ecSoil electrical conductivity (0-20000 μS/cm)
phAcidity/alkalinity (3.0-9.0)

Data Reporting ​

Sensor data is reported via MQTT:

json
[{"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

  1. Click Peripheral Execution in the left menu
  2. Click New Rule button
  3. Fill in basic configuration:
    • Rule Name: Auto Irrigation
    • Report Data: ✅ Enabled
    • Enable: ✅ Enabled

Step 3: Configure Trigger (Polling Trigger)

  1. Click Add Trigger button

  2. Fill in trigger configuration:

    FieldValueDescription
    Trigger TypeSelect Polling TriggerPeriodic condition check
    Target PeripheralSelect modbus_soilSoil sensor
    Data Fieldsoil_moistureSoil moisture
    Operator<Less than
    Threshold3030%
    Polling Interval1000010 seconds

Step 4: Configure Actions (3 actions required)

Action 1: Open Irrigation Valve

  1. Click Add Action button
  2. Fill in:
    • Action Type: Select Modbus Write
    • Target Peripheral: Select modbus_relay
    • Register: 1 (CH2)
    • Value: 65280 (Close)

Action 2: Delay 1 Minute

  1. Click Add Action button again
  2. Fill in:
    • Action Type: Select Delay
    • Delay Time: 60000 (60000ms = 1 minute)

Action 3: Close Irrigation Valve

  1. Click Add Action button a third time

  2. Fill in:

    • Action Type: Select Modbus Write
    • Target Peripheral: Select modbus_relay
    • Register: 1 (CH2)
    • Value: 0 (Open)
  3. Click Save button


Scenario 2: Soil Data Display (Timer Trigger) ​

Feature: Periodically display soil data on OLED

Configuration Steps:

  1. Create rule, name: Display Soil Data
  2. Trigger configuration:
    • Trigger Type: Select Timer Trigger
    • Interval: 10000 (10 seconds)
  3. 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}
  4. Click Save

Notes ​

  1. Baud Rate: Soil sensors commonly use 4800bps; must be unified when sharing bus with other 9600 devices
  2. Burial Depth: Probe burial depth affects readings; generally 10-20cm (crop root zone) recommended
  3. Waterproofing: Probe is waterproof but connector end is not; waterproofing treatment needed for outdoor installation
  4. Calibration: Factory calibrated, but accuracy degrades over long-term use; periodic comparison calibration recommended
  5. Sampling Interval: Soil parameters change slowly; interval of 10000-30000ms is sufficient
  6. Multi-Point Monitoring: For large-area cultivation, deploy multiple sensors (different slave addresses) for comprehensive data

FastBee | fastbee.cn