diff --git a/library.json b/library.json index c61fe73..dcb5b20 100644 --- a/library.json +++ b/library.json @@ -12,7 +12,7 @@ "type": "git", "url": "https://git.xpablo.cz/pablo2048/WiFiConfig.git" }, - "version": "6.3.5", + "version": "6.4.0", "license": "MIT", "frameworks": "arduino", "platforms": ["espressif8266", "espressif32"], diff --git a/library.properties b/library.properties index 89f7fb7..f6bd1e8 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=WiFiConfig -version=6.3.5 +version=6.4.0 author=Pavel Brychta -maintainer=Pavel Brychta +maintainer=Pavel Brychta sentence=Enables seamless module configuration. paragraph=With this library you can enable your ESP8266 or ESP32 module to be configured over WiFi. Includes Captive Portal for easy configuration. category=Other -url=http://www.xpablo.cz +url=https://www.xpablo.cz architectures=esp8266,esp32 diff --git a/src/WiFiConfig.cpp b/src/WiFiConfig.cpp index 401285f..46824ce 100644 --- a/src/WiFiConfig.cpp +++ b/src/WiFiConfig.cpp @@ -1,4 +1,6 @@ /* + * v6.4.0- 27.2.2021 - funkcni inicializace konfigurace + * * v6.3.5-28.11.2020 - moznost nadefinovat titulek webove stranky a popisek okna v prohlizeci pomoci WIFICONFIG_TITLE a WIFICONFIG_WEBTITLE * * v6.3.4-23.3.2020 - staticka konfigurace v AP rezimu funkcni @@ -101,6 +103,14 @@ #include #include "embHTML.h" +#if defined(ESP8266) +// ESP8266 +# define SETHOSTNAME(a) WiFi.hostname(a) +#else +// ESP32 +# define SETHOSTNAME(a) WiFi.setHostname(a) +#endif + #ifdef DEBUG_ESP_PORT # define DEBUG_MSG(_1, ...) DEBUG_ESP_PORT.printf_P(PSTR(_1), ##__VA_ARGS__) #else @@ -609,65 +619,7 @@ void WiFiConfig::_handleSetAP(void) server->send_P(200, TEXTHTML, PAGE_SAVED); delay(2000); // cekame na odeslani dat // nakonfigurujeme ESP dle nove nastavenych parametru - WiFi.persistent(true); // chceme, aby si modul zapamatoval konfiguraci - 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 defined(ESP8266) - if (strlen(WiFiDeviceName)) { - WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni - } -#endif - 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()); -#if !defined(ESP8266) - if (strlen(WiFiDeviceName)) { - WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni - } -#endif -#if defined(ESP8266) - wifi_station_set_auto_connect(true); -#else - //esp_wifi_set_auto_connect(true); -#endif - - delay(1000); - } - break; - - case WIFIMODE_AP: - DEBUG_MSG("AP mode.\r\n"); - 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))) { - WiFi.softAPConfig(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), - IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask)))); - } - break; - - default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???) - DEBUG_MSG("Mode Error!!\r\n"); - break; - } - - delay(1000); // cekame XX sekund na ulozeni atd... + _prepareWifi(); ESP.restart(); } @@ -777,65 +729,6 @@ 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) { @@ -941,11 +834,7 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificon { switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) { case WIFIMODE_STA: { -#if defined(ESP8266) - WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni -#else - WiFi.setHostname(WiFiDeviceName); -#endif + SETHOSTNAME(WiFiDeviceName); // nastavime jmeno zarizeni if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) { DEBUG_MSG("Static configuration SET.\r\n"); WiFi.config(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), @@ -1000,34 +889,90 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int tim return begin(configarea, forceConfigure, cb); // spustime WiFi } -void WiFiConfig::initConfig(int configBase, const String& ssid, const String& pass, const int mode, const int ipcfg, const IPAddress& ip, const IPAddress& mask, const IPAddress& gw, const IPAddress& dns) +void WiFiConfig::initConfig(int cfgBase, const String &ssid, const String &pass, const int mode, const int ipcfg, const IPAddress &ip, const IPAddress &mask, const IPAddress &gw, const IPAddress &dns) { + DEBUG_MSG("initConfig %d\r\n", mode); + configBase = cfgBase; EES_storeString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), (String &)ssid); EES_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), (String &)pass); EEPROM.write(configBase + offsetof(wificonfigarea_t, mode), mode); EEPROM.write(configBase + offsetof(wificonfigarea_t, ip), ipcfg); EEPROM.write(configBase + offsetof(wificonfigarea_t, apchannel), 3); - if (IPCONFIG_STATIC == ipcfg) { - setEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr), (uint32_t) ip); - setEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask), (uint32_t) mask); - setEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway), (uint32_t) gw); - setEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns), (uint32_t) dns); - } + setEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr), (uint32_t) ip); + setEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask), (uint32_t) mask); + setEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway), (uint32_t) gw); + setEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns), (uint32_t) dns); + DEBUG_MSG("IP: %s\r\n", ip.toString().c_str()); EEPROM.commit(); // ulozime zmeny v EEPROM - _prepareWifi(nullptr); // nastavime novou WiFi konfiguraci + _prepareWifi(); // nastavime novou WiFi konfiguraci } -// Zastarale - bude brzy odstraneno!!! -void WiFiConfigPrepare(int configBase, String ssid, String pass) +void WiFiConfig::_prepareWifi(void) { - EES_storeString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), ssid); - EES_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), pass); - EEPROM.write(configBase + offsetof(wificonfigarea_t, mode), WIFIMODE_STA); - EEPROM.write(configBase + offsetof(wificonfigarea_t, ip), IPCONFIG_DHCP); // mame DHCP dynamickou konfiguraci - EEPROM.commit(); // ulozime zmeny v EEPROM - WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta - WiFi.begin(ssid.c_str(), pass.c_str()); + WiFi.persistent(true); // chceme, aby si modul zapamatoval konfiguraci + WiFi.disconnect(); // vsechno odpojime + delay(100); + + 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)); + uint8_t mode = EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)); + + switch (mode) { + case WIFIMODE_STA: { + DEBUG_MSG("STA mode.\r\n"); +#if defined(ESP8266) + if (strlen(WiFiDeviceName)) { + SETHOSTNAME(WiFiDeviceName); // nastavime jmeno zarizeni + } +#endif + 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(ESP8266) + if (strlen(WiFiDeviceName)) { + SETHOSTNAME(WiFiDeviceName); // nastavime jmeno zarizeni + } +#endif +#if defined(ESP8266) + WiFi.setAutoConnect(true); + WiFi.setAutoReconnect(true); +// wifi_station_set_auto_connect(true); +#else + //esp_wifi_set_auto_connect(true); +#endif + delay(1000); + } + break; + + case WIFIMODE_AP: + DEBUG_MSG("AP mode.\r\n"); + 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))) { + WiFi.softAPConfig(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), + IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask)))); + } + break; + + default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???) + DEBUG_MSG("Mode Error (%d)!!\r\n", (int)mode); + break; + } + + delay(1000); // cekame XX sekund na ulozeni atd... } // EOF diff --git a/src/WiFiConfig.h b/src/WiFiConfig.h index 4722d94..f4f644c 100644 --- a/src/WiFiConfig.h +++ b/src/WiFiConfig.h @@ -2,7 +2,7 @@ * @file WiFiConfig.h * @author Pavel Brychta, http://www.xpablo.cz * - * Copyright (c) 2015-20 Pavel Brychta. All rights reserved. + * Copyright (c) 2015-21 Pavel Brychta. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -183,10 +183,10 @@ public: wificonfigresult_t begin(int configarea, int forceConfigure, wificonfig_cb cb); wificonfigresult_t begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb); void addParameter(WiFiConfigUsrParameter *p); - 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); + void initConfig(int cfgBase, 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); + void _prepareWifi(void); wificonfigresult_t _setupAP(wificonfig_cb cb); void _handleDisplayAP(void); void _handleSetAP(void); @@ -205,7 +205,4 @@ private: uint8_t _status; // stav pripojeni - je pouzity pro signalizaci duvodu, proc se nepripoji k AP }; -// Priprava (inicializace) EEPROM oblasti napr. u zcela noveho zarizeni -// EEPROM je inicializovana pro rezim jen STA!!! -void WiFiConfigPrepare(int configBase, String ssid, String pass); #endif