Skip to content

Commit 79f8016

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

File tree

6 files changed

+99
-23
lines changed

6 files changed

+99
-23
lines changed

libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino

Lines changed: 4 additions & 4 deletions
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
}

libraries/RainMaker/examples/RMakerCustomAirCooler/RMakerCustomAirCooler.ino

Lines changed: 4 additions & 4 deletions
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
}

libraries/RainMaker/examples/RMakerSonoffDualR3/RMakerSonoffDualR3.ino

Lines changed: 4 additions & 4 deletions
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
}

libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino

Lines changed: 4 additions & 4 deletions
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
}

libraries/WiFiProv/src/WiFiProv.cpp

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <esp32-hal.h>
3131
#if __has_include("qrcode.h")
3232
#include "qrcode.h"
33+
#include "qrcodegen.h"
3334
#endif
3435

3536
#include <nvs_flash.h>
@@ -168,8 +169,75 @@ void WiFiProvClass ::beginProvision(
168169
}
169170
}
170171

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

libraries/WiFiProv/src/WiFiProv.h

Lines changed: 4 additions & 1 deletion
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)