1
+ #include < Arduino.h>
2
+ #include < WiFi.h>
3
+
4
+ // Matter Manager
5
+ #include < Matter.h>
6
+
7
+ // List of Matter Endpoints for this Node
8
+ // On/Off Light Endpoint
9
+ #include < MatterOnOffLight.h>
10
+ MatterOnOffLight OnOffLight;
11
+
12
+ // set your board LED pin here
13
+ #ifdef LED_BUILTIN
14
+ const uint8_t ledPin = LED_BUILTIN;
15
+ #else
16
+ const uint8_t ledPin = 2 ; // Set your pin here if your board has not defined LED_BUILTIN
17
+ #warning "Do not forget to set the LED pin"
18
+ #endif
19
+
20
+ // Matter Protocol Endpoint Callback
21
+ bool setLightOnOff (bool state) {
22
+ Serial.printf (" User Callback :: New Light State = %s\r\n " , state ? " ON" : " OFF" );
23
+ if (state) {
24
+ digitalWrite (ledPin, HIGH);
25
+ } else {
26
+ digitalWrite (ledPin, LOW);
27
+ }
28
+ // This callback must return the success state to Matter core
29
+ return true ;
30
+ }
31
+
32
+ // WiFi is manually set and stated
33
+ const char *ssid = " your-ssid" ; // Change this to your WiFi SSID
34
+ const char *password = " your-password" ; // Change this to your WiFi password
35
+
36
+ void setup () {
37
+ Serial.begin (115200 );
38
+ while (!Serial) {
39
+ delay (100 );
40
+ }
41
+
42
+ // We start by connecting to a WiFi network
43
+ Serial.print (" Connecting to " );
44
+ Serial.println (ssid);
45
+ // enable IPv6
46
+ WiFi.enableIPv6 (true );
47
+ // Manually connect to WiFi
48
+ WiFi.begin (ssid, password);
49
+ // Wait for connection
50
+ while (WiFi.status () != WL_CONNECTED) {
51
+ delay (500 );
52
+ Serial.print (" ." );
53
+ }
54
+ Serial.println (" \r\n WiFi connected" );
55
+ Serial.println (" IP address: " );
56
+ Serial.println (WiFi.localIP ());
57
+ delay (500 );
58
+
59
+ // Initialize Matter Node
60
+ Matter.begin ();
61
+
62
+ // Initialize Matter EndPoint
63
+ OnOffLight.begin (true );
64
+ OnOffLight.onChangeOnOff (setLightOnOff);
65
+
66
+ // Matter start - Last step, after all EndPoints are initialized
67
+ Matter.start ();
68
+ }
69
+
70
+ uint32_t lastMillis = millis();
71
+ const uint32_t toggle_interval = 15000 ; // light will toggle every 15 seconds
72
+ void loop () {
73
+ // Check Matter Light Commissioning state
74
+ if (!Matter.isDeviceCommissioned ()) {
75
+ Serial.println (" " );
76
+ Serial.println (" Matter Node is not commissioned yet." );
77
+ Serial.println (" Initiate the device discovery in your Matter environment." );
78
+ Serial.println (" Commission it to your Matter hub with the manual pairing code or QR code" );
79
+ Serial.printf (" Manual pairing code: %s\r\n " , Matter.getManualPairingCode ().c_str ());
80
+ Serial.printf (" QR code URL: %s\r\n " , Matter.getOnboardingQRCodeUrl ().c_str ());
81
+ // waits for Matter Light Commissioning.
82
+ uint32_t timeCount = 0 ;
83
+ while (!Matter.isDeviceCommissioned ()) {
84
+ delay (100 );
85
+ if ((timeCount++ % 50 ) == 0 ) { // 50*100ms = 5 sec
86
+ Serial.println (" Matter Node not commissioned yet. Waiting for commissioning." );
87
+ }
88
+ }
89
+ // Initialize the LED (light) GPIO and Matter End Point
90
+ pinMode (ledPin, OUTPUT);
91
+ Serial.printf (" Initial state: %s\r\n " , OnOffLight.getOnOff () ? " ON" : " OFF" );
92
+ setLightOnOff (OnOffLight.getOnOff ()); // configure the Light based on initial state
93
+ Serial.println (" Matter Node is commissioned and connected to Wi-Fi. Ready for use." );
94
+ delay (10000 );
95
+ }
96
+
97
+ // displays the Light state every 3 seconds
98
+ Serial.printf (" Matter Light is %s\r\n " , OnOffLight.getOnOff () ? " ON" : " OFF" );
99
+ delay (3000 );
100
+ if (millis () - lastMillis > toggle_interval) {
101
+ Serial.println (" Toggling Light!" );
102
+ lastMillis = millis ();
103
+ OnOffLight.toggle (); // Matter Controller also can see the change
104
+ }
105
+ }
0 commit comments