reworked STA reconnect mechanism

This commit is contained in:
Bogdan Pilyugin 2023-02-04 09:43:19 +02:00
parent 080abfd79d
commit e7abcf7ece

View File

@ -47,7 +47,6 @@ static EventGroupHandle_t s_wifi_event_group;
#define WIFI_CONNECTED_BIT BIT0 #define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1 #define WIFI_FAIL_BIT BIT1
static int s_retry_num = 0;
static bool isWiFiGotIp = false; static bool isWiFiGotIp = false;
#define DEFAULT_SCAN_LIST_SIZE 20 #define DEFAULT_SCAN_LIST_SIZE 20
@ -97,27 +96,17 @@ static void event_handler(void *arg, esp_event_base_t event_base,
ESP_LOGI(TAG, "Connected to AP"); ESP_LOGI(TAG, "Connected to AP");
} }
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY)
{
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
else
{ {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
isWiFiGotIp = false; isWiFiGotIp = false;
ESP_LOGI(TAG, "connect to the AP fail"); ESP_LOGI(TAG, "connect to the AP fail");
} }
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{ {
ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data; ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data;
const esp_netif_ip_info_t *ip_info = &event->ip_info; const esp_netif_ip_info_t *ip_info = &event->ip_info;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
memcpy(&GetSysConf()->wifiSettings.InfIPAddr, &event->ip_info.ip, sizeof(event->ip_info.ip)); memcpy(&GetSysConf()->wifiSettings.InfIPAddr, &event->ip_info.ip, sizeof(event->ip_info.ip));
memcpy(&GetSysConf()->wifiSettings.InfMask, &event->ip_info.netmask, sizeof(event->ip_info.netmask)); memcpy(&GetSysConf()->wifiSettings.InfMask, &event->ip_info.netmask, sizeof(event->ip_info.netmask));
memcpy(&GetSysConf()->wifiSettings.InfGateway, &event->ip_info.gw, sizeof(event->ip_info.gw)); memcpy(&GetSysConf()->wifiSettings.InfGateway, &event->ip_info.gw, sizeof(event->ip_info.gw));
@ -145,11 +134,11 @@ static void event_handler(void *arg, esp_event_base_t event_base,
} }
} }
static void wifi_init_softap(void *pvParameter)
static void wifi_init_softap(void *pvParameter) {
{
char if_key_str[24]; char if_key_str[24];
esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP(); esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP()
;
strcpy(if_key_str, "WIFI_AP_USER"); strcpy(if_key_str, "WIFI_AP_USER");
esp_netif_conf.if_key = if_key_str; esp_netif_conf.if_key = if_key_str;
@ -213,12 +202,11 @@ static void event_handler(void *arg, esp_event_base_t event_base,
ESP_LOGI(TAG, "wifi_init_softap finished"); ESP_LOGI(TAG, "wifi_init_softap finished");
vTaskDelete(NULL); vTaskDelete(NULL);
} }
static void wifi_init_sta(void *pvParameter) static void wifi_init_sta(void *pvParameter)
{ {
s_wifi_event_group = xEventGroupCreate(); //s_wifi_event_group = xEventGroupCreate();
//sta_netif = esp_netif_create_default_wifi_sta(); //sta_netif = esp_netif_create_default_wifi_sta();
char if_key_str[24]; char if_key_str[24];
esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA(); esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
@ -307,7 +295,7 @@ static void wifi_init_sta(void *pvParameter)
static void wifi_init_apsta(void *pvParameter) static void wifi_init_apsta(void *pvParameter)
{ {
s_wifi_event_group = xEventGroupCreate(); //s_wifi_event_group = xEventGroupCreate();
//BEGIN AP MODE IF //BEGIN AP MODE IF
char ap_if_key_str[24]; char ap_if_key_str[24];
esp_netif_inherent_config_t ap_esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP() esp_netif_inherent_config_t ap_esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP()
@ -454,7 +442,6 @@ static void wifi_init_apsta(void *pvParameter)
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
vTaskDelete(NULL); vTaskDelete(NULL);
} }
@ -468,9 +455,14 @@ void WiFiConnect(void)
esp_wifi_connect(); esp_wifi_connect();
} }
static void WiFiControlTask(void* arg) #define BASE_RECONNECT_INTERVAL 6
static void WiFiControlTask(void *arg)
{ {
//WiFi init and start block //WiFi init and start block
static int reconnect_interval = BASE_RECONNECT_INTERVAL;
static int reconnect_counter = BASE_RECONNECT_INTERVAL;
s_wifi_event_group = xEventGroupCreate();
switch (GetSysConf()->wifiSettings.WiFiMode) switch (GetSysConf()->wifiSettings.WiFiMode)
{ {
case WIFI_MODE_STA: case WIFI_MODE_STA:
@ -484,8 +476,9 @@ static void WiFiControlTask(void* arg)
break; break;
} }
//WiFi in work service //WiFi in work service
while(1) while (1)
{ {
vTaskDelay(pdMS_TO_TICKS(1000));
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE, pdFALSE,
@ -493,20 +486,21 @@ static void WiFiControlTask(void* arg)
portMAX_DELAY); portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT) if (bits & WIFI_CONNECTED_BIT)
{ {
reconnect_interval = BASE_RECONNECT_INTERVAL;
reconnect_counter = BASE_RECONNECT_INTERVAL;
} }
else if (bits & WIFI_FAIL_BIT) else if (bits & WIFI_FAIL_BIT)
{ {
if (--reconnect_counter <= 0)
}
else
{ {
esp_wifi_connect();
reconnect_interval *= 1.5;
if (reconnect_interval >= BASE_RECONNECT_INTERVAL * 10)
reconnect_interval = BASE_RECONNECT_INTERVAL * 10;
reconnect_counter = reconnect_interval;
}
} }
vTaskDelay(pdMS_TO_TICKS(1000));
} }
} }