From d140ccff84fc4881095aeddced2b7917e708963f Mon Sep 17 00:00:00 2001 From: Shawn A Date: Thu, 19 Jul 2018 10:02:53 -0500 Subject: [PATCH] adds setCountry, default US accepts US JP CN, codes must be added to strings and WiFiSetCountry atm --- WiFiManager.cpp | 28 +++++++++++++++++++++++++++- WiFiManager.h | 4 ++++ strings_en.h | 4 ++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/WiFiManager.cpp b/WiFiManager.cpp index 4f5ecaa..b18423e 100644 --- a/WiFiManager.cpp +++ b/WiFiManager.cpp @@ -218,6 +218,8 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) { DEBUG_WM(DEBUG_ERROR,"[FATAL] Unable to enable wifi!"); return false; } + + WiFiSetCountry(); #ifdef ESP32 if(esp32persistent) WiFi.persistent(false); // disable persistent for esp32 after esp_wifi_start or else saves wont work @@ -484,6 +486,7 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo // start access point DEBUG_WM(DEBUG_VERBOSE,F("Enabling AP")); startAP(); + WiFiSetCountry(); // init configportal DEBUG_WM(DEBUG_DEV,F("setupConfigPortal")); @@ -2129,6 +2132,14 @@ bool WiFiManager::getWiFiIsSaved(){ return WiFi_hasAutoConnect(); } +/** + * setCountry + * @since $dev + * @param String cc country code, must be defined in WiFiSetCountry, US, JP, CN + */ +void WiFiManager::setCountry(String cc){ + _wificountry = cc; +} // HELPERS @@ -2167,7 +2178,6 @@ void WiFiManager::DEBUG_WM(wm_debuglevel_t level,Generic text,Genericb textb) { _debugPort.println(); } - /** * [debugSoftAPConfig description] * @access public @@ -2303,6 +2313,22 @@ String WiFiManager::getModeString(uint8_t mode){ return FPSTR(S_NA); } + +bool WiFiManager::WiFiSetCountry(){ + bool ret = false; + // @todo check if wifi is init, no idea how, doesnt seem to be exposed + if(_wificountry == "") return ret; // skip not set + else if(WiFi.getMode() == WIFI_MODE_NULL); // exception if wifi not init! + else if(_wificountry == "US") ret = esp_wifi_set_country(&WM_COUNTRY_US) == ESP_OK; + else if(_wificountry == "JP") ret = esp_wifi_set_country(&WM_COUNTRY_JP) == ESP_OK; + else if(_wificountry == "CN") ret = esp_wifi_set_country(&WM_COUNTRY_CN) == ESP_OK; + else DEBUG_WM(DEBUG_ERROR,"[ERROR] country code not found"); + + if(ret) DEBUG_WM(DEBUG_VERBOSE,"esp_wifi_set_country: " + _wificountry); + else DEBUG_WM(DEBUG_ERROR,"[ERROR] esp_wifi_set_country failed"); + return ret; +} + // set mode ignores WiFi.persistent bool WiFiManager::WiFi_Mode(WiFiMode_t m,bool persistent) { bool ret; diff --git a/WiFiManager.h b/WiFiManager.h index 99f4330..6720ce5 100644 --- a/WiFiManager.h +++ b/WiFiManager.h @@ -235,6 +235,7 @@ class WiFiManager // debug output platform info and versioning void debugPlatformInfo(); String htmlEntities(String str); + void setCountry(String cc); private: std::unique_ptr dnsServer; @@ -314,6 +315,8 @@ class WiFiManager boolean _preloadwifiscan = true; // preload wifiscan if true boolean _disableIpFields = false; // edge case, if true, showxFields(false) forces ip fields off instead of default show when set + String _wificountry = "US"; // country code, @todo define in strings lang + // wrapper functions for handling setting and unsetting persistent for now. bool esp32persistent = false; bool _hasBegun = false; @@ -372,6 +375,7 @@ class WiFiManager bool WiFi_scanNetworks(); bool WiFi_scanNetworks(bool force); bool WiFi_scanNetworks(unsigned int cachetime); + bool WiFiSetCountry(); #ifdef ESP32 static void WiFiEvent(WiFiEvent_t event, system_event_info_t info); diff --git a/strings_en.h b/strings_en.h index 60850a2..de5d57d 100644 --- a/strings_en.h +++ b/strings_en.h @@ -318,3 +318,7 @@ const char * const AUTH_MODE_NAMES[] PROGMEM #endif const char* const WIFI_MODES[] PROGMEM = { "NULL", "STA", "AP", "STA+AP" }; + +const wifi_country_t WM_COUNTRY_US{"US",1,11,WIFI_COUNTRY_POLICY_AUTO}; +const wifi_country_t WM_COUNTRY_CN{"CN",1,13,WIFI_COUNTRY_POLICY_AUTO}; +const wifi_country_t WM_COUNTRY_JP{"JP",1,14,WIFI_COUNTRY_POLICY_AUTO}; \ No newline at end of file