libraries/Matter/examples/MatterOnOffPlugin/README.md
This example demonstrates how to create a Matter-compatible on/off plugin unit (power relay) device using an ESP32 SoC microcontroller.
The application showcases Matter commissioning, device control via smart home ecosystems, and state persistence for power control applications.
| SoC | Wi-Fi | Thread | BLE Commissioning | Relay/LED | Status |
|---|---|---|---|---|---|
| ESP32 | ✅ | ❌ | ❌ | Required | Fully supported |
| ESP32-S2 | ✅ | ❌ | ❌ | Required | Fully supported |
| ESP32-S3 | ✅ | ❌ | ✅ | Required | Fully supported |
| ESP32-C3 | ✅ | ❌ | ✅ | Required | Fully supported |
| ESP32-C5 | ❌ | ✅ | ✅ | Required | Supported (Thread only) |
| ESP32-C6 | ✅ | ❌ | ✅ | Required | Fully supported |
| ESP32-H2 | ❌ | ✅ | ✅ | Required | Supported (Thread only) |
Preferences libraryLED_BUILTIN if defined (for testing), otherwise pin 2. For production use, connect this to your relay control pin.BOOT_PIN by defaultMatterPreferencesWi-Fi (only for ESP32 and ESP32-S2)Before uploading the sketch, configure the following:
Wi-Fi credentials (if not using BLE commissioning - mandatory for ESP32 | ESP32-S2):
const char *ssid = "your-ssid"; // Change to your Wi-Fi SSID
const char *password = "your-password"; // Change to your Wi-Fi password
Power relay pin configuration (if not using built-in LED): For production use, change this to the GPIO pin connected to your relay control module:
const uint8_t onoffPin = 2; // Set your relay control pin here
Button pin configuration (optional):
By default, the BOOT button (GPIO 0) is used for factory reset. You can change this to a different pin if needed.
const uint8_t buttonPin = BOOT_PIN; // Set your button pin here
MatterOnOffPlugin.ino sketch in the Arduino IDE.Once the sketch is running, open the Serial Monitor at a baud rate of 115200. The Wi-Fi connection messages will be displayed only for ESP32 and ESP32-S2. Other targets will use Matter CHIPoBLE to automatically setup the IP Network. You should see output similar to the following, which provides the necessary information for commissioning:
Connecting to your-wifi-ssid
.......
Wi-Fi connected
IP address: 192.168.1.100
Matter Node is not commissioned yet.
Initiate the device discovery in your Matter environment.
Commission it to your Matter hub with the manual pairing code or QR code
Manual pairing code: 34970112332
QR code URL: https://project-chip.github.io/connectedhomeip/qrcode.html?data=MT%3A6FCJ142C00KA0648G00
Matter Node not commissioned yet. Waiting for commissioning.
Matter Node not commissioned yet. Waiting for commissioning.
...
Initial state: OFF
Matter Node is commissioned and connected to the network. Ready for use.
User Callback :: New Plugin State = ON
User Callback :: New Plugin State = OFF
The user button (BOOT button by default) provides factory reset functionality:
Note: This example does not include button toggle functionality. The plugin is controlled exclusively via Matter app commands.
The device saves the last known on/off state using the Preferences library. After a power cycle or restart:
For production use with a power relay module:
Connect the relay module to your ESP32:
onoffPin)Update the pin configuration in the sketch:
const uint8_t onoffPin = 2; // Your relay control pin
Test the relay by controlling it via Matter app - the relay should turn on/off accordingly
Use a Matter-compatible hub (like an Apple HomePod, Google Nest Hub, or Amazon Echo) to commission the device.
The MatterOnOffPlugin example consists of the following main components:
setup(): Initializes hardware (button, relay/LED pin), configures Wi-Fi (if needed), initializes Preferences library, sets up the Matter plugin endpoint with the last saved state (defaults to OFF if not previously saved), registers the callback function, and starts the Matter stack.
loop(): Checks the Matter commissioning state, handles button input for factory reset, and allows the Matter stack to process events.
Callbacks:
setPluginOnOff(): Controls the physical relay/LED based on the on/off state, saves the state to Preferences for persistence, and prints the state change to Serial Monitor.Preferences library is properly initialized and that flash memory is not corruptedArduino IDE Menu -> Tools -> Erase All Flash Before Sketch Upload: "Enabled" or directly with esptool.py --port <PORT> erase_flashThis example is licensed under the Apache License, Version 2.0.