|
| 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 | +#include <Preferences.h> |
| 19 | + |
| 20 | +// List of Matter Endpoints for this Node |
| 21 | +// On/Off Plugin Endpoint |
| 22 | +MatterOnOffPlugin OnOffPlugin; |
| 23 | + |
| 24 | +// it will keep last OnOff state stored, using Preferences |
| 25 | +Preferences matterPref; |
| 26 | +const char *onOffPrefKey = "OnOff"; |
| 27 | + |
| 28 | +// set your board Power Relay pin here - this example uses the built-in LED for easy visualization |
| 29 | +#ifdef LED_BUILTIN |
| 30 | +const uint8_t onoffPin = LED_BUILTIN; |
| 31 | +#else |
| 32 | +const uint8_t onoffPin = 2; // Set your pin here - usually a power relay |
| 33 | +#warning "Do not forget to set the Power Relay pin" |
| 34 | +#endif |
| 35 | + |
| 36 | +// board USER BUTTON pin necessary for Decommissioning |
| 37 | +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. |
| 38 | + |
| 39 | +// Button control |
| 40 | +uint32_t button_time_stamp = 0; // debouncing control |
| 41 | +bool button_state = false; // false = released | true = pressed |
| 42 | +const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission |
| 43 | + |
| 44 | +// WiFi is manually set and started |
| 45 | +const char *ssid = "your-ssid"; // Change this to your WiFi SSID |
| 46 | +const char *password = "your-password"; // Change this to your WiFi password |
| 47 | + |
| 48 | +// Matter Protocol Endpoint Callback |
| 49 | +bool setPluginOnOff(bool state) { |
| 50 | + Serial.printf("User Callback :: New Plugin State = %s\r\n", state ? "ON" : "OFF"); |
| 51 | + if (state) { |
| 52 | + digitalWrite(onoffPin, HIGH); |
| 53 | + } else { |
| 54 | + digitalWrite(onoffPin, LOW); |
| 55 | + } |
| 56 | + // store last OnOff state for when the Plugin is restarted / power goes off |
| 57 | + matterPref.putBool(onOffPrefKey, state); |
| 58 | + // This callback must return the success state to Matter core |
| 59 | + return true; |
| 60 | +} |
| 61 | + |
| 62 | +void setup() { |
| 63 | + // Initialize the USER BUTTON |
| 64 | + pinMode(buttonPin, INPUT_PULLUP); |
| 65 | + // Initialize the Power Relay (plugin) GPIO |
| 66 | + pinMode(onoffPin, OUTPUT); |
| 67 | + |
| 68 | + Serial.begin(115200); |
| 69 | + |
| 70 | + // We start by connecting to a WiFi network |
| 71 | + Serial.print("Connecting to "); |
| 72 | + Serial.println(ssid); |
| 73 | + WiFi.begin(ssid, password); |
| 74 | + // Wait for connection |
| 75 | + while (WiFi.status() != WL_CONNECTED) { |
| 76 | + delay(500); |
| 77 | + Serial.print("."); |
| 78 | + } |
| 79 | + Serial.println("\r\nWiFi connected"); |
| 80 | + Serial.println("IP address: "); |
| 81 | + Serial.println(WiFi.localIP()); |
| 82 | + delay(500); |
| 83 | + |
| 84 | + // Initialize Matter EndPoint |
| 85 | + matterPref.begin("MatterPrefs", false); |
| 86 | + bool lastOnOffState = matterPref.getBool(onOffPrefKey, false); |
| 87 | + OnOffPlugin.begin(lastOnOffState); |
| 88 | + OnOffPlugin.onChange(setPluginOnOff); |
| 89 | + |
| 90 | + // Matter beginning - Last step, after all EndPoints are initialized |
| 91 | + Matter.begin(); |
| 92 | + // This may be a restart of a already commissioned Matter accessory |
| 93 | + if (Matter.isDeviceCommissioned()) { |
| 94 | + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); |
| 95 | + Serial.printf("Initial state: %s\r\n", OnOffPlugin.getOnOff() ? "ON" : "OFF"); |
| 96 | + OnOffPlugin.updateAccessory(); // configure the Plugin based on initial state |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void loop() { |
| 101 | + // Check Matter Plugin Commissioning state, which may change during execution of loop() |
| 102 | + if (!Matter.isDeviceCommissioned()) { |
| 103 | + Serial.println(""); |
| 104 | + Serial.println("Matter Node is not commissioned yet."); |
| 105 | + Serial.println("Initiate the device discovery in your Matter environment."); |
| 106 | + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 107 | + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); |
| 108 | + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 109 | + // waits for Matter Plugin Commissioning. |
| 110 | + uint32_t timeCount = 0; |
| 111 | + while (!Matter.isDeviceCommissioned()) { |
| 112 | + delay(100); |
| 113 | + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec |
| 114 | + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); |
| 115 | + } |
| 116 | + } |
| 117 | + Serial.printf("Initial state: %s\r\n", OnOffPlugin.getOnOff() ? "ON" : "OFF"); |
| 118 | + OnOffPlugin.updateAccessory(); // configure the Plugin based on initial state |
| 119 | + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); |
| 120 | + } |
| 121 | + |
| 122 | + // Check if the button has been pressed |
| 123 | + if (digitalRead(buttonPin) == LOW && !button_state) { |
| 124 | + // deals with button debouncing |
| 125 | + button_time_stamp = millis(); // record the time while the button is pressed. |
| 126 | + button_state = true; // pressed. |
| 127 | + } |
| 128 | + |
| 129 | + // Onboard User Button is used to decommission the Matter Node |
| 130 | + if (button_state && digitalRead(buttonPin) == HIGH) { |
| 131 | + button_state = false; // released |
| 132 | + } |
| 133 | + |
| 134 | + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node |
| 135 | + uint32_t time_diff = millis() - button_time_stamp; |
| 136 | + if (button_state && time_diff > decommissioningTimeout) { |
| 137 | + Serial.println("Decommissioning the Plugin Matter Accessory. It shall be commissioned again."); |
| 138 | + OnOffPlugin.setOnOff(false); // turn the plugin off |
| 139 | + Matter.decommission(); |
| 140 | + button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so |
| 141 | + } |
| 142 | +} |
0 commit comments