Skip to content

Commit b81e06e

Browse files
committed
fix(prov): Improve WiFiProv library and QRcode print
1 parent e23c2b0 commit b81e06e

File tree

6 files changed

+94
-21
lines changed

6 files changed

+94
-21
lines changed

Diff for: libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ void sysProvEvent(arduino_event_t *sys_event) {
3030
case ARDUINO_EVENT_PROV_START:
3131
#if CONFIG_IDF_TARGET_ESP32S2
3232
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
33-
printQR(service_name, pop, "softap");
33+
WiFiProv.printQR(service_name, pop, "softap");
3434
#else
3535
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
36-
printQR(service_name, pop, "ble");
36+
WiFiProv.printQR(service_name, pop, "ble");
3737
#endif
3838
break;
39-
case ARDUINO_EVENT_PROV_INIT: network_prov_mgr_disable_auto_stop(10000); break;
40-
case ARDUINO_EVENT_PROV_CRED_SUCCESS: network_prov_mgr_stop_provisioning(); break;
39+
case ARDUINO_EVENT_PROV_INIT: WiFiProv.disableAutoStop(10000); break;
40+
case ARDUINO_EVENT_PROV_CRED_SUCCESS: WiFiProv.endProvision(); break;
4141
default: ;
4242
}
4343
}

Diff for: libraries/RainMaker/examples/RMakerCustomAirCooler/RMakerCustomAirCooler.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ void sysProvEvent(arduino_event_t *sys_event) {
4646
case ARDUINO_EVENT_PROV_START:
4747
#if CONFIG_IDF_TARGET_ESP32S2
4848
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
49-
printQR(service_name, pop, "softap");
49+
WiFiProv.printQR(service_name, pop, "softap");
5050
#else
5151
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
52-
printQR(service_name, pop, "ble");
52+
WiFiProv.printQR(service_name, pop, "ble");
5353
#endif
5454
break;
55-
case ARDUINO_EVENT_PROV_INIT: network_prov_mgr_disable_auto_stop(10000); break;
56-
case ARDUINO_EVENT_PROV_CRED_SUCCESS: network_prov_mgr_stop_provisioning(); break;
55+
case ARDUINO_EVENT_PROV_INIT: WiFiProv.disableAutoStop(10000); break;
56+
case ARDUINO_EVENT_PROV_CRED_SUCCESS: WiFiProv.endProvision(); break;
5757
default: ;
5858
}
5959
}

Diff for: libraries/RainMaker/examples/RMakerSonoffDualR3/RMakerSonoffDualR3.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ void sysProvEvent(arduino_event_t *sys_event) {
4040
case ARDUINO_EVENT_PROV_START:
4141
#if CONFIG_IDF_TARGET_ESP32
4242
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
43-
printQR(service_name, pop, "ble");
43+
WiFiProv.printQR(service_name, pop, "ble");
4444
#else
4545
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
46-
printQR(service_name, pop, "softap");
46+
WiFiProv.printQR(service_name, pop, "softap");
4747
#endif
4848
break;
4949
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
5050
Serial.printf("\nConnected to Wi-Fi!\n");
5151
digitalWrite(gpio_led, true);
5252
break;
53-
case ARDUINO_EVENT_PROV_INIT: network_prov_mgr_disable_auto_stop(10000); break;
54-
case ARDUINO_EVENT_PROV_CRED_SUCCESS: network_prov_mgr_stop_provisioning(); break;
53+
case ARDUINO_EVENT_PROV_INIT: WiFiProv.disableAutoStop(10000); break;
54+
case ARDUINO_EVENT_PROV_CRED_SUCCESS: WiFiProv.endProvision(); break;
5555
default: ;
5656
}
5757
}

Diff for: libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ void sysProvEvent(arduino_event_t *sys_event) {
3131
case ARDUINO_EVENT_PROV_START:
3232
#if CONFIG_IDF_TARGET_ESP32S2
3333
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
34-
printQR(service_name, pop, "softap");
34+
WiFiProv.printQR(service_name, pop, "softap");
3535
#else
3636
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
37-
printQR(service_name, pop, "ble");
37+
WiFiProv.printQR(service_name, pop, "ble");
3838
#endif
3939
break;
40-
case ARDUINO_EVENT_PROV_INIT: network_prov_mgr_disable_auto_stop(10000); break;
41-
case ARDUINO_EVENT_PROV_CRED_SUCCESS: network_prov_mgr_stop_provisioning(); break;
40+
case ARDUINO_EVENT_PROV_INIT: WiFiProv.disableAutoStop(10000); break;
41+
case ARDUINO_EVENT_PROV_CRED_SUCCESS: WiFiProv.endProvision(); break;
4242
default: ;
4343
}
4444
}

Diff for: libraries/WiFiProv/src/WiFiProv.cpp

+74-4
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,75 @@ void WiFiProvClass ::beginProvision(
168168
}
169169
}
170170

171+
void WiFiProvClass::endProvision() {
172+
network_prov_mgr_stop_provisioning();
173+
}
174+
175+
bool WiFiProvClass::disableAutoStop(uint32_t cleanup_delay) {
176+
esp_err_t err = network_prov_mgr_disable_auto_stop(cleanup_delay);
177+
if (err != ESP_OK) {
178+
log_e("disable_auto_stop failed!");
179+
}
180+
return err == ESP_OK;
181+
}
182+
171183
// Copied from IDF example
172-
void WiFiProvClass ::printQR(const char *name, const char *pop, const char *transport) {
184+
185+
#if __has_include("qrcode.h")
186+
static const char *lt[] = {
187+
/* 0 */ " ",
188+
/* 1 */ "\u2580 ",
189+
/* 2 */ " \u2580",
190+
/* 3 */ "\u2580\u2580",
191+
/* 4 */ "\u2584 ",
192+
/* 5 */ "\u2588 ",
193+
/* 6 */ "\u2584\u2580",
194+
/* 7 */ "\u2588\u2580",
195+
/* 8 */ " \u2584",
196+
/* 9 */ "\u2580\u2584",
197+
/* 10 */ " \u2588",
198+
/* 11 */ "\u2580\u2588",
199+
/* 12 */ "\u2584\u2584",
200+
/* 13 */ "\u2588\u2584",
201+
/* 14 */ "\u2584\u2588",
202+
/* 15 */ "\u2588\u2588",
203+
};
204+
205+
static Print *qr_out = NULL;
206+
207+
static void _qrcode_print_console(esp_qrcode_handle_t qrcode) {
208+
int size = esp_qrcode_get_size(qrcode);
209+
int border = 2;
210+
unsigned char num = 0;
211+
212+
if (qr_out == NULL) {
213+
return;
214+
}
215+
216+
for (int y = -border; y < size + border; y += 2) {
217+
for (int x = -border; x < size + border; x += 2) {
218+
num = 0;
219+
if (esp_qrcode_get_module(qrcode, x, y)) {
220+
num |= 1 << 0;
221+
}
222+
if ((x < size + border) && esp_qrcode_get_module(qrcode, x + 1, y)) {
223+
num |= 1 << 1;
224+
}
225+
if ((y < size + border) && esp_qrcode_get_module(qrcode, x, y + 1)) {
226+
num |= 1 << 2;
227+
}
228+
if ((x < size + border) && (y < size + border) && esp_qrcode_get_module(qrcode, x + 1, y + 1)) {
229+
num |= 1 << 3;
230+
}
231+
qr_out->print(lt[num]);
232+
}
233+
qr_out->print("\n");
234+
}
235+
qr_out->print("\n");
236+
}
237+
#endif
238+
239+
void WiFiProvClass::printQR(const char *name, const char *pop, const char *transport, Print &out) {
173240
if (!name || !transport) {
174241
log_w("Cannot generate QR code payload. Data missing.");
175242
return;
@@ -191,12 +258,15 @@ void WiFiProvClass ::printQR(const char *name, const char *pop, const char *tran
191258
);
192259
}
193260
#if __has_include("qrcode.h")
194-
log_i("Scan this QR code from the provisioning application for Provisioning.");
195261
esp_qrcode_config_t cfg = ESP_QRCODE_CONFIG_DEFAULT();
262+
cfg.display_func = _qrcode_print_console;
263+
out.printf("Scan this QR code from the provisioning application for Provisioning.\n");
264+
qr_out = &out;
196265
esp_qrcode_generate(&cfg, payload);
266+
qr_out = NULL;
267+
out.printf("If QR code is not visible, copy paste the below URL in a browser.\nhttps://rainmaker.espressif.com/qrcode.html?data=%s\n", payload);
197268
#else
198-
log_i("If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s", "https://espressif.github.io/esp-jumpstart/qrcode.html", payload);
199-
log_i("If you are using Arduino as IDF component, install ESP Rainmaker:\nhttps://github.com/espressif/esp-rainmaker");
269+
out.println("If you are using Arduino as IDF component, install ESP Rainmaker:\nhttps://github.com/espressif/esp-rainmaker");
200270
#endif
201271
}
202272

Diff for: libraries/WiFiProv/src/WiFiProv.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#if SOC_WIFI_SUPPORTED
2424

2525
#include "WiFi.h"
26+
#include "HardwareSerial.h"
2627
#include "network_provisioning/manager.h"
2728
//Select the scheme using which you want to provision
2829
typedef enum {
@@ -51,7 +52,9 @@ class WiFiProvClass {
5152
network_prov_security_t security = NETWORK_PROV_SECURITY_1, const char *pop = "abcd1234", const char *service_name = NULL, const char *service_key = NULL,
5253
uint8_t *uuid = NULL, bool reset_provisioned = false
5354
);
54-
void printQR(const char *name, const char *pop, const char *transport);
55+
void endProvision();
56+
bool disableAutoStop(uint32_t cleanup_delay);
57+
void printQR(const char *name, const char *pop, const char *transport, Print &out = Serial);
5558
};
5659

5760
extern WiFiProvClass WiFiProv;

0 commit comments

Comments
 (0)