|
| 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 an example code that will create a Matter Device which can be |
| 17 | + * commissioned and controlled from a Matter Environment APP. |
| 18 | + * Additionally the ESP32 will send debug messages indicating the Matter activity. |
| 19 | + * Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages. |
| 20 | + * |
| 21 | + * The example will create a Matter Occupancy Sensor Device. |
| 22 | + * The Occupancy Sensor will be simulated to change its state every 2 minutes. |
| 23 | + * |
| 24 | + * The onboard button can be kept pressed for 5 seconds to decommission the Matter Node. |
| 25 | + * The example will also show the manual commissioning code and QR code to be used in the Matter environment. |
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | +// Matter Manager |
| 30 | +#include <Matter.h> |
| 31 | +#include <WiFi.h> |
| 32 | + |
| 33 | +// List of Matter Endpoints for this Node |
| 34 | +// Matter Occupancy Sensor Endpoint |
| 35 | +MatterOccupancySensor OccupancySensor; |
| 36 | + |
| 37 | +// set your board USER BUTTON pin here - decommissioning only |
| 38 | +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. |
| 39 | + |
| 40 | +// WiFi is manually set and started |
| 41 | +const char *ssid = "your-ssid"; // Change this to your WiFi SSID |
| 42 | +const char *password = "your-password"; // Change this to your WiFi password |
| 43 | + |
| 44 | +// Button control |
| 45 | +uint32_t button_time_stamp = 0; // debouncing control |
| 46 | +bool button_state = false; // false = released | true = pressed |
| 47 | +const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission |
| 48 | + |
| 49 | +void setup() { |
| 50 | + // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node |
| 51 | + pinMode(buttonPin, INPUT_PULLUP); |
| 52 | + |
| 53 | + Serial.begin(115200); |
| 54 | + |
| 55 | + // Manually connect to WiFi |
| 56 | + WiFi.begin(ssid, password); |
| 57 | + // Wait for connection |
| 58 | + while (WiFi.status() != WL_CONNECTED) { |
| 59 | + delay(500); |
| 60 | + Serial.print("."); |
| 61 | + } |
| 62 | + Serial.println(); |
| 63 | + |
| 64 | + // set initial occupancy sensor state as false and connected to a PIR sensor type (default) |
| 65 | + OccupancySensor.begin(); |
| 66 | + |
| 67 | + // Matter beginning - Last step, after all EndPoints are initialized |
| 68 | + Matter.begin(); |
| 69 | + |
| 70 | + // Check Matter Accessory Commissioning state, which may change during execution of loop() |
| 71 | + if (!Matter.isDeviceCommissioned()) { |
| 72 | + Serial.println(""); |
| 73 | + Serial.println("Matter Node is not commissioned yet."); |
| 74 | + Serial.println("Initiate the device discovery in your Matter environment."); |
| 75 | + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 76 | + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); |
| 77 | + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 78 | + // waits for Matter Occupancy Sensor Commissioning. |
| 79 | + uint32_t timeCount = 0; |
| 80 | + while (!Matter.isDeviceCommissioned()) { |
| 81 | + delay(100); |
| 82 | + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec |
| 83 | + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); |
| 84 | + } |
| 85 | + } |
| 86 | + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +bool simulatedHWOccupancySensor() { |
| 91 | + // Simulated Occupancy Sensor |
| 92 | + static bool occupancyState = false; |
| 93 | + static uint32_t lastTime = millis(); |
| 94 | + const uint32_t occupancyTimeout = 120000; // 2 minutes to toggle the state |
| 95 | + |
| 96 | + // Simulate a Occupancy Sensor state change every 2 minutes |
| 97 | + if (millis() - lastTime > occupancyTimeout) { |
| 98 | + occupancyState = !occupancyState; |
| 99 | + lastTime = millis(); |
| 100 | + } |
| 101 | + return occupancyState; |
| 102 | +} |
| 103 | + |
| 104 | +void loop() { |
| 105 | + // Check if the button has been pressed |
| 106 | + if (digitalRead(buttonPin) == LOW && !button_state) { |
| 107 | + // deals with button debouncing |
| 108 | + button_time_stamp = millis(); // record the time while the button is pressed. |
| 109 | + button_state = true; // pressed. |
| 110 | + } |
| 111 | + |
| 112 | + if (button_state && digitalRead(buttonPin) == HIGH) { |
| 113 | + button_state = false; // released |
| 114 | + } |
| 115 | + |
| 116 | + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node |
| 117 | + uint32_t time_diff = millis() - button_time_stamp; |
| 118 | + if (button_state && time_diff > decommissioningTimeout) { |
| 119 | + Serial.println("Decommissioning the Generic Switch Matter Accessory. It shall be commissioned again."); |
| 120 | + Matter.decommission(); |
| 121 | + button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so |
| 122 | + } |
| 123 | + |
| 124 | + // Check Simulated Occupancy Sensor and set Matter Attribute |
| 125 | + OccupancySensor.setOccupancy(simulatedHWOccupancySensor()); |
| 126 | + |
| 127 | + delay(50); |
| 128 | +} |
0 commit comments