Merge branch 'feature/add_qrcode_in_provisioning_example' into 'master'

examples: Add QR code support for provisioning examples

Closes IDF-2030

See merge request espressif/esp-idf!12409
pull/6718/head
Mahavir Jain 2021-02-25 00:41:01 +00:00
commit 5db150b48a
15 zmienionych plików z 244 dodań i 6 usunięć

Wyświetl plik

@ -1,6 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/qrcode)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(ble_prov)

Wyświetl plik

@ -4,5 +4,5 @@
#
PROJECT_NAME := ble_prov
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/qrcode
include $(IDF_PATH)/make/project.mk

Wyświetl plik

@ -116,6 +116,41 @@ I (718390) app_prov: Stopping provisioning
I (718670) app_prov: Provisioning stopped
```
## QR Code Scanning
Enabling `CONFIG_EXAMPLE_PROV_SHOW_QR` will display a QR code on the serial terminal, which can be scanned from the ESP Provisioning phone apps to start the Wi-Fi provisioning process.
The monitor log should display something like this :
```
I (1640) app_prov: SoftAP Provisioning started with SSID 'PROV_EA69FC', Password 'PROV_PASS'
I (1640) app: Scan this QR code from the provisioning application for Provisioning.
I (1650) QRCODE: Encoding below text with ECC LVL 0 & QR Code Version 10
I (1660) QRCODE: {"ver":"v1","name":"PROV_EA69FC","pop":"abcd1234","transport":"ble"}
█▀▀▀▀▀█ ▄▀ ▄█ ▄█▄▄█▀▄ ██ █▀▀▀▀▀█
█ ███ █ ▀███▀ ██▀▀█ ▀█▀ █ ███ █
█ ▀▀▀ █ █▄ ▄ ▀███ ▄▄█▀ ▄ █ ▀▀▀ █
▀▀▀▀▀▀▀ █▄▀ █ ▀ ▀▄█▄▀▄▀▄█ ▀▀▀▀▀▀▀
▀▀▄▀▄▄▀█ █▄ ▀▄▀█▄█ ▄▄ ██▄█▀▀▄▀▀▄
███ █▀▀ ▄▀▀ ▄█▄ ▀▀█▄█▀▄▄ ▄█ █
▄▀▀ ▀▀▀█▀▄▄▄█▀▀ ██▄█▄▄█▄▀█ ▄▄ ▀█
▄█▄█ ▀▀▄█ ██ ▄█ ██▀█▀ ▄█ █▄
▄█▀█▀ █▄▀▀▄ █▀█▀██ ▄█▀ ▀▀▄ ▀
█▄▄█▄▀█▄▄▀▄▄▀█ ▀▄ ▄▀██ ▄ █▄▄▄ ▀█
▀▄▀▄▀▀▀█ ▄ ▄▀▀▀█▄▀▀▀▀▀▄█ ▄▄ █ ▄▄
▀█▄▀██▀▄▄ ▄▄▀▄ ▄▀▀▀▀█▄▀▄▀█ ▄▄ ▀▀
▀ ▀ ▀ ▀▀█▄▀ ▀▀ ▀▀▀▄▀██ █▀▀▀█▀ ▄▄
█▀▀▀▀▀█ ▀▀██▀█▀ ▀█ ▄ █▀▀█ ▀ ██ ▀▄
█ ███ █ ▄█▀▄▄▄ █▀▀▀ ██ ▀████▀ ▄█
█ ▀▀▀ █ ▄▀▄▄ ▄▀█▀▄▄▄█ ▀ ▄▀█▀▀▀
▀▀▀▀▀▀▀ ▀ ▀▀▀▀ ▀ ▀ ▀ ▀ ▀▀ ▀
I (1870) app: If QR code is not visible, copy paste the below URL in a browser.
https://espressif.github.io/esp-jumpstart/qrcode.html?data={"ver":"v1","name":"PROV_EA69FC","pop":"abcd1234","transport":"ble"}
```
## Troubleshooting
### Provisioning failed

Wyświetl plik

@ -37,4 +37,9 @@ menu "Example Configuration"
help
Set the maximum connection attempts to perform when connecting to a Wi-Fi AP.
config EXAMPLE_PROV_SHOW_QR
bool "Show provisioning QR code"
default y
help
Show the QR code for provisioning.
endmenu

Wyświetl plik

@ -20,8 +20,12 @@
#include <lwip/sys.h>
#include "app_prov.h"
#include "qrcode.h"
#define EXAMPLE_AP_RECONN_ATTEMPTS CONFIG_EXAMPLE_AP_RECONN_ATTEMPTS
#define PROV_QR_VERSION "v1"
#define PROV_TRANSPORT_BLE "ble"
#define QRCODE_BASE_URL "https://espressif.github.io/esp-jumpstart/qrcode.html"
static const char *TAG = "app";
@ -109,6 +113,41 @@ static void start_ble_provisioning(void)
ESP_ERROR_CHECK(app_prov_start_ble_provisioning(security, pop));
}
static void get_device_service_name(char *service_name, size_t max)
{
uint8_t eth_mac[6];
const char *ssid_prefix = "PROV_";
esp_wifi_get_mac(WIFI_IF_STA, eth_mac);
snprintf(service_name, max, "%s%02X%02X%02X",
ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]);
}
static void ble_prov_print_qr(void)
{
char payload[150] = {0};
char name[12] = {0};
char *pop = NULL;
#ifdef CONFIG_EXAMPLE_USE_POP
pop = CONFIG_EXAMPLE_POP;
#endif
get_device_service_name(name, sizeof(name));
if (pop) {
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"pop\":\"%s\",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, pop, PROV_TRANSPORT_BLE);
} else {
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, PROV_TRANSPORT_BLE);
}
#ifdef CONFIG_EXAMPLE_PROV_SHOW_QR
ESP_LOGI(TAG, "Scan this QR code from the provisioning application for Provisioning.");
esp_qrcode_config_t cfg = ESP_QRCODE_CONFIG_DEFAULT();
esp_qrcode_generate(&cfg, payload);
#endif /* CONFIG_APP_WIFI_PROV_SHOW_QR */
ESP_LOGI(TAG, "If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s", QRCODE_BASE_URL, payload);
}
void app_main(void)
{
/* Initialize networking stack */
@ -137,6 +176,7 @@ void app_main(void)
/* If not provisioned, start provisioning via BLE */
ESP_LOGI(TAG, "Starting BLE provisioning");
start_ble_provisioning();
ble_prov_print_qr();
} else {
/* Else start as station with credentials set during provisioning */
ESP_LOGI(TAG, "Starting WiFi station");

Wyświetl plik

@ -1,6 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/qrcode)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(softap_prov)

Wyświetl plik

@ -4,5 +4,5 @@
#
PROJECT_NAME := softap_prov
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/qrcode
include $(IDF_PATH)/make/project.mk

Wyświetl plik

@ -118,6 +118,41 @@ I (668732) app_prov: Provisioning stopped
I (668742) app: SoftAP stopped
```
## QR Code Scanning
Enabling `CONFIG_EXAMPLE_PROV_SHOW_QR` will display a QR code on the serial terminal, which can be scanned from the ESP Provisioning phone apps to start the Wi-Fi provisioning process.
The monitor log should display something like this :
```
I (1640) app_prov: SoftAP Provisioning started with SSID 'PROV_EA69FC', Password 'PROV_PASS'
I (1640) app: Scan this QR code from the provisioning application for Provisioning.
I (1650) QRCODE: Encoding below text with ECC LVL 0 & QR Code Version 10
I (1660) QRCODE: {"ver":"v1","name":"PROV_EA69FC","pop":"abcd1234","transport":"softap"}
█▀▀▀▀▀█ ▄▀ ▄█ ▄█▄▄█▀▄ ██ █▀▀▀▀▀█
█ ███ █ ▀███▀ ██▀▀█ ▀█▀ █ ███ █
█ ▀▀▀ █ █▄ ▄ ▀███ ▄▄█▀ ▄ █ ▀▀▀ █
▀▀▀▀▀▀▀ █▄▀ █ ▀ ▀▄█▄▀▄▀▄█ ▀▀▀▀▀▀▀
▀▀▄▀▄▄▀█ █▄ ▀▄▀█▄█ ▄▄ ██▄█▀▀▄▀▀▄
███ █▀▀ ▄▀▀ ▄█▄ ▀▀█▄█▀▄▄ ▄█ █
▄▀▀ ▀▀▀█▀▄▄▄█▀▀ ██▄█▄▄█▄▀█ ▄▄ ▀█
▄█▄█ ▀▀▄█ ██ ▄█ ██▀█▀ ▄█ █▄
▄█▀█▀ █▄▀▀▄ █▀█▀██ ▄█▀ ▀▀▄ ▀
█▄▄█▄▀█▄▄▀▄▄▀█ ▀▄ ▄▀██ ▄ █▄▄▄ ▀█
▀▄▀▄▀▀▀█ ▄ ▄▀▀▀█▄▀▀▀▀▀▄█ ▄▄ █ ▄▄
▀█▄▀██▀▄▄ ▄▄▀▄ ▄▀▀▀▀█▄▀▄▀█ ▄▄ ▀▀
▀ ▀ ▀ ▀▀█▄▀ ▀▀ ▀▀▀▄▀██ █▀▀▀█▀ ▄▄
█▀▀▀▀▀█ ▀▀██▀█▀ ▀█ ▄ █▀▀█ ▀ ██ ▀▄
█ ███ █ ▄█▀▄▄▄ █▀▀▀ ██ ▀████▀ ▄█
█ ▀▀▀ █ ▄▀▄▄ ▄▀█▀▄▄▄█ ▀ ▄▀█▀▀▀
▀▀▀▀▀▀▀ ▀ ▀▀▀▀ ▀ ▀ ▀ ▀ ▀▀ ▀
I (1870) app: If QR code is not visible, copy paste the below URL in a browser.
https://espressif.github.io/esp-jumpstart/qrcode.html?data={"ver":"v1","name":"PROV_EA69FC","pop":"abcd1234","transport":"softap"}
```
## Troubleshooting
### Provisioning failed

Wyświetl plik

@ -56,4 +56,9 @@ menu "Example Configuration"
help
Set the maximum connection attempts to perform when connecting to a Wi-Fi AP.
config EXAMPLE_PROV_SHOW_QR
bool "Show provisioning QR code"
default y
help
Show the QR code for provisioning.
endmenu

Wyświetl plik

@ -20,8 +20,12 @@
#include <lwip/sys.h>
#include "app_prov.h"
#include "qrcode.h"
#define EXAMPLE_AP_RECONN_ATTEMPTS CONFIG_EXAMPLE_AP_RECONN_ATTEMPTS
#define PROV_QR_VERSION "v1"
#define PROV_TRANSPORT_SOFTAP "softap"
#define QRCODE_BASE_URL "https://espressif.github.io/esp-jumpstart/qrcode.html"
static const char *TAG = "app";
@ -96,6 +100,41 @@ static void start_softap_provisioning(void)
ssid, CONFIG_EXAMPLE_PASS, security, pop));
}
static void get_device_service_name(char *service_name, size_t max)
{
uint8_t eth_mac[6];
const char *ssid_prefix = "PROV_";
esp_wifi_get_mac(WIFI_IF_STA, eth_mac);
snprintf(service_name, max, "%s%02X%02X%02X",
ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]);
}
static void softap_prov_print_qr(void)
{
char payload[150] = {0};
char name[12] = {0};
char *pop = NULL;
#ifdef CONFIG_EXAMPLE_USE_POP
pop = CONFIG_EXAMPLE_POP;
#endif
get_device_service_name(name, sizeof(name));
if (pop) {
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"pop\":\"%s\",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, pop, PROV_TRANSPORT_SOFTAP);
} else {
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, PROV_TRANSPORT_SOFTAP);
}
#ifdef CONFIG_EXAMPLE_PROV_SHOW_QR
ESP_LOGI(TAG, "Scan this QR code from the provisioning application for Provisioning.");
esp_qrcode_config_t cfg = ESP_QRCODE_CONFIG_DEFAULT();
esp_qrcode_generate(&cfg, payload);
#endif /* CONFIG_APP_WIFI_PROV_SHOW_QR */
ESP_LOGI(TAG, "If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s", QRCODE_BASE_URL, payload);
}
void app_main(void)
{
/* Initialize networking stack */
@ -125,6 +164,7 @@ void app_main(void)
/* If not provisioned, start provisioning via soft AP */
ESP_LOGI(TAG, "Starting WiFi SoftAP provisioning");
start_softap_provisioning();
softap_prov_print_qr();
} else {
/* Start WiFi station with credentials set during provisioning */
ESP_LOGI(TAG, "Starting WiFi station");

Wyświetl plik

@ -1,6 +1,6 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/qrcode)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(wifi_prov_mgr)

