Таки добил я подключение к WiFi, чтобы приложение не рушилось при каждом чихе. Получилось монструозно, но я пока еще только постигаю азы. Возможно, можно будет сделать проще или короче. Но пока, карточка пингуется. Правда, триггерится watchdog, но вроде сеанс не крашится. Буду разбираться. Следующая задача — подконнектиться к сайту в инете… В общем, привожу код функции:
int wf_init_station(void){
int stage=WF_INIT_STAGE_1;
s_wifi_event_group = xEventGroupCreate();
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_WIFI_STA();
wifi_init_config_t wf_cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
esp_netif_t *netif;
static const char *TAG = "WiFi initialization procedure: ";
EventBits_t bits;
esp_err_t ret_code=ESP_OK;
wifi_config_t wifi_config = {
.sta = {
.ssid = "My_SSID",
.password = "My_Pass",
/* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8).
* If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
* to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
* WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
*/
//.threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD,
//.sae_pwe_h2e = ESP_WIFI_SAE_MODE,
//.sae_h2e_identifier = EXAMPLE_H2E_IDENTIFIER,
},
};
while(stage!=WF_INIT_OK || stage!=WF_INIT_FAIL){
switch (stage){
case WF_INIT_STAGE_1 : { //Инициализация переменных
stage++;
}; break;
case WF_INIT_STAGE_2 : { //Инициализация сетевого интерфейса
ret_code=esp_netif_init();
if(ret_code==ESP_OK){
stage++;
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
stage=WF_INIT_FAIL;
};
}; break;
case WF_INIT_STAGE_3 : { // Создание очереди событий
ret_code=esp_event_loop_create_default();
if(ret_code==ESP_OK) {
stage++;
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
stage=WF_INIT_FAIL;
};
}; break;
case WF_INIT_STAGE_4 : {
netif = esp_netif_new(&cfg);
if(netif){
stage++;
} else {
ESP_LOGI(TAG,"STAGE 4: esp_netif_new(&cfg) failed");
stage=WF_INIT_FAIL;
}
}; break ;
case WF_INIT_STAGE_5 : {
ret_code=esp_netif_attach_wifi_station(netif);
if(ret_code==ESP_OK){
stage++;
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
stage=WF_INIT_FAIL;
}
}; break;
case WF_INIT_STAGE_6 : {
ret_code=esp_wifi_set_default_wifi_sta_handlers();
if(ret_code==ESP_OK){
stage++;
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
stage=WF_INIT_FAIL;
}
}; break;
case WF_INIT_STAGE_7 : {
ret_code=nvs_flash_init();
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
ret_code=esp_wifi_init(&wf_cfg);
if(ret_code==ESP_OK){
stage++;
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
stage=WF_INIT_FAIL;
}
}; break;
case WF_INIT_STAGE_8 : {
ret_code=esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id);
if(ret_code==ESP_OK){
stage++;
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
stage=WF_INIT_FAIL;
}
}; break;
case WF_INIT_STAGE_9 : {
ret_code=esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip);
if(ret_code==ESP_OK){
stage++;
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
stage=WF_INIT_FAIL;
}
}; break;
case WF_INIT_STAGE_10 : {
esp_wifi_set_mode(WIFI_MODE_STA);
if(ret_code==ESP_OK){
stage++;
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
stage=WF_INIT_FAIL;
}
}; break;
case WF_INIT_STAGE_11 : {
esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
if(ret_code==ESP_OK){
stage++;
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
stage=WF_INIT_FAIL;
}
}; break;
case WF_INIT_STAGE_12 : {
esp_wifi_start();
if(ret_code==ESP_OK){
stage++;
ESP_LOGI(TAG, "STAGE 12: wifi_init_sta finished.");
} else {
ESP_ERROR_CHECK_WITHOUT_ABORT(ret_code);
stage=WF_INIT_FAIL;
}
}; break;
case WF_INIT_STAGE_13 : {
bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "STAGE 13: connected to WiFi ");
stage=WF_INIT_OK;
ret_code=ESP_OK;
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "STAGE 13: Failed to connect to SSID");
stage=WF_INIT_FAIL;
} else {
ESP_LOGE(TAG, "STAGE 13: UNEXPECTED EVENT");
stage=WF_INIT_FAIL;
}
}; break;
default :
stage++;
}; //END OF SWITCH
}; //END OF LOOP
return stage;
}