|
| 1 | +// Copyright 2025 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/* |
| 16 | + This example creates 6 on-off light endpoints that share the same onChangeOnOff() callback code. |
| 17 | + It uses Lambda Function with an extra Lambda Capture information that links the Endpoint to its individual information. |
| 18 | + After the Matter example is commissioned, the expected Serial output shall be similar to this: |
| 19 | +
|
| 20 | +Matter App Control: 'Room 1' (OnOffLight[0], Endpoint 1, GPIO 2) changed to: OFF |
| 21 | +Matter App Control: 'Room 1' (OnOffLight[0], Endpoint 1, GPIO 2) changed to: ON |
| 22 | +Matter App Control: 'Room 5' (OnOffLight[4], Endpoint 5, GPIO 10) changed to: ON |
| 23 | +Matter App Control: 'Room 2' (OnOffLight[1], Endpoint 2, GPIO 4) changed to: ON |
| 24 | +Matter App Control: 'Room 4' (OnOffLight[3], Endpoint 4, GPIO 8) changed to: ON |
| 25 | +Matter App Control: 'Room 6' (OnOffLight[5], Endpoint 6, GPIO 12) changed to: ON |
| 26 | +Matter App Control: 'Room 3' (OnOffLight[2], Endpoint 3, GPIO 6) changed to: ON |
| 27 | +Matter App Control: 'Room 5' (OnOffLight[4], Endpoint 5, GPIO 10) changed to: OFF |
| 28 | +*/ |
| 29 | + |
| 30 | +// Matter Manager |
| 31 | +#include <Matter.h> |
| 32 | +#include <WiFi.h> |
| 33 | + |
| 34 | +// WiFi is manually set and started |
| 35 | +const char *ssid = "your-ssid"; // Change this to your WiFi SSID |
| 36 | +const char *password = "your-password"; // Change this to your WiFi password |
| 37 | + |
| 38 | +//number of On-Off Lights: |
| 39 | +const uint8_t MAX_LIGHT_NUMBER = 6; |
| 40 | + |
| 41 | +// array of OnOffLight endpoints |
| 42 | +MatterOnOffLight OnOffLight[MAX_LIGHT_NUMBER]; |
| 43 | + |
| 44 | +// all pins, one for each on-off light |
| 45 | +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 |
| 46 | + |
| 47 | +// friendly OnOffLights names used for printing a message in the callback |
| 48 | +const char *lightName[MAX_LIGHT_NUMBER] = { |
| 49 | + "Room 1", "Room 2", "Room 3", "Room 4", "Room 5", "Room 6", |
| 50 | +}; |
| 51 | + |
| 52 | +// simple setup() function |
| 53 | +void setup() { |
| 54 | + Serial.begin(115200); // callback will just print a message in the console |
| 55 | + |
| 56 | + // We start by connecting to a WiFi network |
| 57 | + Serial.print("Connecting to "); |
| 58 | + Serial.println(ssid); |
| 59 | + // Manually connect to WiFi |
| 60 | + WiFi.begin(ssid, password); |
| 61 | + // Wait for connection |
| 62 | + while (WiFi.status() != WL_CONNECTED) { |
| 63 | + delay(500); |
| 64 | + Serial.print("."); |
| 65 | + } |
| 66 | + Serial.println("\r\nWiFi connected"); |
| 67 | + Serial.println("IP address: "); |
| 68 | + Serial.println(WiFi.localIP()); |
| 69 | + delay(500); |
| 70 | + |
| 71 | + // setup all the OnOff Light endpoint and their lambda callback functions |
| 72 | + for (uint8_t i = 0; i < MAX_LIGHT_NUMBER; i++) { |
| 73 | + pinMode(lightPins[i], OUTPUT); // set the GPIO function |
| 74 | + OnOffLight[i].begin(false); // off |
| 75 | + |
| 76 | + // inline lambda function using capture array index -> it will just print a message in the console |
| 77 | + OnOffLight[i].onChangeOnOff([i](bool state) -> bool { |
| 78 | + // Display message with the specific light name and details |
| 79 | + Serial.printf( |
| 80 | + "Matter App Control: '%s' (OnOffLight[%d], Endpoint %d, GPIO %d) changed to: %s\r\n", lightName[i], i, OnOffLight[i].getEndPointId(), lightPins[i], |
| 81 | + state ? "ON" : "OFF" |
| 82 | + ); |
| 83 | + |
| 84 | + return true; |
| 85 | + }); |
| 86 | + } |
| 87 | + // last step, starting Matter Stack |
| 88 | + Matter.begin(); |
| 89 | +} |
| 90 | + |
| 91 | +void loop() { |
| 92 | + // Check Matter Plugin Commissioning state, which may change during execution of loop() |
| 93 | + if (!Matter.isDeviceCommissioned()) { |
| 94 | + Serial.println(""); |
| 95 | + Serial.println("Matter Node is not commissioned yet."); |
| 96 | + Serial.println("Initiate the device discovery in your Matter environment."); |
| 97 | + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 98 | + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); |
| 99 | + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 100 | + // waits for Matter Plugin Commissioning. |
| 101 | + uint32_t timeCount = 0; |
| 102 | + while (!Matter.isDeviceCommissioned()) { |
| 103 | + delay(100); |
| 104 | + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec |
| 105 | + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); |
| 106 | + } |
| 107 | + } |
| 108 | + Serial.println("Matter Node is commissioned and connected to the WiFi network. Ready for use."); |
| 109 | + } |
| 110 | + |
| 111 | + delay(500); |
| 112 | +} |
0 commit comments