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 b6 :1;
char bIsWiFiEnabled :1; char bIsWiFiEnabled :1;
} Flags1; // Flag structure } Flags1; // Flag structure
uint8_t MaxPower;
} wifiSettings; } wifiSettings;
#endif #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")) else if (!strcmp((const char*) tmp, (const char*) "apsta"))
GetSysConf()->wifiSettings.WiFiMode = WIFI_MODE_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*/ /*AP section*/
httpd_query_key_value(PostData, "wfiap", GetSysConf()->wifiSettings.ApSSID, httpd_query_key_value(PostData, "wfiap", GetSysConf()->wifiSettings.ApSSID,
sizeof(GetSysConf()->wifiSettings.ApSSID)); sizeof(GetSysConf()->wifiSettings.ApSSID));
@ -291,7 +298,6 @@ static HTTP_IO_RESULT HTTPPostAdaptersSettings(httpd_req_t *req, char *PostData)
#endif #endif
} }
if (httpd_query_key_value(PostData, "apply", tmp, 5) == ESP_OK) if (httpd_query_key_value(PostData, "apply", tmp, 5) == ESP_OK)
{ {
WriteNVSSysConfig(GetSysConf()); 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, snprintf(VarData, MAX_DYNVAR_LENGTH, "{\"ssid\":\"%s\",\"rssi\":%i,\"ch\":%d}", R->ssid, R->rssi,
R->primary); R->primary);
} }
static void HTTPPrint_wifipwr(char *VarData, void *arg)
{
snprintf(VarData, MAX_DYNVAR_LENGTH, "%d", (unsigned int)(GetSysConf()->wifiSettings.MaxPower / 4));
}
#endif #endif
#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE #if CONFIG_WEBGUIAPP_ETHERNET_ENABLE
@ -749,6 +752,7 @@ dyn_var_handler_t HANDLERS_ARRAY[] = {
{ "macadr", sizeof("macadr") - 1, &HTTPPrint_macadr }, { "macadr", sizeof("macadr") - 1, &HTTPPrint_macadr },
{ "apmacadr", sizeof("apmacadr") - 1, &HTTPPrint_apmacadr }, { "apmacadr", sizeof("apmacadr") - 1, &HTTPPrint_apmacadr },
{ "wifisc", sizeof("wifisc") - 1, &HTTPPrint_wifisc }, { "wifisc", sizeof("wifisc") - 1, &HTTPPrint_wifisc },
{ "wifipwr", sizeof("wifipwr") - 1, &HTTPPrint_wifipwr },
#endif #endif
#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE #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_ip4_addr_t*) &Conf->wifiSettings.DNSAddr2);
esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT,
(esp_ip4_addr_t*) &Conf->wifiSettings.DNSAddr3); (esp_ip4_addr_t*) &Conf->wifiSettings.DNSAddr3);
Conf->wifiSettings.MaxPower = 80;
#endif #endif
#if CONFIG_WEBGUIAPP_ETHERNET_ENABLE #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 wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
static bool isScanReady = false; static bool isScanReady = false;
static TaskHandle_t reconnect_task = NULL;
wifi_ap_record_t* GetWiFiAPRecord(uint8_t n) wifi_ap_record_t* GetWiFiAPRecord(uint8_t n)
{ {
if (n < DEFAULT_SCAN_LIST_SIZE) if (n < DEFAULT_SCAN_LIST_SIZE)
@ -87,7 +89,6 @@ void resonnectWithDelay(void *agr)
vTaskDelete(NULL); vTaskDelete(NULL);
} }
static void event_handler(void *arg, esp_event_base_t event_base, static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, int32_t event_id,
void *event_data) 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) else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED)
{ {
ESP_LOGI(TAG, "Connected to AP"); 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) else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{ {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
isWiFiGotIp = false; isWiFiGotIp = false;
ESP_LOGI(TAG, "connect to the AP fail, retry in %d seconds", WIFI_CONNECT_AFTER_FAIL_DELAY); //esp_wifi_connect();
xTaskCreate(resonnectWithDelay, "reconnect_delay", 1024, NULL, 3, NULL);
/*
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) 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, "~~~~~~~~~~~"); ESP_LOGI(TAG, "~~~~~~~~~~~");
isWiFiGotIp = true; isWiFiGotIp = true;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
} }
if (event_id == WIFI_EVENT_AP_STACONNECTED) 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_set_config(WIFI_IF_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start()); 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"); ESP_LOGI(TAG, "wifi_init_softap finished");
vTaskDelete(NULL); 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_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start()); 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."); ESP_LOGI(TAG, "wifi_init_sta finished.");
vTaskDelete(NULL); 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_set_config(WIFI_IF_STA, &sta_wifi_config));
ESP_ERROR_CHECK(esp_wifi_start()); 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"); ESP_LOGI(TAG, "wifi_init_softap_sta finished");
/* 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
@ -515,11 +543,13 @@ static void wifi_scan(void *arg)
{ {
uint16_t number = DEFAULT_SCAN_LIST_SIZE; uint16_t number = DEFAULT_SCAN_LIST_SIZE;
uint16_t ap_count = 0; uint16_t ap_count = 0;
esp_wifi_disconnect();
memset(ap_info, 0, sizeof(ap_info)); memset(ap_info, 0, sizeof(ap_info));
esp_wifi_scan_start(NULL, true); 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_records(&number, ap_info));
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count)); ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
ESP_LOGI(TAG, "Total APs scanned = %u", ap_count); ESP_LOGI(TAG, "Total APs scanned = %u", ap_count);
esp_wifi_connect();
isScanReady = true; isScanReady = true;
vTaskDelete(NULL); vTaskDelete(NULL);
} }