Page 1 of 1

smartconfig and standard wifi connection in one handler

Posted: Mon Sep 04, 2023 3:28 pm
by red_Panda
Please advise how to relaize esp-touch(smartconfig) and the ability to use esp_wifi_set_storage
In one event handler, the logic is as follows...
there is an entry in storage, connect to wifi
no entry, run smartconfig
I couldn't find "wifi authorization error" event and I can run one or the other only by WIFI_EVENT_STA_START event, because I need WIFI_EVENT_STA_DISCONNECTED event for re-authorization in case of wifi loss.

Code: Select all

static void event_handler(void* arg, esp_event_base_t event_base,
                                int32_t event_id, void* event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
        xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
        esp_wifi_connect();
        xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
    } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
        xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
    } else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) {
        ESP_LOGI(TAG, "Scan done");
    } else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) {
        ESP_LOGI(TAG, "Found channel");
    } else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) {
        ESP_LOGI(TAG, "Got SSID and password");

        smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data;
        wifi_config_t wifi_config;
        uint8_t ssid[33] = { 0 };
        uint8_t password[65] = { 0 };
        uint8_t rvd_data[33] = { 0 };

        // wifi_config.sta.failure_retry_cnt = 5; TODO
        bzero(&wifi_config, sizeof(wifi_config_t));
        memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
        memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
        wifi_config.sta.bssid_set = evt->bssid_set;
        if (wifi_config.sta.bssid_set == true) {
            memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
        }

        memcpy(ssid, evt->ssid, sizeof(evt->ssid));
        memcpy(password, evt->password, sizeof(evt->password));
        ESP_LOGI(TAG, "SSID:%s", ssid);
        ESP_LOGI(TAG, "PASSWORD:%s", password);
        if (evt->type == SC_TYPE_ESPTOUCH_V2) {
            ESP_ERROR_CHECK( esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)) );
            ESP_LOGI(TAG, "RVD_DATA:");
            for (int i=0; i<33; i++) {
                printf("%02x ", rvd_data[i]);
            }
            printf("\n");
        }

        ESP_ERROR_CHECK( esp_wifi_disconnect() );
        ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
        esp_wifi_connect();
    } else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) {
        xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT);
    }
}

Re: smartconfig and standard wifi connection in one handler

Posted: Tue Sep 05, 2023 12:19 pm
by red_Panda
@ESP_flying_raijin I don't know how to get an answer to my question, but I see you are one of the developers and so I really ask you to help. I'm stuck.
1 I tested the ESP Touch example on iPhone and it works great, but the Android app is not available for my Andriod OS version 11. This is not a problem or a question but an annoying fact, I really hope Google didn't change something in the OS's policy and this package will work https://pub.dev/packages/esptouch_flutter
2 In the implementation of the callback function for wifi events all events are handled very logically and I don't want to break that logic but the WIFI_EVENT_STA_START event should call both smartconfig in one situation and esp_wifi_connect in another, but there is also a WIFI_EVENT_STA_DISCONNECTED event that can occur when the router is disconnected, for example, or another, for example, when the ssid and password are already contained in nvs and it would be logical to connect to the network, but the user has changed the wifi settings on the router.
I apologize very much and I understand that this is not an algorithmic lesson, but maybe there is a well-established solution that assumes trouble-free use in different scenarios?
I apologize for distracting you, but I have no idea where to get an answer, I've been reading everything I can find on the Internet for 24 hours, I even connected Bard to my problem :) , but he confused me more than helped me.
Or maybe ignore the comfort of using smartconfig and do it the old-fashioned way through captive portal or just send the wifi settings from the application by connecting to the AP device? I'm confused :cry: