Back to Home

Soil Sensor Data Compensation on ESP32: Scientific Models and Agro-Recommendations

The article describes open-source firmware for ESP32 microcontroller that provides scientific compensation of data from multifunctional soil sensor JXCT. Correction models, agro-recommendation system for 24 crops, and integration with popular IoT platforms are considered.

Precision Agro-Monitoring: Scientific Soil Sensor Compensation on ESP32
Advertisement 728x90

Scientific Data Compensation for ESP32 Soil Sensor: From Raw Readings to Precision Ag Recommendations

The JXCT 7-in-1 soil sensor delivers raw Modbus RTU data, but without temperature and moisture compensation, EC, pH, and NPK readings are seriously skewed. This open-source ESP32 firmware applies research-backed correction algorithms, integrates with leading IoT platforms, and provides tailored recommendations for 24 major crops.

System Architecture and Wiring

The system is built around an ESP32 microcontroller communicating with the sensor via RS485 using an SP3485E transceiver. The sensor needs a separate 12–24V power supply, while the ESP32 and SP3485E run on 3.3V.

Key Wiring Components:

Google AdInline article slot
  • ESP32 GPIO16 (RX) and GPIO17 (TX) for serial communication.
  • GPIO4 and GPIO5 to control the transceiver's DE/RE pins.
  • Essential common ground (GND) connection between ESP32, SP3485E, and sensor power supply.
  • Proper RS485 polarity (A+/B-) and 120Ω terminator on long cable runs.

Scientific Foundations of Measurement Compensation

The firmware uses a two-stage data processing pipeline. Stage one applies basic SensorCorrection with scaling and offset factors. Stage two, optional, leverages SensorCompensationService with peer-reviewed models.

Implemented Compensation Models:

  • Electrical Conductivity (EC): Rhoades et al. (1989) model. EC rises about 2.1% per °C above 25°C.

EC_comp = EC_raw × (1 + 0.021 × (T - 25))

Google AdInline article slot
  • pH: Nernst equation per Ross et al. (2008). pH drops 0.003 units per °C above 25°C.

pH_comp = pH_raw - 0.003 × (T - 25)

  • NPK Content: Delgado et al. (2020) model factoring temperature, moisture, and soil type (13 types, from sand to peat).

N_comp = N_raw × e^(δ_N(T-20)) × (1 + ε_N(θ-30))

The NutrientInteractionService also accounts for antagonistic (N↔K, P↔Zn) and synergistic (N+S, Ca+B) nutrient interactions.

Google AdInline article slot

Crop Recommendation Engine

CropRecommendationEngine includes profiles for 24 crops, from vegetables (tomatoes, cucumbers) and berries (strawberries, raspberries) to fruit trees (apples, grapes) and field crops (wheat, soybeans). Each has optimal ranges for all seven sensor parameters.

Factors Shaping Recommendations:

  • Soil type (13 classifications).
  • Season (spring, summer, fall, winter).
  • Growing environment (open field, greenhouse, indoor).

The system uses color-coded status indicators: normal, caution, warning, critical.

Calibration and Management

Calibration supports lab-based CSV uploads (raw/corrected pairs) or on-the-fly adjustments via REST API.

Key API Endpoints for Calibration:

  • POST /api/calibration/{parameter} — Add calibration point for a specific parameter.
  • GET /api/calibration/export & POST /api/calibration/import — Export/import calibration tables.
  • GET /api/correction/factors — Manage basic correction coefficients (slope/offset).

IoT Ecosystem Integration

Multiple output and control channels are supported.

Supported Platforms and Protocols:

  • Web Interface: Built-in responsive server for WiFi/MQTT setup, real-time data viewing, and firmware updates.
  • REST API (v1): Programmatic access to sensor data (/api/v1/sensor), diagnostics, and config management.
  • MQTT: Publishes to custom topics with delta filtering for changes only. Home Assistant Auto-Discovery supported.
  • ThingSpeak: Sends data to channels at configurable intervals with built-in validation.

Advanced Features and Deployment

Firmware includes reliability boosters: irrigation event detection via moisture spikes, geolocation-based seasonal adjustments, data filtering (Kalman, median), watchdog timer, and OTA updates with SHA256 integrity checks.

Quick Start for Developers:

  • Clone the repo: git clone https://github.com/Gfermoto/soil-sensor-7in1.git
  • Install dependencies: pip install -r requirements.txt
  • Build and flash firmware with PlatformIO: pio run -t upload
  • Upload filesystem: pio run -t uploadfs
  • Connect to ESP32 access point for initial network setup.

Tech stack: C++17, PlatformIO, Arduino framework, Modbus RTU. Licensed under AGPL-3.0.

Key Takeaways

  • Raw soil sensor data needs temperature and moisture compensation for accurate EC, pH, and NPK.
  • Firmware applies proven models (Rhoades, Nernst, Delgado) and nutrient interactions.
  • Tailored recommendations for 24 crops based on soil, season, and growing conditions.
  • Seamless integration with Home Assistant, MQTT, ThingSpeak, and REST API.
  • Open-source with full docs for custom adaptations.

— Editorial Team

Advertisement 728x90

Read Next