added wifi power, fixed conflict with wifi startup connect and reconnect

on AP disconnected
This commit is contained in:
Bogdan Pilyugin 2023-03-28 16:17:47 +02:00
parent ebf050673c
commit 7f78b5a699
5 changed files with 57 additions and 17 deletions

View File

@ -160,7 +160,7 @@
char b6 :1;
char bIsWiFiEnabled :1;
} Flags1; // Flag structure
uint8_t MaxPower;
} wifiSettings;
#endif

View File

@ -147,6 +147,13 @@ static HTTP_IO_RESULT HTTPPostAdaptersSettings(httpd_req_t *req, char *PostData)
else if (!strcmp((const char*) tmp, (const char*) "apsta"))
GetSysConf()->wifiSettings.WiFiMode = WIFI_MODE_APSTA;
}
if (httpd_query_key_value(PostData, "wifipwr", tmp, sizeof(tmp)) == ESP_OK)
{
uint8_t pwr = atoi((const char*) tmp);
if (pwr >=8 && pwr <= 84)
GetSysConf()->wifiSettings.MaxPower = pwr * 4;
}
/*AP section*/
httpd_query_key_value(PostData, "wfiap", GetSysConf()->wifiSettings.ApSSID,
sizeof(GetSysConf()->wifiSettings.ApSSID));
@ -291,7 +298,6 @@ static HTTP_IO_RESULT HTTPPostAdaptersSettings(httpd_req_t *req, char *PostData)
#endif
}
if (httpd_query_key_value(PostData, "apply", tmp, 5) == ESP_OK)
{
WriteNVSSysConfig(GetSysConf());

View File

@ -365,7 +365,10 @@ static void HTTPPrint_wifisc(char *VarData, void *arg)
snprintf(VarData, MAX_DYNVAR_LENGTH, "{\"ssid\":\"%s\",\"rssi\":%i,\"ch\":%d}", R->ssid, R->rssi,
R->primary);
}
static void HTTPPrint_wifipwr(char *VarData, void *arg)
{
snprintf(VarData, MAX_DYNVAR_LENGTH, "%d", (unsigned int)(GetSysConf()->wifiSettings.MaxPower / 4));
}
#endif
#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE
@ -749,6 +752,7 @@ dyn_var_handler_t HANDLERS_ARRAY[] = {
{ "macadr", sizeof("macadr") - 1, &HTTPPrint_macadr },
{ "apmacadr", sizeof("apmacadr") - 1, &HTTPPrint_apmacadr },
{ "wifisc", sizeof("wifisc") - 1, &HTTPPrint_wifisc },
{ "wifipwr", sizeof("wifipwr") - 1, &HTTPPrint_wifipwr },
#endif
#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE

View File

@ -298,7 +298,7 @@ static void ResetSysConfig(SYS_CONFIG *Conf)
(esp_ip4_addr_t*) &Conf->wifiSettings.DNSAddr2);
esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT,
(esp_ip4_addr_t*) &Conf->wifiSettings.DNSAddr3);
Conf->wifiSettings.MaxPower = 80;
#endif
#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE

View File

@ -56,6 +56,8 @@ static bool isWiFiGotIp = false;
static wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
static bool isScanReady = false;
static TaskHandle_t reconnect_task = NULL;
wifi_ap_record_t* GetWiFiAPRecord(uint8_t n)
{
if (n < DEFAULT_SCAN_LIST_SIZE)
@ -87,7 +89,6 @@ void resonnectWithDelay(void *agr)
vTaskDelete(NULL);
}
static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id,
void *event_data)
@ -105,13 +106,23 @@ static void event_handler(void *arg, esp_event_base_t event_base,
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED)
{
ESP_LOGI(TAG, "Connected to AP");
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
isWiFiGotIp = false;
ESP_LOGI(TAG, "connect to the AP fail, retry in %d seconds", WIFI_CONNECT_AFTER_FAIL_DELAY);
xTaskCreate(resonnectWithDelay, "reconnect_delay", 1024, NULL, 3, NULL);
//esp_wifi_connect();
/*
ESP_LOGE(TAG, "Connect to the AP fail");
if (!reconnect_task)
{
xTaskCreate(resonnectWithDelay, "reconnect_delay", 1024, NULL, 3, &reconnect_task);
ESP_LOGW(TAG, "Pending reconnect in %d seconds", WIFI_CONNECT_AFTER_FAIL_DELAY);
}
*/
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
@ -131,7 +142,7 @@ static void event_handler(void *arg, esp_event_base_t event_base,
ESP_LOGI(TAG, "~~~~~~~~~~~");
isWiFiGotIp = true;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
if (event_id == WIFI_EVENT_AP_STACONNECTED)
@ -212,6 +223,10 @@ static void wifi_init_softap(void *pvParameter)
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
int max_power = GetSysConf()->wifiSettings.MaxPower;
if (max_power >= 8 && max_power <= 84)
esp_wifi_set_max_tx_power(max_power);
ESP_LOGI(TAG, "wifi_init_softap finished");
vTaskDelete(NULL);
}
@ -291,6 +306,10 @@ static void wifi_init_sta(void *pvParameter)
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
int max_power = GetSysConf()->wifiSettings.MaxPower;
if (max_power >= 8 && max_power <= 84)
esp_wifi_set_max_tx_power(max_power);
ESP_LOGI(TAG, "wifi_init_sta finished.");
vTaskDelete(NULL);
}
@ -438,6 +457,15 @@ static void wifi_init_apsta(void *pvParameter)
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
int max_power = GetSysConf()->wifiSettings.MaxPower;
if (max_power >= 8 && max_power <= 84)
esp_wifi_set_max_tx_power(max_power);
wifi_country_t CC;
esp_wifi_get_country(&CC);
ESP_LOGW(TAG, "Country code %.*s, start_ch=%d, total_ch=%d, max power %d", 3, CC.cc, CC.schan, CC.nchan,
CC.max_tx_power);
ESP_LOGI(TAG, "wifi_init_softap_sta finished");
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
@ -515,11 +543,13 @@ static void wifi_scan(void *arg)
{
uint16_t number = DEFAULT_SCAN_LIST_SIZE;
uint16_t ap_count = 0;
esp_wifi_disconnect();
memset(ap_info, 0, sizeof(ap_info));
esp_wifi_scan_start(NULL, true);
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
ESP_LOGI(TAG, "Total APs scanned = %u", ap_count);
esp_wifi_connect();
isScanReady = true;
vTaskDelete(NULL);
}