I want to restart the wifi provisioning manager when ESP station can not connect to AP provided during provisioning.
But for that ESP doesn't assign IP to device when I start reprovisioning.
I have made changes in the function with bool variable as below. changes from example highlighted in bold. Also attached logs here.
Code: Select all
esp_err_t softap_provisioning_init(bool is_reprvosioning)
{
esp_err_t ret = 0;
const esp_timer_create_args_t oneshot_timer_soft_ap_args = {
.callback = &soft_ap_expiry_cb,
.name = "one-shot-on"
};
ret = esp_timer_create(&oneshot_timer_soft_ap_args, &soft_ap);
if(ret != ESP_OK)
{
ESP_LOGE(TAG, "Error in create soft ap timer");
return ret;
}
[b] if (!is_reprvosioning)[/b]
{
/* Initialize TCP/IP */
ESP_ERROR_CHECK(esp_netif_init());
/* Initialize the event loop */
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_event_group = xEventGroupCreate();
/* Register our event handler for Wi-Fi, IP and Provisioning related events */
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
/* Initialize Wi-Fi including netif with default config */
esp_netif_create_default_wifi_sta();
#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP
esp_netif_create_default_wifi_ap();
#endif /* CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP */
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
}
/* Configuration for the provisioning manager */
wifi_prov_mgr_config_t config = {
/* What is the Provisioning Scheme that we want ?
* wifi_prov_scheme_softap or wifi_prov_scheme_ble */
#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_BLE
.scheme = wifi_prov_scheme_ble,
#endif /* CONFIG_EXAMPLE_PROV_TRANSPORT_BLE */
#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP
.scheme = wifi_prov_scheme_softap,
#endif /* CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP */
/* Any default scheme specific event handler that you would
* like to choose. Since our example application requires
* neither BT nor BLE, we can choose to release the associated
* memory once provisioning is complete, or not needed
* (in case when device is already provisioned). Choosing
* appropriate scheme specific event handler allows the manager
* to take care of this automatically. This can be set to
* WIFI_PROV_EVENT_HANDLER_NONE when using wifi_prov_scheme_softap*/
#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_BLE
.scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM
#endif /* CONFIG_EXAMPLE_PROV_TRANSPORT_BLE */
#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP
.scheme_event_handler = WIFI_PROV_EVENT_HANDLER_NONE
#endif /* CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP */
};
/* Initialize provisioning manager with the
* configuration parameters set above */
ESP_ERROR_CHECK(wifi_prov_mgr_init(config));
bool provisioned = false;
#ifdef CONFIG_EXAMPLE_RESET_PROVISIONED
wifi_prov_mgr_reset_provisioning();
#else
/* Let's find out if the device is provisioned */
ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned));
printf("provisioned = %d",provisioned);
wifi_config_t wifi_cred_old;
esp_wifi_get_config(WIFI_IF_STA, &wifi_cred_old);
#endif
/* If device is not yet provisioned start provisioning service */
ESP_LOGI(TAG, "Starting provisioning");
/* What is the Device Service Name that we want
* This translates to :
* - Wi-Fi SSID when scheme is wifi_prov_scheme_softap
* - device name when scheme is wifi_prov_scheme_ble
*/
char service_name[12];
get_device_service_name(service_name, sizeof(service_name));
/* What is the security level that we want (0 or 1):
* - WIFI_PROV_SECURITY_0 is simply plain text communication.
* - WIFI_PROV_SECURITY_1 is secure communication which consists of secure handshake
* using X25519 key exchange and proof of possession (pop) and AES-CTR
* for encryption/decryption of messages.
*/
wifi_prov_security_t security = WIFI_PROV_SECURITY_1;
/* Do we want a proof-of-possession (ignored if Security 0 is selected):
* - this should be a string with length > 0
* - NULL if not used
*/
const char *pop = "abcd1234";
/* What is the service key (could be NULL)
* This translates to :
* - Wi-Fi password when scheme is wifi_prov_scheme_softap
* (Minimum expected length: 8, maximum 64 for WPA2-PSK)
* - simply ignored when scheme is wifi_prov_scheme_ble
*/
const char *service_key = NULL;
#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_BLE
/* This step is only useful when scheme is wifi_prov_scheme_ble. This will
* set a custom 128 bit UUID which will be included in the BLE advertisement
* and will correspond to the primary GATT service that provides provisioning
* endpoints as GATT characteristics. Each GATT characteristic will be
* formed using the primary service UUID as base, with different auto assigned
* 12th and 13th bytes (assume counting starts from 0th byte). The client side
* applications must identify the endpoints by reading the User Characteristic
* Description descriptor (0x2901) for each characteristic, which contains the
* endpoint name of the characteristic */
uint8_t custom_service_uuid[] = {
/* LSB <---------------------------------------
* ---------------------------------------> MSB */
0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02,
};
/* If your build fails with linker errors at this point, then you may have
* forgotten to enable the BT stack or BTDM BLE settings in the SDK (e.g. see
* the sdkconfig.defaults in the example project) */
wifi_prov_scheme_ble_set_service_uuid(custom_service_uuid);
#endif /* CONFIG_EXAMPLE_PROV_TRANSPORT_BLE */
/* An endpoint that applications can create if they expect to
* get bugsy time configuration during provisioning workflow.
* This call must be made before starting the provisioning.
*/
[b]if (!is_reprvosioning)[/b]
{
wifi_prov_mgr_endpoint_create("proto-time");
}
/* Start provisioning service */
ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key));
/* The handler for the bugsy time configuration endpoint created above.
* This call must be made after starting the provisioning, and only if the endpoint
* has already been created above.
*/
wifi_prov_mgr_endpoint_register("proto-time", get_time_handler, NULL);
/* Uncomment the following to wait for the provisioning to finish and then release
* the resources of the manager. Since in this case de-initialization is triggered
* 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 */
is_connected_to_mobile = false;
esp_timer_start_once(soft_ap, PROVISIONING_MANAGER_WAIT_TIME_IN_SEC * 1000000);
printf("Soft AP timer starts\n");
soft_ap_flag = true;
if (!is_reprvosioning)
dhcps_set_new_lease_cb(soft_ap_dhcps_cb);
while(soft_ap_flag)
{
sys_delay_ms(1000);
}
printf("provisioned = %d\n",provisioned);
if(provisioned && !is_connected_to_mobile){
ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA");
/* We don't need the manager as device is already provisioned,
* so let's release it's resources */
wifi_prov_mgr_deinit();
if (esp_wifi_set_storage(WIFI_STORAGE_FLASH) != ESP_OK) {
ESP_LOGE(TAG, "Failed to set storage Wi-Fi");
return ESP_FAIL;
}
ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_cred_old);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to set empty Wi-Fi credentials");
}
ret = esp_wifi_get_config(WIFI_IF_STA, &wifi_cred_old);
printf("ssid = %s , pwd = %s\n",wifi_cred_old.sta.ssid, wifi_cred_old.sta.password);
/* Start Wi-Fi station */
wifi_init_sta();
esp_wifi_connect();
}
/* Wait for Wi-Fi connection */
xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, false, true, portMAX_DELAY);
return ESP_OK;
}
Logs below.
I (43859) wifi:new:<1,0>, old:<1,1>, ap:<1,1>, sta:<0,0>, prof:1
I (43859) wifi:station: c2:df:4a:7e:5c:f1 join, AID=1, bgn, 20
W (43999) wifi:<ba-add>idx:2 (ifx:1, c2:df:4a:7e:5c:f1), tid:0, ssn:0, winSize:64
I (61979) wifi:station: c2:df:4a:7e:5c:f1 leave, AID = 1, bss_flags is 134243, bss:0x3fca8398
I (61979) wifi:new:<1,0>, old:<1,0>, ap:<1,1>, sta:<0,0>, prof:1
W (61989) wifi:<ba-del>idx
I (66069) wifi:new:<1,0>, old:<1,0>, ap:<1,1>, sta:<0,0>, prof:1
I (66069) wifi:station: c2:df:4a:7e:5c:f1 join, AID=1, bgn, 20
W (66169) wifi:<ba-add>idx:2 (ifx:1, c2:df:4a:7e:5c:f1), tid:0, ssn:0, winSize:64
Can anyone please help with this ?