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