Provisioning Manager WiFi not working

Stomper
Posts: 16
Joined: Tue Mar 31, 2020 10:33 pm

Provisioning Manager WiFi not working

Postby Stomper » Tue Mar 31, 2020 10:49 pm

Hi,

I tried the provisioning manager example code from the examples.

I changed the sample to use wifi provisioning

Code: Select all

void app_main()
{
    /* Initialize NVS partition */
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        /* NVS partition was truncated
         * and needs to be erased */
        ESP_ERROR_CHECK(nvs_flash_erase());

        /* Retry nvs_flash_init */
        ESP_ERROR_CHECK(nvs_flash_init());
    }

    /* Initialize TCP/IP */
    tcpip_adapter_init();


    /* Initialize the event loop */
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    wifi_event_group = xEventGroupCreate();

    /* Initialize mDNS */
	esp_err_t err = mdns_init();
	if (err)
	{
		printf("MDNS Init failed: %d\n", err);
		return;
	}

	//set hostname
	mdns_hostname_set("foobarfoo");
	//set default instance
	mdns_instance_name_set("MyFooDevice");

    /* 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 */
    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 */
        .scheme = wifi_prov_scheme_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*/
        .scheme_event_handler = WIFI_PROV_EVENT_HANDLER_NONE
    };

    /* Initialize provisioning manager with the
     * configuration parameters set above */
    ESP_ERROR_CHECK(wifi_prov_mgr_init(config));

    bool provisioned = false;
    /* Let's find out if the device is provisioned */
    ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned));

    /* If device is not yet provisioned start provisioning service */
    if (!provisioned) {
        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_0;

        /* 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
         *     - simply ignored when scheme is wifi_prov_scheme_ble
         */
        const char *service_key = NULL;

        /* 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 */
            0x21, 0x43, 0x65, 0x87, 0x09, 0xba, 0xdc, 0xfe,
            0xef, 0xcd, 0xab, 0x90, 0x78, 0x56, 0x34, 0x12
        };
        wifi_prov_scheme_ble_set_service_uuid(custom_service_uuid);

        /* Start provisioning service */
        ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key));

        /* 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();
    } else {
        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();

        /* Start Wi-Fi station */
        wifi_init_sta();
    }

    /* Wait for Wi-Fi connection */
    xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, false, true, portMAX_DELAY);

    /* Start main application now */
    while (1) {
        ESP_LOGI(TAG, "Hello World!");
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
After that I created an html page to post on the URI "http://foobarfoo/proto-ver".

Code: Select all

<html lang="en">

<head>
<title>Prov Test</title>
</head>

<form action="http://foobarfoo/proto-ver" method="post">

  <input type="submit" value="Submit">
</form>

</html>
And I expected to get a string back. But all I get is ERR_EMPTY_RESPONSE in my browser and the debug console of the esp32 says:

Code: Select all

.
.
.
I (730) app: Starting provisioning
I (820) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 0
I (820) wifi: mode : sta (a4:cf:12:1e:44:14)
I (830) wifi: mode : sta (a4:cf:12:1e:44:14) + softAP (a4:cf:12:1e:44:15)
I (830) wifi: Total power save buffer number: 16
I (830) wifi: Init max length of beacon: 752/752
I (840) wifi: Init max length of beacon: 752/752
I (840) wifi: Total power save buffer number: 16
I (860) wifi_prov_mgr: Provisioning started with service name : PROV_1E4414 
I (860) app: Provisioning started
I (11850) wifi: new:<1,1>, old:<1,0>, ap:<1,1>, sta:<0,0>, prof:1
I (11850) wifi: station: 40:a3:cc:a0:c3:0a join, AID=1, bgn, 40U
I (12040) tcpip_adapter: softAP assign IP to station,IP is: 192.168.4.2
E (22400) protocomm_httpd: Content length not found
W (22400) httpd_uri: httpd_uri: uri handler execution failed
What am I doing wrong?
Where can I find an api reference how to use the provisioning manager?

Best regards
Stomper

ESP_Piyush
Posts: 309
Joined: Wed Feb 20, 2019 7:02 am

Re: Provisioning Manager WiFi not working

Postby ESP_Piyush » Mon Apr 06, 2020 10:52 am

Hello, there is a specific format in which you need to post data on the provisioning handlers. We use protobuf for data serialisation. Please check here: https://docs.espressif.com/projects/esp ... oning.html.

Sample implementations of the client can be found in our iOS app (https://github.com/espressif/esp-idf-provisioning-ios) and Android app (https://github.com/espressif/esp-idf-pr ... ng-android). A python based implementation is available in esp-idf (https://github.com/espressif/esp-idf/tr ... s/esp_prov)

Stomper
Posts: 16
Joined: Tue Mar 31, 2020 10:33 pm

Re: Provisioning Manager WiFi not working

Postby Stomper » Tue Apr 14, 2020 6:57 pm

Ah... ok got it. :mrgreen:

Thanks for your answer!

ahmedwahdan
Posts: 9
Joined: Mon Jan 04, 2021 4:32 pm

Re: Provisioning Manager WiFi not working

Postby ahmedwahdan » Mon Jan 04, 2021 4:34 pm

Have you managed to complete this task using html and JS?

Who is online

Users browsing this forum: Google [Bot] and 92 guests