3.3.7
This commit is contained in:
+126
@@ -0,0 +1,126 @@
|
||||
// Copyright 2025 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.
|
||||
|
||||
/*
|
||||
This example creates 6 on-off light endpoints that share the same onChangeOnOff() callback code.
|
||||
It uses Lambda Function with an extra Lambda Capture information that links the Endpoint to its individual information.
|
||||
After the Matter example is commissioned, the expected Serial output shall be similar to this:
|
||||
|
||||
Matter App Control: 'Room 1' (OnOffLight[0], Endpoint 1, GPIO 2) changed to: OFF
|
||||
Matter App Control: 'Room 1' (OnOffLight[0], Endpoint 1, GPIO 2) changed to: ON
|
||||
Matter App Control: 'Room 5' (OnOffLight[4], Endpoint 5, GPIO 10) changed to: ON
|
||||
Matter App Control: 'Room 2' (OnOffLight[1], Endpoint 2, GPIO 4) changed to: ON
|
||||
Matter App Control: 'Room 4' (OnOffLight[3], Endpoint 4, GPIO 8) changed to: ON
|
||||
Matter App Control: 'Room 6' (OnOffLight[5], Endpoint 6, GPIO 12) changed to: ON
|
||||
Matter App Control: 'Room 3' (OnOffLight[2], Endpoint 3, GPIO 6) changed to: ON
|
||||
Matter App Control: 'Room 5' (OnOffLight[4], Endpoint 5, GPIO 10) changed to: OFF
|
||||
*/
|
||||
|
||||
// Matter Manager
|
||||
#include <Matter.h>
|
||||
#include <Preferences.h>
|
||||
#if !CONFIG_ENABLE_CHIPOBLE
|
||||
// if the device can be commissioned using BLE, WiFi is not used - save flash space
|
||||
#include <WiFi.h>
|
||||
// CONFIG_ENABLE_CHIPOBLE is enabled when BLE is used to commission the Matter Network
|
||||
// WiFi is manually set and started
|
||||
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
|
||||
const char *password = "your-password"; // Change this to your WiFi password
|
||||
#endif
|
||||
|
||||
//number of On-Off Lights:
|
||||
const uint8_t MAX_LIGHT_NUMBER = 6;
|
||||
|
||||
// array of OnOffLight endpoints
|
||||
MatterOnOffLight OnOffLight[MAX_LIGHT_NUMBER];
|
||||
|
||||
// all pins, one for each on-off light
|
||||
uint8_t lightPins[MAX_LIGHT_NUMBER] = {2, 4, 6, 8, 10, 12}; // must replace it by the real pin for the target SoC and application
|
||||
|
||||
// friendly OnOffLights names used for printing a message in the callback
|
||||
const char *lightName[MAX_LIGHT_NUMBER] = {
|
||||
"Room 1", "Room 2", "Room 3", "Room 4", "Room 5", "Room 6",
|
||||
};
|
||||
|
||||
// simple setup() function
|
||||
void setup() {
|
||||
Serial.begin(115200); // callback will just print a message in the console
|
||||
|
||||
// CONFIG_ENABLE_CHIPOBLE is enabled when BLE is used to commission the Matter Network
|
||||
#if !CONFIG_ENABLE_CHIPOBLE
|
||||
// We start by connecting to a WiFi network
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
// Manually connect to WiFi
|
||||
WiFi.begin(ssid, password);
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("\r\nWiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
delay(500);
|
||||
#endif
|
||||
|
||||
// setup all the OnOff Light endpoint and their lambda callback functions
|
||||
for (uint8_t i = 0; i < MAX_LIGHT_NUMBER; i++) {
|
||||
pinMode(lightPins[i], OUTPUT); // set the GPIO function
|
||||
OnOffLight[i].begin(false); // off
|
||||
|
||||
// inline lambda function using capture array index -> it will just print a message in the console
|
||||
OnOffLight[i].onChangeOnOff([i](bool state) -> bool {
|
||||
// Display message with the specific light name and details
|
||||
Serial.printf(
|
||||
"Matter App Control: '%s' (OnOffLight[%d], Endpoint %d, GPIO %d) changed to: %s\r\n", lightName[i], i, OnOffLight[i].getEndPointId(), lightPins[i],
|
||||
state ? "ON" : "OFF"
|
||||
);
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
// last step, starting Matter Stack
|
||||
Matter.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Check Matter Plugin Commissioning state, which may change during execution of loop()
|
||||
if (!Matter.isDeviceCommissioned()) {
|
||||
Serial.println("");
|
||||
Serial.println("Matter Node is not commissioned yet.");
|
||||
Serial.println("Initiate the device discovery in your Matter environment.");
|
||||
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
|
||||
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
|
||||
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
|
||||
// waits for Matter Plugin Commissioning.
|
||||
uint32_t timeCount = 0;
|
||||
while (!Matter.isDeviceCommissioned()) {
|
||||
delay(100);
|
||||
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
|
||||
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (Matter.isDeviceConnected()) {
|
||||
Serial.println("Matter Node is commissioned and connected to the network. Ready for use.");
|
||||
} else {
|
||||
Serial.println("Matter Node is commissioned. Waiting for the network connection.");
|
||||
}
|
||||
// wait 3 seconds for the network connection
|
||||
delay(3000);
|
||||
}
|
||||
|
||||
delay(100);
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
# Matter Lambda Single Callback Many Endpoints Example
|
||||
|
||||
This example demonstrates how to create multiple Matter endpoints in a single node using a shared lambda function callback with capture in an ESP32 SoC microcontroller.\
|
||||
The application showcases Matter commissioning, multiple endpoint management, and efficient callback handling using C++ lambda functions with capture variables.
|
||||
|
||||
## Supported Targets
|
||||
|
||||
| SoC | Wi-Fi | Thread | BLE Commissioning | GPIO Pins | 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) |
|
||||
|
||||
### Note on Commissioning:
|
||||
|
||||
- **ESP32 & ESP32-S2** do not support commissioning over Bluetooth LE. For these chips, you must provide Wi-Fi credentials directly in the sketch code so they can connect to your network manually.
|
||||
- **ESP32-C6** Although it has Thread support, the ESP32 Arduino Matter Library has been pre compiled using Wi-Fi only. In order to configure it for Thread-only operation it is necessary to build the project using Arduino as an IDF Component and to disable the Matter Wi-Fi station feature.
|
||||
- **ESP32-C5** Although it has Wi-Fi 2.4 GHz and 5 GHz support, the ESP32 Arduino Matter Library has been pre compiled using Thread only. In order to configure it for Wi-Fi operation it is necessary to build the project using Arduino as an ESP-IDF component and disable Thread network, keeping only Wi-Fi station.
|
||||
|
||||
## Features
|
||||
|
||||
- Matter protocol implementation with multiple endpoints in a single node
|
||||
- Six on/off light endpoints sharing a single callback function
|
||||
- Lambda function with capture variable for efficient endpoint identification
|
||||
- Support for both Wi-Fi and Thread(*) connectivity
|
||||
- Each endpoint has a unique GPIO pin and friendly name
|
||||
- Matter commissioning via QR code or manual pairing code
|
||||
- Integration with Apple HomeKit, Amazon Alexa, and Google Home
|
||||
(*) It is necessary to compile the project using Arduino as IDF Component.
|
||||
|
||||
## Hardware Requirements
|
||||
|
||||
- ESP32 compatible development board (see supported targets table)
|
||||
- Optional: Six LEDs connected to GPIO pins (2, 4, 6, 8, 10, 12) for visual feedback
|
||||
|
||||
## Pin Configuration
|
||||
|
||||
By default, the example uses six GPIO pins for the on/off lights:
|
||||
- **Light 1 (Room 1)**: GPIO 2
|
||||
- **Light 2 (Room 2)**: GPIO 4
|
||||
- **Light 3 (Room 3)**: GPIO 6
|
||||
- **Light 4 (Room 4)**: GPIO 8
|
||||
- **Light 5 (Room 5)**: GPIO 10
|
||||
- **Light 6 (Room 6)**: GPIO 12
|
||||
|
||||
You can modify the `lightPins` array to match your hardware configuration:
|
||||
|
||||
```cpp
|
||||
uint8_t lightPins[MAX_LIGHT_NUMBER] = {2, 4, 6, 8, 10, 12};
|
||||
```
|
||||
|
||||
## Software Setup
|
||||
|
||||
### Prerequisites
|
||||
|
||||
1. Install the Arduino IDE (2.0 or newer recommended)
|
||||
2. Install ESP32 Arduino Core with Matter support
|
||||
3. ESP32 Arduino libraries:
|
||||
- `Matter`
|
||||
- `Preferences`
|
||||
- `Wi-Fi` (only for ESP32 and ESP32-S2)
|
||||
|
||||
### Configuration
|
||||
|
||||
Before uploading the sketch, configure the following:
|
||||
|
||||
1. **Wi-Fi credentials** (if not using BLE commissioning - mandatory for ESP32 | ESP32-S2):
|
||||
```cpp
|
||||
const char *ssid = "your-ssid"; // Change to your Wi-Fi SSID
|
||||
const char *password = "your-password"; // Change to your Wi-Fi password
|
||||
```
|
||||
|
||||
2. **GPIO pin configuration** (optional):
|
||||
Modify the `lightPins` array to match your hardware:
|
||||
```cpp
|
||||
uint8_t lightPins[MAX_LIGHT_NUMBER] = {2, 4, 6, 8, 10, 12};
|
||||
```
|
||||
|
||||
3. **Light names** (optional):
|
||||
Modify the `lightName` array to customize the friendly names:
|
||||
```cpp
|
||||
const char *lightName[MAX_LIGHT_NUMBER] = {
|
||||
"Room 1", "Room 2", "Room 3", "Room 4", "Room 5", "Room 6",
|
||||
};
|
||||
```
|
||||
|
||||
4. **Number of endpoints** (optional):
|
||||
Change `MAX_LIGHT_NUMBER` to create more or fewer endpoints:
|
||||
```cpp
|
||||
const uint8_t MAX_LIGHT_NUMBER = 6;
|
||||
```
|
||||
|
||||
## Building and Flashing
|
||||
|
||||
1. Open the `MatterLambdaSingleCallbackManyEPs.ino` sketch in the Arduino IDE.
|
||||
2. Select your ESP32 board from the **Tools > Board** menu.
|
||||
<!-- vale off -->
|
||||
3. Select **"Huge APP (3MB No OTA/1MB SPIFFS)"** from **Tools > Partition Scheme** menu.
|
||||
<!-- vale on -->
|
||||
4. Enable **"Erase All Flash Before Sketch Upload"** option from **Tools** menu.
|
||||
5. Connect your ESP32 board to your computer via USB.
|
||||
6. Click the **Upload** button to compile and flash the sketch.
|
||||
|
||||
## Expected Output
|
||||
|
||||
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.
|
||||
...
|
||||
Matter Node is commissioned and connected to the network. Ready for use.
|
||||
Matter App Control: 'Room 1' (OnOffLight[0], Endpoint 1, GPIO 2) changed to: OFF
|
||||
Matter App Control: 'Room 1' (OnOffLight[0], Endpoint 1, GPIO 2) changed to: ON
|
||||
Matter App Control: 'Room 5' (OnOffLight[4], Endpoint 5, GPIO 10) changed to: ON
|
||||
Matter App Control: 'Room 2' (OnOffLight[1], Endpoint 2, GPIO 4) changed to: ON
|
||||
```
|
||||
|
||||
## Using the Device
|
||||
|
||||
### Lambda Function with Capture
|
||||
|
||||
This example demonstrates how to use C++ lambda functions with capture variables to efficiently handle multiple endpoints with a single callback function. The lambda capture `[i]` allows the callback to identify which endpoint triggered the event:
|
||||
|
||||
```cpp
|
||||
OnOffLight[i].onChangeOnOff([i](bool state) -> bool {
|
||||
Serial.printf(
|
||||
"Matter App Control: '%s' (OnOffLight[%d], Endpoint %d, GPIO %d) changed to: %s\r\n",
|
||||
lightName[i], i, OnOffLight[i].getEndPointId(), lightPins[i], state ? "ON" : "OFF"
|
||||
);
|
||||
return true;
|
||||
});
|
||||
```
|
||||
|
||||
### Smart Home Integration
|
||||
|
||||
Use a Matter-compatible hub (like an Apple HomePod, Google Nest Hub, or Amazon Echo) to commission the device. After commissioning, you will see six separate on/off light devices in your smart home app, each representing a different room.
|
||||
|
||||
#### Apple Home
|
||||
|
||||
1. Open the Home app on your iOS device
|
||||
2. Tap the "+" button > Add Accessory
|
||||
3. Scan the QR code displayed in the Serial Monitor, or
|
||||
4. Tap "I Don't Have a Code or Cannot Scan" and enter the manual pairing code
|
||||
5. Follow the prompts to complete setup
|
||||
6. The device will appear as six separate on/off lights in your Home app (Room 1 through Room 6)
|
||||
|
||||
#### Amazon Alexa
|
||||
|
||||
1. Open the Alexa app
|
||||
2. Tap More > Add Device > Matter
|
||||
3. Select "Scan QR code" or "Enter code manually"
|
||||
4. Complete the setup process
|
||||
5. The six lights will appear in your Alexa app
|
||||
6. You can control each room independently
|
||||
|
||||
#### Google Home
|
||||
|
||||
1. Open the Google Home app
|
||||
2. Tap "+" > Set up device > New device
|
||||
3. Choose "Matter device"
|
||||
4. Scan the QR code or enter the manual pairing code
|
||||
5. Follow the prompts to complete setup
|
||||
6. The six lights will appear in your Google Home app
|
||||
|
||||
## Code Structure
|
||||
|
||||
The MatterLambdaSingleCallbackManyEPs example consists of the following main components:
|
||||
|
||||
1. **Arrays and Configuration**:
|
||||
- `OnOffLight[MAX_LIGHT_NUMBER]`: Array of Matter on/off light endpoints
|
||||
- `lightPins[]`: Array of GPIO pins for each light
|
||||
- `lightName[]`: Array of friendly names for each light
|
||||
|
||||
2. **`setup()`**: Configures Wi-Fi (if needed), initializes all GPIO pins, initializes all Matter endpoints, registers lambda callbacks with capture variables for each endpoint, and starts the Matter stack.
|
||||
|
||||
3. **`loop()`**: Checks the Matter commissioning state and connection status, displays appropriate messages, and allows the Matter stack to process events.
|
||||
|
||||
4. **Lambda Callback**:
|
||||
- Uses capture variable `[i]` to identify which endpoint triggered the callback
|
||||
- Displays detailed information including friendly name, array index, endpoint ID, GPIO pin, and state
|
||||
- Demonstrates efficient callback handling for multiple endpoints
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- **Device not visible during commissioning**: Ensure Wi-Fi or Thread connectivity is properly configured
|
||||
- **Only some endpoints appear**: Some smart home platforms may group or display endpoints differently. Check your app's device list
|
||||
- **GPIO pins not responding**: Verify pin configurations match your hardware. Ensure pins are not used by other peripherals
|
||||
- **Failed to commission**: Try erasing the SoC Flash Memory by using `Arduino IDE Menu` -> `Tools` -> `Erase All Flash Before Sketch Upload: "Enabled"` or directly with `esptool.py --port <PORT> erase_flash`
|
||||
- **No serial output**: Check baudrate (115200) and USB connection
|
||||
- **Compilation errors with lambda functions**: Ensure you're using a C++11 compatible compiler (ESP32 Arduino Core 2.0+ supports this)
|
||||
|
||||
## Related Documentation
|
||||
|
||||
- [Matter Overview](https://docs.espressif.com/projects/arduino-esp32/en/latest/matter/matter.html)
|
||||
- [Matter Endpoint Base Class](https://docs.espressif.com/projects/arduino-esp32/en/latest/matter/matter_ep.html)
|
||||
|
||||
## License
|
||||
|
||||
This example is licensed under the Apache License, Version 2.0.
|
||||
@@ -0,0 +1,4 @@
|
||||
fqbn_append: PartitionScheme=huge_app
|
||||
|
||||
requires:
|
||||
- CONFIG_ESP_MATTER_ENABLE_DATA_MODEL=y
|
||||
Reference in New Issue
Block a user