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