|
| 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 | +// Color Light Endpoint |
| 22 | +MatterEnhancedColorLight EnhancedColorLight; |
| 23 | + |
| 24 | +// It will use HSV color to control all Matter Attribute Changes |
| 25 | +HsvColor_t currentHSVColor = {0, 0, 0}; |
| 26 | + |
| 27 | +// it will keep last OnOff & HSV Color state stored, using Preferences |
| 28 | +Preferences matterPref; |
| 29 | +const char *onOffPrefKey = "OnOff"; |
| 30 | +const char *hsvColorPrefKey = "HSV"; |
| 31 | + |
| 32 | +// set your board RGB LED pin here |
| 33 | +#ifdef RGB_BUILTIN |
| 34 | +const uint8_t ledPin = RGB_BUILTIN; |
| 35 | +#else |
| 36 | +const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN |
| 37 | +#warning "Do not forget to set the RGB LED pin" |
| 38 | +#endif |
| 39 | + |
| 40 | +// set your board USER BUTTON pin here |
| 41 | +const uint8_t buttonPin = 0; // Set your pin here. Using BOOT Button. C6/C3 use GPIO9. |
| 42 | + |
| 43 | +// WiFi is manually set and started |
| 44 | +const char *ssid = "your-ssid"; // Change this to your WiFi SSID |
| 45 | +const char *password = "your-password"; // Change this to your WiFi password |
| 46 | + |
| 47 | +// Set the RGB LED Light based on the current state of the Enhanced Color Light |
| 48 | +bool setLightState(bool state, espHsvColor_t colorHSV, uint8_t brighteness, uint16_t temperature_Mireds) { |
| 49 | + |
| 50 | + if (state) { |
| 51 | +#ifdef RGB_BUILTIN |
| 52 | + // currentHSVColor keeps final color result |
| 53 | + espRgbColor_t rgbColor = espHsvColorToRgbColor(currentHSVColor); |
| 54 | + // set the RGB LED |
| 55 | + rgbLedWrite(ledPin, rgbColor.r, rgbColor.g, rgbColor.b); |
| 56 | +#else |
| 57 | + // No Color RGB LED, just use the HSV value (brightness) to control the LED |
| 58 | + analogWrite(ledPin, colorHSV.v); |
| 59 | +#endif |
| 60 | + } else { |
| 61 | + digitalWrite(ledPin, LOW); |
| 62 | + } |
| 63 | + // store last HSV Color and OnOff state for when the Light is restarted / power goes off |
| 64 | + matterPref.putBool(onOffPrefKey, state); |
| 65 | + matterPref.putUInt(hsvColorPrefKey, currentHSVColor.h << 16 | currentHSVColor.s << 8 | currentHSVColor.v); |
| 66 | + // This callback must return the success state to Matter core |
| 67 | + return true; |
| 68 | +} |
| 69 | + |
| 70 | +void setup() { |
| 71 | + // Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch |
| 72 | + pinMode(buttonPin, INPUT_PULLUP); |
| 73 | + // Initialize the LED (light) GPIO and Matter End Point |
| 74 | + pinMode(ledPin, OUTPUT); |
| 75 | + |
| 76 | + Serial.begin(115200); |
| 77 | + while (!Serial) { |
| 78 | + delay(100); |
| 79 | + } |
| 80 | + |
| 81 | + // We start by connecting to a WiFi network |
| 82 | + Serial.print("Connecting to "); |
| 83 | + Serial.println(ssid); |
| 84 | + // enable IPv6 |
| 85 | + WiFi.enableIPv6(true); |
| 86 | + // Manually connect to WiFi |
| 87 | + WiFi.begin(ssid, password); |
| 88 | + // Wait for connection |
| 89 | + while (WiFi.status() != WL_CONNECTED) { |
| 90 | + delay(500); |
| 91 | + Serial.print("."); |
| 92 | + } |
| 93 | + Serial.println("\r\nWiFi connected"); |
| 94 | + Serial.println("IP address: "); |
| 95 | + Serial.println(WiFi.localIP()); |
| 96 | + delay(500); |
| 97 | + |
| 98 | + // Initialize Matter EndPoint |
| 99 | + matterPref.begin("MatterPrefs", false); |
| 100 | + // default OnOff state is ON if not stored before |
| 101 | + bool lastOnOffState = matterPref.getBool(onOffPrefKey, true); |
| 102 | + // default HSV color is (21, 216, 25) - Warm White Color at 10% intensity |
| 103 | + uint32_t prefHsvColor = matterPref.getUInt(hsvColorPrefKey, 21 << 16 | 216 << 8 | 25); |
| 104 | + currentHSVColor = {uint8_t(prefHsvColor >> 16), uint8_t(prefHsvColor >> 8), uint8_t(prefHsvColor)}; |
| 105 | + EnhancedColorLight.begin(lastOnOffState, currentHSVColor); |
| 106 | + // set the callback function to handle the Light state change |
| 107 | + EnhancedColorLight.onChange(setLightState); |
| 108 | + |
| 109 | + // lambda functions are used to set the attribute change callbacks |
| 110 | + EnhancedColorLight.onChangeOnOff([](bool state) { |
| 111 | + Serial.printf("Light OnOff changed to %s\r\n", state ? "ON" : "OFF"); |
| 112 | + return true; |
| 113 | + }); |
| 114 | + EnhancedColorLight.onChangeColorTemperature([](uint16_t colorTemperature) { |
| 115 | + Serial.printf("Light Color Temperature changed to %d\r\n", colorTemperature); |
| 116 | + // get correspondent Hue and Saturation of the color temperature |
| 117 | + HsvColor_t hsvTemperature = espRgbColorToHsvColor(espCTToRgbColor(colorTemperature)); |
| 118 | + // keep previous the brightness and just change the Hue and Saturation |
| 119 | + currentHSVColor.h = hsvTemperature.h; |
| 120 | + currentHSVColor.s = hsvTemperature.s; |
| 121 | + return true; |
| 122 | + }); |
| 123 | + EnhancedColorLight.onChangeBrightness([](uint8_t brightness) { |
| 124 | + Serial.printf("Light brightness changed to %d\r\n", brightness); |
| 125 | + // change current brightness (HSV value) |
| 126 | + currentHSVColor.v = brightness; |
| 127 | + return true; |
| 128 | + }); |
| 129 | + EnhancedColorLight.onChangeColorHSV([](HsvColor_t hsvColor) { |
| 130 | + Serial.printf("Light HSV Color changed to (%d,%d,%d)\r\n", hsvColor.h, hsvColor.s, hsvColor.v); |
| 131 | + // keep the current brightness and just change Hue and Saturation |
| 132 | + currentHSVColor.h = hsvColor.h; |
| 133 | + currentHSVColor.s = hsvColor.s; |
| 134 | + return true; |
| 135 | + }); |
| 136 | + |
| 137 | + // Matter beginning - Last step, after all EndPoints are initialized |
| 138 | + Matter.begin(); |
| 139 | + // This may be a restart of a already commissioned Matter accessory |
| 140 | + if (Matter.isDeviceCommissioned()) { |
| 141 | + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); |
| 142 | + Serial.printf( |
| 143 | + "Initial state: %s | RGB Color: (%d,%d,%d) \r\n", EnhancedColorLight ? "ON" : "OFF", EnhancedColorLight.getColorRGB().r, |
| 144 | + EnhancedColorLight.getColorRGB().g, EnhancedColorLight.getColorRGB().b |
| 145 | + ); |
| 146 | + // configure the Light based on initial on-off state and its color |
| 147 | + EnhancedColorLight.updateAccessory(); |
| 148 | + } |
| 149 | +} |
| 150 | +// Button control |
| 151 | +uint32_t button_time_stamp = 0; // debouncing control |
| 152 | +bool button_state = false; // false = released | true = pressed |
| 153 | +const uint32_t debouceTime = 250; // button debouncing time (ms) |
| 154 | +const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission the light |
| 155 | + |
| 156 | +void loop() { |
| 157 | + // Check Matter Light Commissioning state, which may change during execution of loop() |
| 158 | + if (!Matter.isDeviceCommissioned()) { |
| 159 | + Serial.println(""); |
| 160 | + Serial.println("Matter Node is not commissioned yet."); |
| 161 | + Serial.println("Initiate the device discovery in your Matter environment."); |
| 162 | + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 163 | + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); |
| 164 | + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 165 | + // waits for Matter Light Commissioning. |
| 166 | + uint32_t timeCount = 0; |
| 167 | + while (!Matter.isDeviceCommissioned()) { |
| 168 | + delay(100); |
| 169 | + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec |
| 170 | + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); |
| 171 | + } |
| 172 | + } |
| 173 | + Serial.printf( |
| 174 | + "Initial state: %s | RGB Color: (%d,%d,%d) \r\n", EnhancedColorLight ? "ON" : "OFF", EnhancedColorLight.getColorRGB().r, |
| 175 | + EnhancedColorLight.getColorRGB().g, EnhancedColorLight.getColorRGB().b |
| 176 | + ); |
| 177 | + // configure the Light based on initial on-off state and its color |
| 178 | + EnhancedColorLight.updateAccessory(); |
| 179 | + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); |
| 180 | + } |
| 181 | + |
| 182 | + // A button is also used to control the light |
| 183 | + // Check if the button has been pressed |
| 184 | + if (digitalRead(buttonPin) == LOW && !button_state) { |
| 185 | + // deals with button debouncing |
| 186 | + button_time_stamp = millis(); // record the time while the button is pressed. |
| 187 | + button_state = true; // pressed. |
| 188 | + } |
| 189 | + |
| 190 | + // Onboard User Button is used as a Light toggle switch or to decommission it |
| 191 | + uint32_t time_diff = millis() - button_time_stamp; |
| 192 | + if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) { |
| 193 | + button_state = false; // released |
| 194 | + // Toggle button is released - toggle the light |
| 195 | + Serial.println("User button released. Toggling Light!"); |
| 196 | + EnhancedColorLight.toggle(); // Matter Controller also can see the change |
| 197 | + |
| 198 | + // Factory reset is triggered if the button is pressed longer than 10 seconds |
| 199 | + if (time_diff > decommissioningTimeout) { |
| 200 | + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); |
| 201 | + EnhancedColorLight = false; // turn the light off |
| 202 | + Matter.decommission(); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments