|
|
|
@ -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 <encipheredEEPROMStrings.h>
|
|
|
|
|
#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
|
|
|
|
|