diff --git a/src/WiFiConfig.cpp b/src/WiFiConfig.cpp index e7a4b95..def10b4 100644 --- a/src/WiFiConfig.cpp +++ b/src/WiFiConfig.cpp @@ -775,6 +775,65 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb) return WCR_TIMEOUT; // nepripojeno, vyprsel timeout konfiguracniho AP } +void WiFiConfig::_prepareWifi(wificonfig_cb cb) +{ +// nakonfigurujeme ESP dle nove nastavenych parametru + WiFi.disconnect(); // vsechno odpojime + + String s = EES_readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid)); + String pass = EES_readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass)); + + switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) { + case WIFIMODE_STA: { + DEBUG_MSG("STA mode.\r\n"); + if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) { + DEBUG_MSG("Static configuration.\r\n"); + WiFi.config(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), + IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns)))); + delay(100); + } + WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta + WiFi.begin(s.c_str(), pass.c_str()); + DEBUG_MSG("STA params: %s, %s\r\n", s.c_str(), pass.c_str()); +#if defined(ESP32) + if (strlen(WiFiDeviceName)) { + WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni + } +#endif + } + break; + + case WIFIMODE_AP: + DEBUG_MSG("AP mode.\r\n"); + if (s.endsWith(F("?"))) { + char lmac[16]; + + sprintf_P(lmac, PSTR("%06X"), ESP_getChipId()); + s.replace(F("?"), String(lmac)); + } + WiFi.mode(WIFI_AP); // startujeme AP + if (pass.length()) { + // je zadane heslo do AP + WiFi.softAP(s.c_str(), pass.c_str(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel))); + } else { + // otevreny AP + WiFi.softAP(s.c_str(), NULL, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel))); + } + if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) { + delay(100); // kvuli ESP32 - cekame na start AP + WiFi.softAPConfig(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), + IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask)))); + } + if (cb) + cb(WCS_CONNECTED); + break; + + default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???) + DEBUG_MSG("Mode Error!!\r\n"); + break; + } +} + // Testovani, zda se modul pripojil k AP bool WiFiConfig::_testWifi(wificonfig_cb cb) { @@ -954,7 +1013,7 @@ void WiFiConfig::initConfig(int configBase, const String& ssid, const String& pa setEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns), (uint32_t) dns); } EEPROM.commit(); // ulozime zmeny v EEPROM - _testWifi(nullptr); // nastavime novou WiFi konfiguraci + _prepareWifi(nullptr); // nastavime novou WiFi konfiguraci } // Zastarale - bude brzy odstraneno!!! diff --git a/src/WiFiConfig.h b/src/WiFiConfig.h index 7accc91..4722d94 100644 --- a/src/WiFiConfig.h +++ b/src/WiFiConfig.h @@ -186,6 +186,7 @@ public: void initConfig(int configBase, const String& ssid, const String&pass, const int mode = WIFIMODE_STA, const int ipcfg = IPCONFIG_DHCP, const IPAddress& ip = 0, const IPAddress& mask = 0, const IPAddress& gw = 0, const IPAddress& dns = 0); private: + void _prepareWifi(wificonfig_cb cb); wificonfigresult_t _setupAP(wificonfig_cb cb); void _handleDisplayAP(void); void _handleSetAP(void);