-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_provision.ino
100 lines (91 loc) · 3.73 KB
/
wifi_provision.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Please read README.md file in this folder, or on the web:
https://github.com/espressif/arduino-esp32/tree/master/libraries/WiFiProv/examples/WiFiProv
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. If you see Error like this: "Sketch
too big" In Arduino IDE go to: Tools > Partition scheme > chose anything that
has more than 1.4MB APP
- for example "No OTA (2MB APP/2MB SPIFFS)"
*/
#include "WiFi.h"
#include "WiFiProv.h"
#define USE_SOFT_AP // Uncomment if you want to enforce using Soft AP method
// instead of BLE
const char *pop =
"8765432a"; // Proof of possession - otherwise called a PIN - string
// provided by the device, entered by user in the phone app
const char *service_name =
"esp32S3"; // Name of your device (the Espressif apps expects by default
// device name starting with "Prov_")
const char *service_key =
NULL; // Password used for SofAP method (NULL = no password needed)
bool reset_provisioned = false; // When true the library will automatically
// delete previously provisioned data.
void SysProvEvent(arduino_event_t *sys_event) {
switch (sys_event->event_id) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.print("\nConnected IP address : ");
Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr));
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("\nDisconnected. Connecting to the AP again... ");
break;
case ARDUINO_EVENT_PROV_START:
Serial.println("\nProvisioning started\nGive Credentials of your access "
"point using smartphone app");
break;
case ARDUINO_EVENT_PROV_CRED_RECV: {
Serial.println("\nReceived Wi-Fi credentials");
Serial.print("\tSSID : ");
Serial.println((const char *)sys_event->event_info.prov_cred_recv.ssid);
Serial.print("\tPassword : ");
Serial.println((char const *)sys_event->event_info.prov_cred_recv.password);
break;
}
case ARDUINO_EVENT_PROV_CRED_FAIL: {
Serial.println("\nProvisioning failed!\nPlease reset to factory and retry "
"provisioning\n");
if (sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR)
Serial.println("\nWi-Fi AP password incorrect");
else
Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" "
"before beginProvision()");
break;
}
case ARDUINO_EVENT_PROV_CRED_SUCCESS:
Serial.println("\nProvisioning Successful");
break;
case ARDUINO_EVENT_PROV_END:
Serial.println("\nProvisioning Ends");
break;
default:
break;
}
}
void setup() {
Serial.begin(115200);
WiFi.onEvent(SysProvEvent);
#if CONFIG_IDF_TARGET_ESP32 && CONFIG_BLUEDROID_ENABLED && not USE_SOFT_AP
Serial.println("Begin Provisioning using BLE");
// Sample uuid that user can pass during provisioning using BLE
uint8_t uuid[16] = {0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02};
WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE,
WIFI_PROV_SCHEME_HANDLER_FREE_BTDM,
WIFI_PROV_SECURITY_1, pop, service_name, service_key,
uuid, reset_provisioned);
#else
Serial.println("Begin Provisioning using Soft AP");
WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP,
WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1,
pop, service_name, service_key);
#endif
#if CONFIG_BLUEDROID_ENABLED && not USE_SOFT_AP
log_d("ble qr");
WiFiProv.printQR(service_name, pop, "ble");
#else
log_d("wifi qr");
WiFiProv.printQR(service_name, pop, "softap");
#endif
}
void loop() {}