Skip to content

Commit ad74aa0

Browse files
authored
feat(matter): Arduino WiFi Prov example for Matter
1 parent 414e4f3 commit ad74aa0

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
Please read README.md file in this folder, or on the web:
3+
https://github.com/espressif/arduino-esp32/tree/master/libraries/WiFiProv/examples/WiFiProv
4+
5+
Note: This sketch takes up a lot of space for the app and may not be able to flash with default setting on some chips.
6+
If you see Error like this: "Sketch too big"
7+
In Arduino IDE go to: Tools > Partition scheme > chose anything that has more than 1.4MB APP
8+
- for example "No OTA (2MB APP/2MB SPIFFS)"
9+
10+
This example demonstrates that it is possible to provision WiFi using BLE or Software AP using
11+
the ESP BLE Prov APP or ESP SoftAP Provisioning APP from Android Play or/and iOS APP Store
12+
13+
Once the WiFi is provisioned, Matter will start its process as usual.
14+
15+
This same Example could be used for any other WiFi Provisioning method.
16+
*/
17+
18+
19+
// Matter Manager
20+
#include <Matter.h>
21+
#include <WiFiProv.h>
22+
#include <WiFi.h>
23+
24+
#if !CONFIG_BLUEDROID_ENABLED
25+
#define USE_SOFT_AP // ESP32-S2 has no BLE, therefore, it shall use SoftAP Provisioning
26+
#endif
27+
//#define USE_SOFT_AP // Uncomment if you want to enforce using the Soft AP method instead of BLE
28+
29+
const char *pop = "abcd1234"; // Proof of possession - otherwise called a PIN - string provided by the device, entered by the user in the phone app
30+
const char *service_name = "PROV_123"; // Name of your device (the Espressif apps expects by default device name starting with "Prov_")
31+
const char *service_key = NULL; // Password used for SofAP method (NULL = no password needed)
32+
bool reset_provisioned = true; // When true the library will automatically delete previously provisioned data.
33+
34+
// List of Matter Endpoints for this Node
35+
// Single On/Off Light Endpoint - at least one per node
36+
MatterOnOffLight OnOffLight;
37+
38+
// Light GPIO that can be controlled by Matter APP
39+
#ifdef LED_BUILTIN
40+
const uint8_t ledPin = LED_BUILTIN;
41+
#else
42+
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
43+
#endif
44+
45+
// Matter Protocol Endpoint (On/OFF Light) Callback
46+
bool matterCB(bool state) {
47+
digitalWrite(ledPin, state ? HIGH : LOW);
48+
// This callback must return the success state to Matter core
49+
return true;
50+
}
51+
52+
void setup() {
53+
Serial.begin(115200);
54+
// Initialize the LED GPIO
55+
pinMode(ledPin, OUTPUT);
56+
57+
WiFi.begin(); // no SSID/PWD - get it from the Provisioning APP or from NVS (last successful connection)
58+
59+
// BLE Provisioning using the ESP SoftAP Prov works fine for any BLE SoC, including ESP32, ESP32S3 and ESP32C3.
60+
#if CONFIG_BLUEDROID_ENABLED && !defined(USE_SOFT_AP)
61+
Serial.println("Begin Provisioning using BLE");
62+
// Sample uuid that user can pass during provisioning using BLE
63+
uint8_t uuid[16] = {0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf, 0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02};
64+
WiFiProv.beginProvision(
65+
NETWORK_PROV_SCHEME_BLE, NETWORK_PROV_SCHEME_HANDLER_FREE_BLE, NETWORK_PROV_SECURITY_1, pop, service_name, service_key, uuid, reset_provisioned
66+
);
67+
Serial.println("You may use this BLE QRCode:");
68+
WiFiProv.printQR(service_name, pop, "ble");
69+
#else
70+
Serial.println("Begin Provisioning using Soft AP");
71+
WiFiProv.beginProvision(NETWORK_PROV_SCHEME_SOFTAP, NETWORK_PROV_SCHEME_HANDLER_NONE, NETWORK_PROV_SECURITY_1, pop, service_name, service_key);
72+
Serial.println("You may use this WiFi QRCode:");
73+
WiFiProv.printQR(service_name, pop, "softap");
74+
#endif
75+
76+
// Wait for WiFi connection
77+
uint32_t counter = 0;
78+
while (WiFi.status() != WL_CONNECTED) {
79+
// resets the device after 10 minutes
80+
if (counter > 2 * 60 * 10) {
81+
Serial.println("\r\n================================================");
82+
Serial.println("Already 10 minutes past. The device will reboot.");
83+
Serial.println("================================================\r\n");
84+
Serial.flush(); // wait until the Serial has sent the whole message.
85+
ESP.restart();
86+
}
87+
// WiFi searching feedback
88+
Serial.print(".");
89+
delay(500);
90+
// adds a new line every 30 seconds
91+
counter++;
92+
if (!(counter % 60)) {
93+
Serial.println();
94+
}
95+
}
96+
97+
// WiFi shall be connected by now
98+
Serial.println();
99+
100+
// Initialize at least one Matter EndPoint
101+
OnOffLight.begin();
102+
103+
// Associate a callback to the Matter Controller
104+
OnOffLight.onChange(matterCB);
105+
106+
// Matter beginning - Last step, after all EndPoints are initialized
107+
Matter.begin();
108+
109+
while (!Matter.isDeviceCommissioned()) {
110+
Serial.println("Matter Node is not commissioned yet.");
111+
Serial.println("Initiate the device discovery in your Matter environment.");
112+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
113+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
114+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
115+
Serial.println();
116+
// waits 30 seconds for Matter Commissioning, keeping it blocked until done
117+
delay(30000)
118+
}
119+
}
120+
121+
void loop() {
122+
delay(500);
123+
}

0 commit comments

Comments
 (0)