|
| 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 | + |
| 22 | +// Matter Manager |
| 23 | +#include <Matter.h> |
| 24 | +#include <WiFi.h> |
| 25 | + |
| 26 | +// List of Matter Endpoints for this Node |
| 27 | +// Matter Pressure Sensor Endpoint |
| 28 | +MatterPressureSensor SimulatedPressureSensor; |
| 29 | + |
| 30 | +// set your board USER BUTTON pin here - decommissioning button |
| 31 | +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. |
| 32 | + |
| 33 | +// WiFi is manually set and started |
| 34 | +const char *ssid = "your-ssid"; // Change this to your WiFi SSID |
| 35 | +const char *password = "your-password"; // Change this to your WiFi password |
| 36 | + |
| 37 | +// Button control - decommision the Matter Node |
| 38 | +uint32_t button_time_stamp = 0; // debouncing control |
| 39 | +bool button_state = false; // false = released | true = pressed |
| 40 | +const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission |
| 41 | + |
| 42 | +// Simulate a pressure sensor - add your preferred pressure sensor library code here |
| 43 | +float getSimulatedPressure() { |
| 44 | + // The Endpoint implementation keeps an uint16_t as internal value information, |
| 45 | + // which stores data in hPa (pressure measurement unit) |
| 46 | + static float simulatedPressureHWSensor = 950; |
| 47 | + |
| 48 | + // it will increase from 950 to 1100 hPa in steps of 10 hPa to simulate a pressure sensor |
| 49 | + simulatedPressureHWSensor = simulatedPressureHWSensor + 10; |
| 50 | + if (simulatedPressureHWSensor > 1100) { |
| 51 | + simulatedPressureHWSensor = 950; |
| 52 | + } |
| 53 | + |
| 54 | + return simulatedPressureHWSensor; |
| 55 | +} |
| 56 | + |
| 57 | +void setup() { |
| 58 | + // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node |
| 59 | + pinMode(buttonPin, INPUT_PULLUP); |
| 60 | + |
| 61 | + Serial.begin(115200); |
| 62 | + |
| 63 | + // Manually connect to WiFi |
| 64 | + WiFi.begin(ssid, password); |
| 65 | + // Wait for connection |
| 66 | + while (WiFi.status() != WL_CONNECTED) { |
| 67 | + delay(500); |
| 68 | + Serial.print("."); |
| 69 | + } |
| 70 | + Serial.println(); |
| 71 | + |
| 72 | + // set initial pressure sensor measurement |
| 73 | + // Simulated Sensor - it shall initially print 900hPa and then move to the 950 to 1100 hPa as pressure range |
| 74 | + SimulatedPressureSensor.begin(900.00); |
| 75 | + |
| 76 | + // Matter beginning - Last step, after all EndPoints are initialized |
| 77 | + Matter.begin(); |
| 78 | + |
| 79 | + // Check Matter Accessory Commissioning state, which may change during execution of loop() |
| 80 | + if (!Matter.isDeviceCommissioned()) { |
| 81 | + Serial.println(""); |
| 82 | + Serial.println("Matter Node is not commissioned yet."); |
| 83 | + Serial.println("Initiate the device discovery in your Matter environment."); |
| 84 | + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); |
| 85 | + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); |
| 86 | + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); |
| 87 | + // waits for Matter Pressure Sensor Commissioning. |
| 88 | + uint32_t timeCount = 0; |
| 89 | + while (!Matter.isDeviceCommissioned()) { |
| 90 | + delay(100); |
| 91 | + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec |
| 92 | + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); |
| 93 | + } |
| 94 | + } |
| 95 | + Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use."); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +void loop() { |
| 100 | + static uint32_t timeCounter = 0; |
| 101 | + |
| 102 | + // Print the current pressure value every 5s |
| 103 | + if (!(timeCounter++ % 10)) { // delaying for 500ms x 10 = 5s |
| 104 | + // Print the current pressure value |
| 105 | + Serial.printf("Current Pressure is %.02fhPa\r\n", SimulatedPressureSensor.getPressure()); |
| 106 | + // Update Pressure from the (Simulated) Hardware Sensor |
| 107 | + // Matter APP shall display the updated pressure percent |
| 108 | + SimulatedPressureSensor.setPressure(getSimulatedPressure()); |
| 109 | + } |
| 110 | + |
| 111 | + // Check if the button has been pressed |
| 112 | + if (digitalRead(buttonPin) == LOW && !button_state) { |
| 113 | + // deals with button debouncing |
| 114 | + button_time_stamp = millis(); // record the time while the button is pressed. |
| 115 | + button_state = true; // pressed. |
| 116 | + } |
| 117 | + |
| 118 | + if (digitalRead(buttonPin) == HIGH && button_state) { |
| 119 | + button_state = false; // released |
| 120 | + } |
| 121 | + |
| 122 | + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node |
| 123 | + uint32_t time_diff = millis() - button_time_stamp; |
| 124 | + if (button_state && time_diff > decommissioningTimeout) { |
| 125 | + // Factory reset is triggered if the button is pressed longer than 10 seconds |
| 126 | + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); |
| 127 | + Matter.decommission(); |
| 128 | + } |
| 129 | + |
| 130 | + delay(500); |
| 131 | +} |
0 commit comments