diff --git a/include/WebGUIAppMain.h b/include/WebGUIAppMain.h index 939496e..1de696e 100644 --- a/include/WebGUIAppMain.h +++ b/include/WebGUIAppMain.h @@ -160,7 +160,7 @@ char b6 :1; char bIsWiFiEnabled :1; } Flags1; // Flag structure - + uint8_t MaxPower; } wifiSettings; #endif diff --git a/src/HTTPPostSystem.c b/src/HTTPPostSystem.c index da9d99b..a9aa73e 100644 --- a/src/HTTPPostSystem.c +++ b/src/HTTPPostSystem.c @@ -106,26 +106,26 @@ static HTTP_IO_RESULT HTTPPostAdaptersSettings(httpd_req_t *req, char *PostData) if (httpd_query_key_value(PostData, "ethen", tmp, sizeof(tmp)) == ESP_OK) { if (!strcmp((const char*) tmp, (const char*) "1")) - TempIsETHEnabled = true; + TempIsETHEnabled = true; } if (httpd_query_key_value(PostData, "dhcp", tmp, sizeof(tmp)) == ESP_OK) { if (!strcmp((const char*) tmp, (const char*) "1")) - TempIsETHDHCPEnabled = true; + TempIsETHDHCPEnabled = true; } if (httpd_query_key_value(PostData, "ipa", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.IPAddr); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.IPAddr); if (httpd_query_key_value(PostData, "mas", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Mask); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Mask); if (httpd_query_key_value(PostData, "gte", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Gateway); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Gateway); if (httpd_query_key_value(PostData, "dns1", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr1); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr1); if (httpd_query_key_value(PostData, "dns2", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr2); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr2); if (httpd_query_key_value(PostData, "dns3", tmp, 15) == ESP_OK) - esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr3); + esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr3); #endif @@ -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()); diff --git a/src/HTTPPrintSystem.c b/src/HTTPPrintSystem.c index 46f1ef4..5c2a4b4 100644 --- a/src/HTTPPrintSystem.c +++ b/src/HTTPPrintSystem.c @@ -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 diff --git a/src/WebGUIAppMain.c b/src/WebGUIAppMain.c index e24b530..c874fe5 100644 --- a/src/WebGUIAppMain.c +++ b/src/WebGUIAppMain.c @@ -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 diff --git a/src/WiFiTransport.c b/src/WiFiTransport.c index 562b9ee..a412bd2 100644 --- a/src/WiFiTransport.c +++ b/src/WiFiTransport.c @@ -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) @@ -150,7 +161,7 @@ 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() - ; + ; strcpy(if_key_str, "WIFI_AP_USER"); esp_netif_conf.if_key = if_key_str; @@ -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); }