reworked STA reconnect mechanism
This commit is contained in:
parent
080abfd79d
commit
e7abcf7ece
|
|
@ -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
|
||||||
|
|
@ -98,26 +97,16 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
||||||
}
|
}
|
||||||
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)
|
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||||
{
|
|
||||||
esp_wifi_connect();
|
|
||||||
s_retry_num++;
|
|
||||||
ESP_LOGI(TAG, "retry to connect to the AP");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
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,80 +134,79 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void wifi_init_softap(void *pvParameter)
|
||||||
|
{
|
||||||
|
char if_key_str[24];
|
||||||
|
esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP()
|
||||||
|
;
|
||||||
|
|
||||||
static void wifi_init_softap(void *pvParameter)
|
strcpy(if_key_str, "WIFI_AP_USER");
|
||||||
{
|
esp_netif_conf.if_key = if_key_str;
|
||||||
char if_key_str[24];
|
esp_netif_conf.route_prio = AP_PRIO;
|
||||||
esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
|
|
||||||
|
|
||||||
strcpy(if_key_str, "WIFI_AP_USER");
|
esp_netif_config_t cfg_netif = {
|
||||||
esp_netif_conf.if_key = if_key_str;
|
.base = &esp_netif_conf,
|
||||||
esp_netif_conf.route_prio = AP_PRIO;
|
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP
|
||||||
|
};
|
||||||
|
|
||||||
esp_netif_config_t cfg_netif = {
|
ap_netif = esp_netif_new(&cfg_netif);
|
||||||
.base = &esp_netif_conf,
|
assert(ap_netif);
|
||||||
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP
|
|
||||||
};
|
|
||||||
|
|
||||||
ap_netif = esp_netif_new(&cfg_netif);
|
esp_netif_ip_info_t ip_info;
|
||||||
assert(ap_netif);
|
memcpy(&ip_info.ip, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
||||||
|
memcpy(&ip_info.gw, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
||||||
|
memcpy(&ip_info.netmask, &GetSysConf()->wifiSettings.InfMask, 4);
|
||||||
|
|
||||||
esp_netif_ip_info_t ip_info;
|
esp_netif_dns_info_t dns_info;
|
||||||
memcpy(&ip_info.ip, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
memcpy(&dns_info, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
||||||
memcpy(&ip_info.gw, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
|
||||||
memcpy(&ip_info.netmask, &GetSysConf()->wifiSettings.InfMask, 4);
|
|
||||||
|
|
||||||
esp_netif_dns_info_t dns_info;
|
esp_netif_dhcps_stop(ap_netif);
|
||||||
memcpy(&dns_info, &GetSysConf()->wifiSettings.ApIPAddr, 4);
|
esp_netif_set_ip_info(ap_netif, &ip_info);
|
||||||
|
esp_netif_set_dns_info(ap_netif, ESP_NETIF_DNS_MAIN, &dns_info);
|
||||||
|
esp_netif_dhcps_start(ap_netif);
|
||||||
|
|
||||||
esp_netif_dhcps_stop(ap_netif);
|
esp_netif_attach_wifi_ap(ap_netif);
|
||||||
esp_netif_set_ip_info(ap_netif, &ip_info);
|
esp_wifi_set_default_wifi_ap_handlers();
|
||||||
esp_netif_set_dns_info(ap_netif, ESP_NETIF_DNS_MAIN, &dns_info);
|
|
||||||
esp_netif_dhcps_start(ap_netif);
|
|
||||||
|
|
||||||
esp_netif_attach_wifi_ap(ap_netif);
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||||
esp_wifi_set_default_wifi_ap_handlers();
|
;
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||||
|
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||||
;
|
ESP_EVENT_ANY_ID,
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
&event_handler,
|
||||||
|
NULL,
|
||||||
|
NULL));
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
wifi_config_t wifi_config = {
|
||||||
ESP_EVENT_ANY_ID,
|
.ap = {
|
||||||
&event_handler,
|
|
||||||
NULL,
|
|
||||||
NULL));
|
|
||||||
|
|
||||||
wifi_config_t wifi_config = {
|
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
|
||||||
.ap = {
|
.max_connection = EXAMPLE_MAX_STA_CONN,
|
||||||
|
.authmode = WIFI_AUTH_WPA_WPA2_PSK
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if (strlen(CONFIG_WEBGUIAPP_WIFI_KEY_AP) == 0)
|
||||||
|
{
|
||||||
|
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
||||||
|
}
|
||||||
|
|
||||||
.channel = EXAMPLE_ESP_WIFI_CHANNEL,
|
memcpy(wifi_config.ap.ssid, GetSysConf()->wifiSettings.ApSSID, strlen(GetSysConf()->wifiSettings.ApSSID));
|
||||||
.max_connection = EXAMPLE_MAX_STA_CONN,
|
memcpy(wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey,
|
||||||
.authmode = WIFI_AUTH_WPA_WPA2_PSK
|
strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
||||||
},
|
wifi_config.ap.ssid_len = strlen(GetSysConf()->wifiSettings.ApSSID);
|
||||||
};
|
|
||||||
if (strlen(CONFIG_WEBGUIAPP_WIFI_KEY_AP) == 0)
|
|
||||||
{
|
|
||||||
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(wifi_config.ap.ssid, GetSysConf()->wifiSettings.ApSSID, strlen(GetSysConf()->wifiSettings.ApSSID));
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||||
memcpy(wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey,
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
|
||||||
strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||||||
wifi_config.ap.ssid_len = strlen(GetSysConf()->wifiSettings.ApSSID);
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "wifi_init_softap finished");
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "wifi_init_softap finished");
|
||||||
|
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user