|
| 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 | + |
| 23 | +// Matter Manager |
| 24 | +#include <Matter.h> |
| 25 | +#include <WiFi.h> |
| 26 | + |
| 27 | +// List of Matter Endpoints for this Node |
| 28 | +// Single On/Off Light Endpoint - at least one per node |
| 29 | +MatterOnOffLight OnOffLight; |
| 30 | + |
| 31 | +// Light GPIO that can be controlled by Matter APP |
| 32 | +#ifdef LED_BUILTIN |
| 33 | +const uint8_t ledPin = LED_BUILTIN; |
| 34 | +#else |
| 35 | +const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN |
| 36 | +#endif |
| 37 | + |
| 38 | +// Matter Protocol Endpoint (On/OFF Light) Callback |
| 39 | +bool matterCB(bool state) { |
| 40 | + digitalWrite(ledPin, state ? HIGH : LOW); |
| 41 | + // This callback must return the success state to Matter core |
| 42 | + return true; |
| 43 | +} |
| 44 | + |
| 45 | +// WiFi is manually set and started |
| 46 | +const char *ssid = "your-ssid"; // Change this to your WiFi SSID |
| 47 | +const char *password = "your-password"; // Change this to your WiFi password |
| 48 | + |
| 49 | +void setup() { |
| 50 | + // Initialize the LED GPIO |
| 51 | + pinMode(ledPin, OUTPUT); |
| 52 | + |
| 53 | + WiFi.enableIPv6(true); |
| 54 | + // Manually connect to WiFi |
| 55 | + WiFi.begin(ssid, password); |
| 56 | + // Wait for connection |
| 57 | + while (WiFi.status() != WL_CONNECTED) { |
| 58 | + delay(500); |
| 59 | + } |
| 60 | + |
| 61 | + // Initialize at least one Matter EndPoint |
| 62 | + OnOffLight.begin(); |
| 63 | + |
| 64 | + // Associate a callback to the Matter Controller |
| 65 | + OnOffLight.onChange(matterCB); |
| 66 | + |
| 67 | + // Matter beginning - Last step, after all EndPoints are initialized |
| 68 | + Matter.begin(); |
| 69 | + |
| 70 | + if (!Matter.isDeviceCommissioned()) { |
| 71 | + log_i("Matter Node is not commissioned yet."); |
| 72 | + log_i("Initiate the device discovery in your Matter environment."); |
| 73 | + log_i("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 74 | + log_i("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); |
| 75 | + log_i("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void loop() { |
| 80 | + delay(500); |
| 81 | +} |
0 commit comments