MatterSoilSensor

About

The MatterSoilSensor class provides a soil sensor endpoint for Matter networks (device type 0x0045). This endpoint implements the Matter Soil Measurement cluster (0x0430) as a code-driven CHIP server object.

Features:

  • Soil moisture reporting from 0–100%.

  • Read-only measurement path via setSoilMoisture() (no sub-percent precision).

  • Built on the esp_matter::endpoint::soil_sensor device-type wrapper with a code-driven Soil Measurement cluster.

  • Matter standard compliance.

Use Cases:

  • Garden and greenhouse monitoring.

  • Irrigation automation triggers.

  • Potted plant soil moisture logging.

API Reference

Constructor

MatterSoilSensor

Creates a new Matter soil sensor endpoint.

MatterSoilSensor();

Initialization

begin

Creates the endpoint (Descriptor, Identify, device type, Soil Measurement cluster placeholder). Call before Matter.begin(). The overload begin(uint8_t initialSoilMoisturePercent) additionally seeds an initial whole-percent reading, which is cached until the live cluster is available.

bool begin();
bool begin(uint8_t initialSoilMoisturePercent);

begin() takes no initial moisture value. The measured attribute stays null until the first successful setSoilMoisture().

begin(uint8_t) does the same and then seeds the moisture cache. initialSoilMoisturePercent is a whole number in [0..100]. When this call happens before Matter.begin(), the value is applied in onStackStarted() once the live cluster is registered.

Both return true if successful, false otherwise.

Typical usage:

SoilSensor.begin();
Matter.begin();
SoilSensor.setSoilMoisture(readingPercent);

The live SoilMeasurementCluster is constructed when the Matter stack starts. setSoilMoisture() may be called after the endpoint begin() even before Matter.begin(): the value is cached and applied once the live cluster is registered.

end

Stops processing Matter soil sensor events.

void end();

Soil moisture

setSoilMoisture

Sets the reported soil moisture (0–100%) after endpoint begin(). Before Matter.begin() the value is cached and applied when the live cluster is registered; afterward it updates the live cluster directly.

bool setSoilMoisture(uint8_t soilMoisturePercent);
  • soilMoisturePercent - Whole number in [0..100].

This function will return true if successful, false otherwise.

getSoilMoisture

Returns the cached soil moisture (0–100%). This is 0 until the first setSoilMoisture(). The cluster attribute stays null until a publish succeeds.

uint8_t getSoilMoisture();

Operators

uint8_t operators

Assignment and conversion operators call setSoilMoisture() / getSoilMoisture().

void operator=(uint8_t soilMoisturePercent);
operator uint8_t();

Implementation notes

  • SoilMoistureMeasuredValue is not available through getAttributeVal() / updateAttributeVal(); use setSoilMoisture() which calls SoilMeasurement::SetSoilMoistureMeasuredValue() under the CHIP stack lock. A repeated call with the same value is skipped only after that publish succeeds.

  • Unlike MatterHumiditySensor, there is no sub-percent precision.

Example

Soil Sensor

// Copyright 2026 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Matter Manager
#include <Arduino.h>
#include <Matter.h>

// Matter Soil Sensor Endpoint
MatterSoilSensor SoilSensor;

// Wi-Fi credentials for this sketch. Fill these in when the board cannot
// commission over BLE (Arduino prebuild on ESP32 / ESP32-S2): the sketch
// joins the AP itself. When Matter commissions over BLE (CHIPoBLE), the
// hub sends SSID and password — leave the placeholders; they are unused.
#define WIFI_SSID     "your-ssid"
#define WIFI_PASSWORD "your-password"

// set your board USER BUTTON pin here - decommissioning button
const uint8_t buttonPin = BOOT_PIN;  // Set your pin here. Using BOOT Button.
MatterButton button;

// Simulate a soil moisture sensor - add your preferred soil moisture sensor library code here
uint8_t getSimulatedSoilMoisture() {
  // Whole percent [0..100]. Sweeps 20%..60% up, then down, in 1% steps.
  static uint8_t simulatedSoilMoistureHWSensor = 20;
  static int8_t direction = 1;

  uint8_t reading = simulatedSoilMoistureHWSensor;
  int16_t next = (int16_t)simulatedSoilMoistureHWSensor + direction;
  if (next >= 60) {
    next = 60;
    direction = -1;
  } else if (next <= 20) {
    next = 20;
    direction = 1;
  }
  simulatedSoilMoistureHWSensor = (uint8_t)next;
  return reading;
}

void setup() {
  button.begin(buttonPin);

  Serial.begin(115200);

#if !CONFIG_ENABLE_CHIPOBLE
  matterConnectWiFi(WIFI_SSID, WIFI_PASSWORD);
#endif

  // Simulated Sensor - it shall initially print 20% and then rise to 60% and fall back to 20% in 1% steps
  SoilSensor.begin();

  matterSetExampleIdentity("Soil Sensor");
  Matter.begin();
  matterWaitUntilReady();

  SoilSensor.setSoilMoisture(getSimulatedSoilMoisture());
}

void loop() {
  matterRestartIfNoFabric();

  static uint32_t timeCounter = 0;

  if (!(timeCounter++ % 10)) {  // 500 ms x 10 = 5 s
    Serial.printf("Current Soil Moisture is %u%%\r\n", SoilSensor.getSoilMoisture());
    SoilSensor.setSoilMoisture(getSimulatedSoilMoisture());
  }

  matterButtonEvent_t ev;
  while ((ev = button.poll()) != MATTER_BUTTON_NONE) {
    if (ev == MATTER_BUTTON_LONG_HOLD) {
      Serial.println("Decommissioning Soil Sensor Matter Accessory. It shall be commissioned again.");
      Matter.decommission();
    }
  }

  delay(500);
}