|
| 1 | +<<<<<<< Updated upstream |
1 | 2 | // Copyright 2024 Espressif Systems (Shanghai) PTE LTD
|
2 | 3 | //
|
3 | 4 | // Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -162,3 +163,169 @@ void loop() {
|
162 | 163 |
|
163 | 164 | delay(500); // works as a debounce for the button and also for the LED blink
|
164 | 165 | }
|
| 166 | +======= |
| 167 | +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD |
| 168 | +// |
| 169 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 170 | +// you may not use this file except in compliance with the License. |
| 171 | +// You may obtain a copy of the License at |
| 172 | + |
| 173 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 174 | +// |
| 175 | +// Unless required by applicable law or agreed to in writing, software |
| 176 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 177 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 178 | +// See the License for the specific language governing permissions and |
| 179 | +// limitations under the License. |
| 180 | + |
| 181 | +/* |
| 182 | + * This example is the smallest code that will create a Matter Device which can be |
| 183 | + * commissioned and controlled from a Matter Environment APP. |
| 184 | + * It controls a GPIO that could be attached to a LED for visualization. |
| 185 | + * Additionally the ESP32 will send debug messages indicating the Matter activity. |
| 186 | + * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. |
| 187 | + * |
| 188 | + * This example is a simple Matter On/Off Light that can be controlled by a Matter Controller. |
| 189 | + * It demonstrates how to use On Identify callback when the Identify Cluster is called. |
| 190 | + * The Matter user APP can be used to request the device to identify itself by blinking the LED. |
| 191 | + */ |
| 192 | + |
| 193 | +// Matter Manager |
| 194 | +#include <Matter.h> |
| 195 | +#include <WiFi.h> |
| 196 | + |
| 197 | +// List of Matter Endpoints for this Node |
| 198 | +// Single On/Off Light Endpoint - at least one per node |
| 199 | +MatterOnOffLight OnOffLight; |
| 200 | + |
| 201 | +// WiFi is manually set and started |
| 202 | +const char *ssid = "your-ssid"; // Change this to your WiFi SSID |
| 203 | +const char *password = "your-password"; // Change this to your WiFi password |
| 204 | + |
| 205 | +// Light GPIO that can be controlled by Matter APP |
| 206 | +#ifdef LED_BUILTIN |
| 207 | +const uint8_t ledPin = LED_BUILTIN; |
| 208 | +#else |
| 209 | +const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN |
| 210 | +#endif |
| 211 | + |
| 212 | +// set your board USER BUTTON pin here - decommissioning button |
| 213 | +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. |
| 214 | + |
| 215 | +// Button control - decommision the Matter Node |
| 216 | +uint32_t button_time_stamp = 0; // debouncing control |
| 217 | +bool button_state = false; // false = released | true = pressed |
| 218 | +const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission |
| 219 | + |
| 220 | +// Identify Flag and blink time - Blink the LED |
| 221 | +const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed |
| 222 | +volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state |
| 223 | +bool identifyBlink = false; // Blink state when in Identify state |
| 224 | + |
| 225 | +// Matter Protocol Endpoint (On/OFF Light) Callback |
| 226 | +bool onOffLightCallback(bool state) { |
| 227 | + digitalWrite(ledPin, state ? HIGH : LOW); |
| 228 | + // This callback must return the success state to Matter core |
| 229 | + return true; |
| 230 | +} |
| 231 | + |
| 232 | +// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined |
| 233 | +bool onIdentifyLightCallback(bool identifyIsActive) { |
| 234 | + Serial.printf("Identify Cluster is %s\r\n", identifyIsActive ? "Active" : "Inactive"); |
| 235 | + if (identifyIsActive) { |
| 236 | + // Start Blinking the light in loop() |
| 237 | + identifyFlag = true; |
| 238 | + identifyBlink = !OnOffLight; // Start with the inverted light state |
| 239 | + } else { |
| 240 | + // Stop Blinking and restore the light to the its last state |
| 241 | + identifyFlag = false; |
| 242 | + // force returning to the original state by toggling the light twice |
| 243 | + OnOffLight.toggle(); |
| 244 | + OnOffLight.toggle(); |
| 245 | + } |
| 246 | + return true; |
| 247 | +} |
| 248 | + |
| 249 | +void setup() { |
| 250 | + // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node |
| 251 | + pinMode(buttonPin, INPUT_PULLUP); |
| 252 | + // Initialize the LED GPIO |
| 253 | + pinMode(ledPin, OUTPUT); |
| 254 | + |
| 255 | + Serial.begin(115200); |
| 256 | + |
| 257 | + // Manually connect to WiFi |
| 258 | + WiFi.begin(ssid, password); |
| 259 | + // Wait for connection |
| 260 | + while (WiFi.status() != WL_CONNECTED) { |
| 261 | + delay(500); |
| 262 | + Serial.print("."); |
| 263 | + } |
| 264 | + Serial.println(); |
| 265 | + |
| 266 | + // Initialize at least one Matter EndPoint |
| 267 | + OnOffLight.begin(); |
| 268 | + |
| 269 | + // On Identify Callback - Blink the LED |
| 270 | + OnOffLight.onIdentify(onIdentifyLightCallback); |
| 271 | + |
| 272 | + // Associate a callback to the Matter Controller |
| 273 | + OnOffLight.onChange(onOffLightCallback); |
| 274 | + |
| 275 | + // Matter beginning - Last step, after all EndPoints are initialized |
| 276 | + Matter.begin(); |
| 277 | + |
| 278 | + // Check Matter Accessory Commissioning state, which may change during execution of loop() |
| 279 | + if (!Matter.isDeviceCommissioned()) { |
| 280 | + Serial.println(""); |
| 281 | + Serial.println("Matter Node is not commissioned yet."); |
| 282 | + Serial.println("Initiate the device discovery in your Matter environment."); |
| 283 | + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 284 | + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); |
| 285 | + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 286 | + // waits for Matter Occupancy Sensor Commissioning. |
| 287 | + uint32_t timeCount = 0; |
| 288 | + while (!Matter.isDeviceCommissioned()) { |
| 289 | + delay(100); |
| 290 | + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec |
| 291 | + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); |
| 292 | + } |
| 293 | + } |
| 294 | + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); |
| 295 | + } |
| 296 | +} |
| 297 | + |
| 298 | +void loop() { |
| 299 | + // check if the Light is in identify state and blink it every 500ms (delay loop time) |
| 300 | + if (identifyFlag) { |
| 301 | +#ifdef LED_BUILTIN |
| 302 | + uint8_t brightness = 32 * identifyBlink; |
| 303 | + rgbLedWrite(identifyLedPin, brightness, 0, 0); |
| 304 | +#else |
| 305 | + digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW); |
| 306 | +#endif |
| 307 | + identifyBlink = !identifyBlink; |
| 308 | + } |
| 309 | + |
| 310 | + // Check if the button has been pressed |
| 311 | + if (digitalRead(buttonPin) == LOW && !button_state) { |
| 312 | + // deals with button debouncing |
| 313 | + button_time_stamp = millis(); // record the time while the button is pressed. |
| 314 | + button_state = true; // pressed. |
| 315 | + } |
| 316 | + |
| 317 | + if (digitalRead(buttonPin) == HIGH && button_state) { |
| 318 | + button_state = false; // released |
| 319 | + } |
| 320 | + |
| 321 | + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node |
| 322 | + uint32_t time_diff = millis() - button_time_stamp; |
| 323 | + if (button_state && time_diff > decommissioningTimeout) { |
| 324 | + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); |
| 325 | + Matter.decommission(); |
| 326 | + button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so |
| 327 | + } |
| 328 | + |
| 329 | + delay(500); // works as a debounce for the button and also for the LED blink |
| 330 | +} |
| 331 | +>>>>>>> Stashed changes |
0 commit comments