|
1 | | -<<<<<<< Updated upstream |
2 | | -// Copyright 2024 Espressif Systems (Shanghai) PTE LTD |
3 | | -// |
4 | | -// Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | -// you may not use this file except in compliance with the License. |
6 | | -// You may obtain a copy of the License at |
7 | | - |
8 | | -// http://www.apache.org/licenses/LICENSE-2.0 |
9 | | -// |
10 | | -// Unless required by applicable law or agreed to in writing, software |
11 | | -// distributed under the License is distributed on an "AS IS" BASIS, |
12 | | -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | -// See the License for the specific language governing permissions and |
14 | | -// limitations under the License. |
15 | | - |
16 | | -/* |
17 | | - * This example is the smallest code that will create a Matter Device which can be |
18 | | - * commissioned and controlled from a Matter Environment APP. |
19 | | - * It controls a GPIO that could be attached to a LED for visualization. |
20 | | - * Additionally the ESP32 will send debug messages indicating the Matter activity. |
21 | | - * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. |
22 | | - * |
23 | | - * This example is a simple Matter On/Off Light that can be controlled by a Matter Controller. |
24 | | - * It demonstrates how to use On Identify callback when the Identify Cluster is called. |
25 | | - * The Matter user APP can be used to request the device to identify itself by blinking the LED. |
26 | | - */ |
27 | | - |
28 | | -// Matter Manager |
29 | | -#include <Matter.h> |
30 | | -#include <WiFi.h> |
31 | | - |
32 | | -// List of Matter Endpoints for this Node |
33 | | -// Single On/Off Light Endpoint - at least one per node |
34 | | -MatterOnOffLight OnOffLight; |
35 | | - |
36 | | -// WiFi is manually set and started |
37 | | -const char *ssid = "your-ssid"; // Change this to your WiFi SSID |
38 | | -const char *password = "your-password"; // Change this to your WiFi password |
39 | | - |
40 | | -// Light GPIO that can be controlled by Matter APP |
41 | | -#ifdef LED_BUILTIN |
42 | | -const uint8_t ledPin = LED_BUILTIN; |
43 | | -#else |
44 | | -const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN |
45 | | -#endif |
46 | | - |
47 | | -// set your board USER BUTTON pin here - decommissioning button |
48 | | -const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. |
49 | | - |
50 | | -// Button control - decommision the Matter Node |
51 | | -uint32_t button_time_stamp = 0; // debouncing control |
52 | | -bool button_state = false; // false = released | true = pressed |
53 | | -const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission |
54 | | - |
55 | | -// Identify Flag and blink time - Blink the LED |
56 | | -const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed |
57 | | -volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state |
58 | | -bool identifyBlink = false; // Blink state when in Identify state |
59 | | - |
60 | | -// Matter Protocol Endpoint (On/OFF Light) Callback |
61 | | -bool onOffLightCallback(bool state) { |
62 | | - digitalWrite(ledPin, state ? HIGH : LOW); |
63 | | - // This callback must return the success state to Matter core |
64 | | - return true; |
65 | | -} |
66 | | - |
67 | | -// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined |
68 | | -bool onIdentifyLightCallback(bool identifyIsActive) { |
69 | | - Serial.printf("Identify Cluster is %s\r\n", identifyIsActive ? "Active" : "Inactive"); |
70 | | - if (identifyIsActive) { |
71 | | - // Start Blinking the light in loop() |
72 | | - identifyFlag = true; |
73 | | - identifyBlink = !OnOffLight; // Start with the inverted light state |
74 | | - } else { |
75 | | - // Stop Blinking and restore the light to the its last state |
76 | | - identifyFlag = false; |
77 | | - // force returning to the original state by toggling the light twice |
78 | | - OnOffLight.toggle(); |
79 | | - OnOffLight.toggle(); |
80 | | - } |
81 | | - return true; |
82 | | -} |
83 | | - |
84 | | -void setup() { |
85 | | - // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node |
86 | | - pinMode(buttonPin, INPUT_PULLUP); |
87 | | - // Initialize the LED GPIO |
88 | | - pinMode(ledPin, OUTPUT); |
89 | | - |
90 | | - Serial.begin(115200); |
91 | | - |
92 | | - // Manually connect to WiFi |
93 | | - WiFi.begin(ssid, password); |
94 | | - // Wait for connection |
95 | | - while (WiFi.status() != WL_CONNECTED) { |
96 | | - delay(500); |
97 | | - Serial.print("."); |
98 | | - } |
99 | | - Serial.println(); |
100 | | - |
101 | | - // Initialize at least one Matter EndPoint |
102 | | - OnOffLight.begin(); |
103 | | - |
104 | | - // On Identify Callback - Blink the LED |
105 | | - OnOffLight.onIdentify(onIdentifyLightCallback); |
106 | | - |
107 | | - // Associate a callback to the Matter Controller |
108 | | - OnOffLight.onChange(onOffLightCallback); |
109 | | - |
110 | | - // Matter beginning - Last step, after all EndPoints are initialized |
111 | | - Matter.begin(); |
112 | | - |
113 | | - // Check Matter Accessory Commissioning state, which may change during execution of loop() |
114 | | - if (!Matter.isDeviceCommissioned()) { |
115 | | - Serial.println(""); |
116 | | - Serial.println("Matter Node is not commissioned yet."); |
117 | | - Serial.println("Initiate the device discovery in your Matter environment."); |
118 | | - Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
119 | | - Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); |
120 | | - Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); |
121 | | - // waits for Matter Occupancy Sensor Commissioning. |
122 | | - uint32_t timeCount = 0; |
123 | | - while (!Matter.isDeviceCommissioned()) { |
124 | | - delay(100); |
125 | | - if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec |
126 | | - Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); |
127 | | - } |
128 | | - } |
129 | | - Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); |
130 | | - } |
131 | | -} |
132 | | - |
133 | | -void loop() { |
134 | | - // check if the Ligth is in identify state and blink it every 500ms (delay loop time) |
135 | | - if (identifyFlag) { |
136 | | -#ifdef LED_BUILTIN |
137 | | - uint8_t brightness = 32 * identifyBlink; |
138 | | - rgbLedWrite(identifyLedPin, brightness, 0, 0); |
139 | | -#else |
140 | | - digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); |
141 | | -#endif |
142 | | - identifyBlink = !identifyBlink; |
143 | | - } |
144 | | - |
145 | | - // Check if the button has been pressed |
146 | | - if (digitalRead(buttonPin) == LOW && !button_state) { |
147 | | - // deals with button debouncing |
148 | | - button_time_stamp = millis(); // record the time while the button is pressed. |
149 | | - button_state = true; // pressed. |
150 | | - } |
151 | | - |
152 | | - if (digitalRead(buttonPin) == HIGH && button_state) { |
153 | | - button_state = false; // released |
154 | | - } |
155 | | - |
156 | | - // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node |
157 | | - uint32_t time_diff = millis() - button_time_stamp; |
158 | | - if (button_state && time_diff > decommissioningTimeout) { |
159 | | - Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); |
160 | | - Matter.decommission(); |
161 | | - button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so |
162 | | - } |
163 | | - |
164 | | - delay(500); // works as a debounce for the button and also for the LED blink |
165 | | -} |
166 | | -======= |
167 | 1 | // Copyright 2024 Espressif Systems (Shanghai) PTE LTD |
168 | 2 | // |
169 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
@@ -328,4 +162,3 @@ void loop() { |
328 | 162 |
|
329 | 163 | delay(500); // works as a debounce for the button and also for the LED blink |
330 | 164 | } |
331 | | ->>>>>>> Stashed changes |
0 commit comments