espressif / esp-idf-provisioning-android

Android Provisioning application for ESP-IDF Unified provisioning
Apache License 2.0
199 stars 131 forks source link

Provisioning with custom-data after device establishes Wi-Fi connection #60

Open cvonk opened 2 years ago

cvonk commented 2 years ago

I am using BLE to provision an ESP32 device. It is nice that the BLE connection stays active while the device goes through its paces of connecting to e.g. Wi-Fi.

For my current application, I do not only need to provision the WiFi config, but also need to config and send and apply MQTT config. I succeeded to pass this MQTT config to an additional endpoint as described here. I do this before the Wi-Fi config is passed or applied.

Similar to how the WiFi configuration gets sent and applied, I would like to apply the MQTT config by having the device connect to the MQTT broker. So, I created and registered a second custom endpoint.

wifi_prov_mgr_endpoint_create("mqtt-config");
wifi_prov_mgr_endpoint_create("mqtt-status");
ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key));
wifi_prov_mgr_endpoint_register("mqtt-config", _mqtt_config_handler, NULL);
wifi_prov_mgr_endpoint_register("mqtt-status", _mqtt_status_handler, NULL);

where

esp_err_t _mqtt_status_handler(uint32_t session_id, const uint8_t *inbuf, ssize_t inlen,
                               uint8_t **outbuf, ssize_t *outlen, void *priv_data)
{
    if (inbuf) {
        ESP_LOGI(TAG, "%s Received data: %.*s", __FUNCTION__, inlen, (char *)inbuf);
    }
    char response[] = "SUCCESS";
    *outbuf = (uint8_t *)strdup(response);
    if (*outbuf == NULL) {
        ESP_LOGE(TAG, "System out of memory");
        return ESP_ERR_NO_MEM;
    }
    *outlen = strlen(response) + 1; // +1 for NULL terminating byte

    return ESP_OK;
}

However, applying the MQTT config, can only be done once the devices establishes its Wi-Fi connection. In other words until after the ApplyConfig().

The documentation says

The default behavior is that once the device successfully connects using the Wi-Fi credentials set by the apply_config command, the provisioning service will be stopped. ... If this default behavior is not desired, it can be disabled by calling wifi_prov_mgr_disable_auto_stop(). Now the provisioning service will only be stopped after an explicit call to wifi_prov_mgr_stop_provisioning().

So, on the device I added a call to wifi_prov_mgr_disable_auto_stop(). Now, the GATT connection stays active. However, my this endpoint still doesn't receive the data send from my modified android phone app. However, in the Android Studio debugger, I see that the phone app sends the information to the correct endpoint.

Can you shed some light on this issue? Thanks.

FYI a similar issue will arise if you want to wait for the device to download e.g. an OTA update. It also needs the Wi-Fi connection established, and it would be great it the phone app would be able to show the progress.

KhushbuShah25 commented 2 years ago

Hi @cvonk ,

Have you tried sending custom data (MQTT config) before Wi-Fi credential from phone app ? Can you also check return codes of your endpoint create and register calls ?

cvonk commented 2 years ago

As I recall it sends the MQTT cfg first before connecting wifi. Sorry no details as I am on the road right now.

On Mon, Jun 27, 2022, 00:01 Khushbu Shah @.***> wrote:

Hi @cvonk https://github.com/cvonk ,

Have you tried sending custom data (MQTT config) before Wi-Fi credential from phone app ? Can you also check return codes of your endpoint create and register calls ?

— Reply to this email directly, view it on GitHub https://github.com/espressif/esp-idf-provisioning-android/issues/60#issuecomment-1166957611, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACKIDGX3ZVIP2IW7ROH6FBTVRFGWDANCNFSM5SJ2VCCA . You are receiving this because you were mentioned.Message ID: @.***>