|
| 1 | +// Copyright 2024 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 is the smallest code that will create a Matter Device which can be |
| 17 | + * commissioned and controlled from a Matter Environment APP. |
| 18 | + * It controls a GPIO that could be attached to a LED for visualization. |
| 19 | + * Additionally the ESP32 will send debug messages indicating the Matter activity. |
| 20 | + * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. |
| 21 | + * |
| 22 | + * This example is a simple Matter On/Off Light that can be controlled by a Matter Controller. |
| 23 | + * It demonstrates how to use On Identify callback when the Identify Cluster is called. |
| 24 | + * The Matter user APP can be used to request the device to identify itself by blinking the LED. |
| 25 | + */ |
| 26 | + |
| 27 | +// Matter Manager |
| 28 | +#include <Matter.h> |
| 29 | +#include <WiFi.h> |
| 30 | + |
| 31 | +// List of Matter Endpoints for this Node |
| 32 | +// Single On/Off Light Endpoint - at least one per node |
| 33 | +MatterOnOffLight OnOffLight; |
| 34 | + |
| 35 | +// Light GPIO that can be controlled by Matter APP |
| 36 | +#ifdef LED_BUILTIN |
| 37 | +const uint8_t ledPin = LED_BUILTIN; |
| 38 | +#else |
| 39 | +const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN |
| 40 | +#endif |
| 41 | + |
| 42 | +// set your board USER BUTTON pin here - decommissioning button |
| 43 | +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. |
| 44 | + |
| 45 | +// Button control - decommision the Matter Node |
| 46 | +uint32_t button_time_stamp = 0; // debouncing control |
| 47 | +bool button_state = false; // false = released | true = pressed |
| 48 | +const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission |
| 49 | + |
| 50 | +// Matter Protocol Endpoint (On/OFF Light) Callback |
| 51 | +bool matterCB(bool state) { |
| 52 | + digitalWrite(ledPin, state ? HIGH : LOW); |
| 53 | + // This callback must return the success state to Matter core |
| 54 | + return true; |
| 55 | +} |
| 56 | + |
| 57 | +// WiFi is manually set and started |
| 58 | +const char *ssid = "your-ssid"; // Change this to your WiFi SSID |
| 59 | +const char *password = "your-password"; // Change this to your WiFi password |
| 60 | + |
| 61 | +void setup() { |
| 62 | + // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node |
| 63 | + pinMode(buttonPin, INPUT_PULLUP); |
| 64 | + // Initialize the LED GPIO |
| 65 | + pinMode(ledPin, OUTPUT); |
| 66 | + |
| 67 | + // Manually connect to WiFi |
| 68 | + WiFi.begin(ssid, password); |
| 69 | + // Wait for connection |
| 70 | + while (WiFi.status() != WL_CONNECTED) { |
| 71 | + delay(500); |
| 72 | + } |
| 73 | + |
| 74 | + // Initialize at least one Matter EndPoint |
| 75 | + OnOffLight.begin(); |
| 76 | + |
| 77 | + // On Identify Callback - Blink the LED |
| 78 | + OnOffLight.onIdentify([](bool identifyIsActive, uint8_t counter) { |
| 79 | + log_i("Identify Cluster is %s, counter: %d", identifyIsActive ? "Active" : "Inactive", counter); |
| 80 | + if (identifyIsActive) { |
| 81 | + // Start Blinking the light |
| 82 | + OnOffLight.toggle(); |
| 83 | + } else { |
| 84 | + // Stop Blinking and restore the light to the its last state |
| 85 | + OnOffLight.updateAccessory(); |
| 86 | + } |
| 87 | + return true; |
| 88 | + }); |
| 89 | + |
| 90 | + // Associate a callback to the Matter Controller |
| 91 | + OnOffLight.onChange(matterCB); |
| 92 | + |
| 93 | + // Matter beginning - Last step, after all EndPoints are initialized |
| 94 | + Matter.begin(); |
| 95 | + |
| 96 | + if (!Matter.isDeviceCommissioned()) { |
| 97 | + log_i("Matter Node is not commissioned yet."); |
| 98 | + log_i("Initiate the device discovery in your Matter environment."); |
| 99 | + log_i("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 100 | + log_i("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); |
| 101 | + log_i("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void loop() { |
| 106 | + // Check if the button has been pressed |
| 107 | + if (digitalRead(buttonPin) == LOW && !button_state) { |
| 108 | + // deals with button debouncing |
| 109 | + button_time_stamp = millis(); // record the time while the button is pressed. |
| 110 | + button_state = true; // pressed. |
| 111 | + } |
| 112 | + |
| 113 | + if (digitalRead(buttonPin) == HIGH && button_state) { |
| 114 | + button_state = false; // released |
| 115 | + } |
| 116 | + |
| 117 | + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node |
| 118 | + uint32_t time_diff = millis() - button_time_stamp; |
| 119 | + if (button_state && time_diff > decommissioningTimeout) { |
| 120 | + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); |
| 121 | + Matter.decommission(); |
| 122 | + button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so |
| 123 | + } |
| 124 | + |
| 125 | + delay(500); |
| 126 | +} |
0 commit comments