|
| 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 | +// Matter Manager |
| 16 | +#include <Matter.h> |
| 17 | +#include <WiFi.h> |
| 18 | + |
| 19 | +// List of Matter Endpoints for this Node |
| 20 | +// Generic Switch Endpoint - works as a smart button with a single click |
| 21 | +MatterGenericSwitch SmartButton; |
| 22 | + |
| 23 | +// set your board USER BUTTON pin here |
| 24 | +const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9. |
| 25 | + |
| 26 | +// WiFi is manually set and started |
| 27 | +const char *ssid = "your-ssid"; // Change this to your WiFi SSID |
| 28 | +const char *password = "your-password"; // Change this to your WiFi password |
| 29 | + |
| 30 | +void setup() { |
| 31 | + // Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch |
| 32 | + pinMode(buttonPin, INPUT_PULLUP); |
| 33 | + |
| 34 | + Serial.begin(115200); |
| 35 | + while (!Serial) { |
| 36 | + delay(100); |
| 37 | + } |
| 38 | + |
| 39 | + // We start by connecting to a WiFi network |
| 40 | + Serial.print("Connecting to "); |
| 41 | + Serial.println(ssid); |
| 42 | + // enable IPv6 |
| 43 | + WiFi.enableIPv6(true); |
| 44 | + // Manually connect to WiFi |
| 45 | + WiFi.begin(ssid, password); |
| 46 | + // Wait for connection |
| 47 | + while (WiFi.status() != WL_CONNECTED) { |
| 48 | + delay(500); |
| 49 | + Serial.print("."); |
| 50 | + } |
| 51 | + Serial.println("\r\nWiFi connected"); |
| 52 | + Serial.println("IP address: "); |
| 53 | + Serial.println(WiFi.localIP()); |
| 54 | + delay(500); |
| 55 | + |
| 56 | + // Initialize the Matter EndPoint |
| 57 | + SmartButton.begin(); |
| 58 | + |
| 59 | + // Matter beginning - Last step, after all EndPoints are initialized |
| 60 | + Matter.begin(); |
| 61 | + // This may be a restart of a already commissioned Matter accessory |
| 62 | + if (Matter.isDeviceCommissioned()) { |
| 63 | + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); |
| 64 | + } |
| 65 | +} |
| 66 | +// Button control |
| 67 | +uint32_t button_time_stamp = 0; // debouncing control |
| 68 | +bool button_state = false; // false = released | true = pressed |
| 69 | +const uint32_t debouceTime = 250; // button debouncing time (ms) |
| 70 | +const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the Matter Fabric |
| 71 | + |
| 72 | +void loop() { |
| 73 | + // Check Matter Accessory Commissioning state, which may change during execution of loop() |
| 74 | + if (!Matter.isDeviceCommissioned()) { |
| 75 | + Serial.println(""); |
| 76 | + Serial.println("Matter Node is not commissioned yet."); |
| 77 | + Serial.println("Initiate the device discovery in your Matter environment."); |
| 78 | + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 79 | + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); |
| 80 | + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 81 | + // waits for Matter Generic Switch Commissioning. |
| 82 | + uint32_t timeCount = 0; |
| 83 | + while (!Matter.isDeviceCommissioned()) { |
| 84 | + delay(100); |
| 85 | + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec |
| 86 | + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); |
| 87 | + } |
| 88 | + } |
| 89 | + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); |
| 90 | + } |
| 91 | + |
| 92 | + // A builtin button is used to trigger a command to the Matter Controller |
| 93 | + // Check if the button has been pressed |
| 94 | + if (digitalRead(buttonPin) == LOW && !button_state) { |
| 95 | + // deals with button debouncing |
| 96 | + button_time_stamp = millis(); // record the time while the button is pressed. |
| 97 | + button_state = true; // pressed. |
| 98 | + } |
| 99 | + |
| 100 | + // Onboard User Button is used as a smart button or to decommission it |
| 101 | + uint32_t time_diff = millis() - button_time_stamp; |
| 102 | + if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) { |
| 103 | + button_state = false; // released |
| 104 | + // builtin button is released - send a click event to the Matter Controller |
| 105 | + Serial.println("User button released. Sending Click to the Matter Controller!"); |
| 106 | + // Matter Controller will receive an event and, if programmed, it will trigger an action |
| 107 | + SmartButton.click(); |
| 108 | + |
| 109 | + // Factory reset is triggered if the button is pressed longer than 10 seconds |
| 110 | + if (time_diff > decommissioningTimeout) { |
| 111 | + Serial.println("Decommissioning the Generic Switch Matter Accessory. It shall be commissioned again."); |
| 112 | + Matter.decommission(); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments