Skip to content

Commit

Permalink
fix(prov): Improve WiFiProv library and QRcode print
Browse files Browse the repository at this point in the history
  • Loading branch information
me-no-dev committed Aug 14, 2024
1 parent e23c2b0 commit b81e06e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 21 deletions.
8 changes: 4 additions & 4 deletions libraries/RainMaker/examples/RMakerCustom/RMakerCustom.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ void sysProvEvent(arduino_event_t *sys_event) {
case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32S2
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
WiFiProv.printQR(service_name, pop, "softap");
#else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
WiFiProv.printQR(service_name, pop, "ble");
#endif
break;
case ARDUINO_EVENT_PROV_INIT: network_prov_mgr_disable_auto_stop(10000); break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: network_prov_mgr_stop_provisioning(); break;
case ARDUINO_EVENT_PROV_INIT: WiFiProv.disableAutoStop(10000); break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: WiFiProv.endProvision(); break;
default: ;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ void sysProvEvent(arduino_event_t *sys_event) {
case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32S2
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
WiFiProv.printQR(service_name, pop, "softap");
#else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
WiFiProv.printQR(service_name, pop, "ble");
#endif
break;
case ARDUINO_EVENT_PROV_INIT: network_prov_mgr_disable_auto_stop(10000); break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: network_prov_mgr_stop_provisioning(); break;
case ARDUINO_EVENT_PROV_INIT: WiFiProv.disableAutoStop(10000); break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: WiFiProv.endProvision(); break;
default: ;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ void sysProvEvent(arduino_event_t *sys_event) {
case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
WiFiProv.printQR(service_name, pop, "ble");
#else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
WiFiProv.printQR(service_name, pop, "softap");
#endif
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.printf("\nConnected to Wi-Fi!\n");
digitalWrite(gpio_led, true);
break;
case ARDUINO_EVENT_PROV_INIT: network_prov_mgr_disable_auto_stop(10000); break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: network_prov_mgr_stop_provisioning(); break;
case ARDUINO_EVENT_PROV_INIT: WiFiProv.disableAutoStop(10000); break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: WiFiProv.endProvision(); break;
default: ;
}
}
Expand Down
8 changes: 4 additions & 4 deletions libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ void sysProvEvent(arduino_event_t *sys_event) {
case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32S2
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
WiFiProv.printQR(service_name, pop, "softap");
#else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
WiFiProv.printQR(service_name, pop, "ble");
#endif
break;
case ARDUINO_EVENT_PROV_INIT: network_prov_mgr_disable_auto_stop(10000); break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: network_prov_mgr_stop_provisioning(); break;
case ARDUINO_EVENT_PROV_INIT: WiFiProv.disableAutoStop(10000); break;
case ARDUINO_EVENT_PROV_CRED_SUCCESS: WiFiProv.endProvision(); break;
default: ;
}
}
Expand Down
78 changes: 74 additions & 4 deletions libraries/WiFiProv/src/WiFiProv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,75 @@ void WiFiProvClass ::beginProvision(
}
}

void WiFiProvClass::endProvision() {
network_prov_mgr_stop_provisioning();
}

bool WiFiProvClass::disableAutoStop(uint32_t cleanup_delay) {
esp_err_t err = network_prov_mgr_disable_auto_stop(cleanup_delay);
if (err != ESP_OK) {
log_e("disable_auto_stop failed!");
}
return err == ESP_OK;
}

// Copied from IDF example
void WiFiProvClass ::printQR(const char *name, const char *pop, const char *transport) {

#if __has_include("qrcode.h")
static const char *lt[] = {
/* 0 */ " ",
/* 1 */ "\u2580 ",
/* 2 */ " \u2580",
/* 3 */ "\u2580\u2580",
/* 4 */ "\u2584 ",
/* 5 */ "\u2588 ",
/* 6 */ "\u2584\u2580",
/* 7 */ "\u2588\u2580",
/* 8 */ " \u2584",
/* 9 */ "\u2580\u2584",
/* 10 */ " \u2588",
/* 11 */ "\u2580\u2588",
/* 12 */ "\u2584\u2584",
/* 13 */ "\u2588\u2584",
/* 14 */ "\u2584\u2588",
/* 15 */ "\u2588\u2588",
};

static Print *qr_out = NULL;

static void _qrcode_print_console(esp_qrcode_handle_t qrcode) {
int size = esp_qrcode_get_size(qrcode);
int border = 2;
unsigned char num = 0;

if (qr_out == NULL) {
return;
}

for (int y = -border; y < size + border; y += 2) {
for (int x = -border; x < size + border; x += 2) {
num = 0;
if (esp_qrcode_get_module(qrcode, x, y)) {
num |= 1 << 0;
}
if ((x < size + border) && esp_qrcode_get_module(qrcode, x + 1, y)) {
num |= 1 << 1;
}
if ((y < size + border) && esp_qrcode_get_module(qrcode, x, y + 1)) {
num |= 1 << 2;
}
if ((x < size + border) && (y < size + border) && esp_qrcode_get_module(qrcode, x + 1, y + 1)) {
num |= 1 << 3;
}
qr_out->print(lt[num]);
}
qr_out->print("\n");
}
qr_out->print("\n");
}
#endif

void WiFiProvClass::printQR(const char *name, const char *pop, const char *transport, Print &out) {
if (!name || !transport) {
log_w("Cannot generate QR code payload. Data missing.");
return;
Expand All @@ -191,12 +258,15 @@ void WiFiProvClass ::printQR(const char *name, const char *pop, const char *tran
);
}
#if __has_include("qrcode.h")
log_i("Scan this QR code from the provisioning application for Provisioning.");
esp_qrcode_config_t cfg = ESP_QRCODE_CONFIG_DEFAULT();
cfg.display_func = _qrcode_print_console;
out.printf("Scan this QR code from the provisioning application for Provisioning.\n");
qr_out = &out;
esp_qrcode_generate(&cfg, payload);
qr_out = NULL;
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);
#else
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);
log_i("If you are using Arduino as IDF component, install ESP Rainmaker:\nhttps://github.com/espressif/esp-rainmaker");
out.println("If you are using Arduino as IDF component, install ESP Rainmaker:\nhttps://github.com/espressif/esp-rainmaker");
#endif
}

Expand Down
5 changes: 4 additions & 1 deletion libraries/WiFiProv/src/WiFiProv.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#if SOC_WIFI_SUPPORTED

#include "WiFi.h"
#include "HardwareSerial.h"
#include "network_provisioning/manager.h"
//Select the scheme using which you want to provision
typedef enum {
Expand Down Expand Up @@ -51,7 +52,9 @@ class WiFiProvClass {
network_prov_security_t security = NETWORK_PROV_SECURITY_1, const char *pop = "abcd1234", const char *service_name = NULL, const char *service_key = NULL,
uint8_t *uuid = NULL, bool reset_provisioned = false
);
void printQR(const char *name, const char *pop, const char *transport);
void endProvision();
bool disableAutoStop(uint32_t cleanup_delay);
void printQR(const char *name, const char *pop, const char *transport, Print &out = Serial);
};

extern WiFiProvClass WiFiProv;
Expand Down

0 comments on commit b81e06e

Please sign in to comment.