Wyświetl plik

@ -4,5 +4,5 @@
#
PROJECT_NAME := wifi_prov_mgr
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/qrcode
include $(IDF_PATH)/make/project.mk

Wyświetl plik

@ -112,6 +112,42 @@ I (54355) app: Hello World!
I (55355) app: Hello World!
```
### QR Code Scanning
Enabling `CONFIG_EXAMPLE_PROV_SHOW_QR` will display a QR code on the serial terminal, which can be scanned from the ESP Provisioning phone apps to start the Wi-Fi provisioning process.
The monitor log should display something like this :
```
I (1462) app: Provisioning started
I (1472) app: Scan this QR code from the provisioning application for Provisioning.
I (1472) QRCODE: Encoding below text with ECC LVL 0 & QR Code Version 10
I (1482) QRCODE: {"ver":"v1","name":"PROV_EA69FC","pop":"abcd1234","transport":"ble"}
GAP procedure initiated: advertise; disc_mode=2 adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=256 adv_itvl_max=256
█▀▀▀▀▀█ ▀▀▀█▄█ ▀▀▄ █▄ ▀ █▀▀▀▀▀█
█ ███ █ ▀▄█ █▄ ▀▄█ ▄██ █ █ ███ █
█ ▀▀▀ █ ▄▀█▀▄▀ ▀█▄▀ ██ █ ▀▀▀ █
▀▀▀▀▀▀▀ █▄▀ █▄█▄█ ▀ █ █ ▀ ▀▀▀▀▀▀▀
▀▀ ▀▀ ▀ ▀▄ ▀▄ ▄▀▀▀█ ▀▄ ▀ ▀▄▄ ▄▄▀
███▄█▄▀ █▀ ▀▀▀▀▄▄█ █▀ █ ▄█▄█▀
▀███▀ ▀▄▄██ ▄▄██▄ ▀▀▀▀ ▄▀█ ▀▄▄▀
▄███ ▀██▀▀ ▄ ▄█▄▀▀█▄ ▀▄▀▄▄█ ▄
▀█▀ █▄▀▀ ▀▀█▀▀ █▀▄▀▄▀ ▄█ ███▄ ██
██▀█ ▀▄█ █▄▀▄███▀▄▀█ ▀█ █▀▀ ▀▄▄▀
█▄▀▄█▀▀ ▀▄ ▀▄▄█▄▀▀█▄█▄█▀▀█ ▀▄ ▄▀
█ ▄█▄ ▀ ▄▀ █▄ ▀█▄█▄▀▀█▀█ ▄█ ▀▄▄█
▀▀▀▀ ▀ █▀█▀▀▄▄██▄█▀█ ▀██▀▀▀█▄▄▀
█▀▀▀▀▀█ ▄█▀▀▀██ ▄▀▄ █▄█ ▀ █ ▄ ▄
█ ███ █ █ ▀▄█▀▀█▀▄█▄▄ ▀██▀▀▀▀▄▄▀▀
█ ▀▀▀ █ ▄█ ▀ ▄█▀█ █▀ ▀▀███▄▀█ █▄█
▀▀▀▀▀▀▀ ▀ ▀ ▀▀ ▀ ▀▀▀▀▀▀
I (1702) app: If QR code is not visible, copy paste the below URL in a browser.
https://espressif.github.io/esp-jumpstart/qrcode.html?data={"ver":"v1","name":"PROV_EA69FC","pop":"abcd1234","transport":"ble"}
```
### Wi-Fi Scanning
Provisioning manager also supports providing real-time Wi-Fi scan results (performed on the device) during provisioning. This allows the client side applications to choose the AP for which the device Wi-Fi station is to be configured. Various information about the visible APs is available, like signal strength (RSSI) and security type, etc. Also, the manager now provides capabilities information which can be used by client applications to determine the security type and availability of specific features (like `wifi_scan`).

Wyświetl plik

@ -27,4 +27,10 @@ menu "Example Configuration"
help
This erases the NVS to reset provisioned status of the device on every reboot.
Provisioned status is determined by the Wi-Fi STA configuration, saved on the NVS.
config EXAMPLE_PROV_SHOW_QR
bool "Show provisioning QR code"
default y
help
Show the QR code for provisioning.
endmenu

Wyświetl plik

@ -28,6 +28,7 @@
#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP
#include <wifi_provisioning/scheme_softap.h>
#endif /* CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP */
#include "qrcode.h"
static const char *TAG = "app";
@ -35,6 +36,11 @@ static const char *TAG = "app";
const int WIFI_CONNECTED_EVENT = BIT0;
static EventGroupHandle_t wifi_event_group;
#define PROV_QR_VERSION "v1"
#define PROV_TRANSPORT_SOFTAP "softap"
#define PROV_TRANSPORT_BLE "ble"
#define QRCODE_BASE_URL "https://espressif.github.io/esp-jumpstart/qrcode.html"
/* Event handler for catching system events */
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
@ -120,6 +126,30 @@ esp_err_t custom_prov_data_handler(uint32_t session_id, const uint8_t *inbuf, ss
return ESP_OK;
}
static void wifi_prov_print_qr(const char *name, const char *pop, const char *transport)
{
if (!name || !transport) {
ESP_LOGW(TAG, "Cannot generate QR code payload. Data missing.");
return;
}
char payload[150] = {0};
if (pop) {
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"pop\":\"%s\",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, pop, transport);
} else {
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, transport);
}
#ifdef CONFIG_EXAMPLE_PROV_SHOW_QR
ESP_LOGI(TAG, "Scan this QR code from the provisioning application for Provisioning.");
esp_qrcode_config_t cfg = ESP_QRCODE_CONFIG_DEFAULT();
esp_qrcode_generate(&cfg, payload);
#endif /* CONFIG_APP_WIFI_PROV_SHOW_QR */
ESP_LOGI(TAG, "If QR code is not visible, copy paste the below URL in a browser.\n%s?data=%s", QRCODE_BASE_URL, payload);
}
void app_main(void)
{
/* Initialize NVS partition */
@ -264,6 +294,12 @@ void app_main(void)
* by the default event loop handler, we don't need to call the following */
// wifi_prov_mgr_wait();
// wifi_prov_mgr_deinit();
/* Print QR code for provisioning */
#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_BLE
wifi_prov_print_qr(service_name, pop, PROV_TRANSPORT_BLE);
#else /* CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP */
wifi_prov_print_qr(service_name, pop, PROV_TRANSPORT_SOFTAP);
#endif /* CONFIG_EXAMPLE_PROV_TRANSPORT_BLE */
} else {
ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA");