|
|
|
@ -1,4 +1,6 @@
|
|
|
|
|
/*
|
|
|
|
|
* v7.0.0- 4.3.2021 - verze s konfiguraci v JSON na souborovem systemu
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
@ -98,11 +100,14 @@
|
|
|
|
|
# include <rom/rtc.h> // duvod resetu (https://github.com/espressif/arduino-esp32/issues/449)
|
|
|
|
|
#endif
|
|
|
|
|
#include "WiFiConfig.h"
|
|
|
|
|
#include <EEPROM.h>
|
|
|
|
|
#include <Ticker.h>
|
|
|
|
|
#include <encipheredEEPROMStrings.h>
|
|
|
|
|
#include <sysvars.hpp>
|
|
|
|
|
#include "embHTML.h"
|
|
|
|
|
|
|
|
|
|
#if not defined(WIFICFG_FILE)
|
|
|
|
|
# define WIFICFG_FILE "/wifi.json"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
// ESP8266
|
|
|
|
|
# define SETHOSTNAME(a) WiFi.hostname(a)
|
|
|
|
@ -131,8 +136,6 @@ extern "C" {
|
|
|
|
|
|
|
|
|
|
extern char WiFiDeviceName[];
|
|
|
|
|
|
|
|
|
|
static int configBase; // musi byt trvale ulozene, aby fungovaly metody pro ziskani retezcu z EEPROM
|
|
|
|
|
|
|
|
|
|
static std::unique_ptr<DNSServer> dnsServer;
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
static std::unique_ptr<ESP8266WebServer> server;
|
|
|
|
@ -168,64 +171,12 @@ IPAddress getOurIP(void)
|
|
|
|
|
uint8_t * getOurMAC(uint8_t *mac)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (WIFIMODE_STA == EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)))
|
|
|
|
|
if (WIFIMODE_STA == svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE)))
|
|
|
|
|
return WiFi.macAddress(mac);
|
|
|
|
|
else
|
|
|
|
|
return WiFi.softAPmacAddress(mac);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t getEEPROMuint32(unsigned int start)
|
|
|
|
|
{
|
|
|
|
|
uint32_t result = 0;
|
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 4; ++i) {
|
|
|
|
|
result <<= 8;
|
|
|
|
|
result += EEPROM.read(start);
|
|
|
|
|
++start;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setEEPROMuint32(unsigned int start, uint32_t val)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 4; ++i) {
|
|
|
|
|
EEPROM.write(start + 3 - i, (uint8_t)val);
|
|
|
|
|
val >>= 8;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String getEEPROMString(unsigned int start, size_t len)
|
|
|
|
|
{
|
|
|
|
|
String string = "";
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = start; i < + start + len; ++i) {
|
|
|
|
|
uint8_t b = EEPROM.read(i);
|
|
|
|
|
|
|
|
|
|
if ((0xff == b) || (0 == b))
|
|
|
|
|
break;
|
|
|
|
|
string += char(b);
|
|
|
|
|
}
|
|
|
|
|
return string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setEEPROMString(unsigned int start, size_t len, String &string)
|
|
|
|
|
{
|
|
|
|
|
unsigned int si = 0;
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = start; i < start + len; ++i) {
|
|
|
|
|
char c;
|
|
|
|
|
|
|
|
|
|
if (si < string.length()) {
|
|
|
|
|
c = string[si];
|
|
|
|
|
} else {
|
|
|
|
|
c = 0;
|
|
|
|
|
}
|
|
|
|
|
EEPROM.write(i, c);
|
|
|
|
|
++si;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WiFiConfigUsrParameter::WiFiConfigUsrParameter(const char *id, const char *label, const char *defaultValue, unsigned int length, storeparam_cb cb)
|
|
|
|
|
{
|
|
|
|
|
_next = NULL;
|
|
|
|
@ -437,7 +388,7 @@ void WiFiConfig::_handleRoot(void)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0xfe:
|
|
|
|
|
content.concat(F("Špatná EEPROM"));
|
|
|
|
|
content.concat(F("Špatná konfigurace"));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0xff:
|
|
|
|
@ -508,30 +459,31 @@ void WiFiConfig::_handleDisplayAP(void)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s = FPSTR(PAGE_INDEX2);
|
|
|
|
|
v = EES_readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
|
|
|
|
v = svGetV<String>(F("ssid"), nullptr, F(WIFICFG_FILE));
|
|
|
|
|
strDecode(v);
|
|
|
|
|
if (v.length())
|
|
|
|
|
s.replace(F("{s}"), String(FPSTR(VALUE)) + v + F("'"));
|
|
|
|
|
else
|
|
|
|
|
s.replace(F("{s}"), F(""));
|
|
|
|
|
v = EES_readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
|
|
|
|
|
v = svGetV<String>(F("pass"), nullptr, F(WIFICFG_FILE));
|
|
|
|
|
if (v.length())
|
|
|
|
|
s.replace(F("{p}"), String(FPSTR(VALUE)) + v + F("'"));
|
|
|
|
|
else
|
|
|
|
|
s.replace(F("{p}"), F(""));
|
|
|
|
|
if (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)) == WIFIMODE_AP)
|
|
|
|
|
if (svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE)) == WIFIMODE_AP)
|
|
|
|
|
s.replace(F("{a}"), FPSTR(CHECKED));
|
|
|
|
|
else
|
|
|
|
|
s.replace(F("{a}"), F(""));
|
|
|
|
|
if (EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)) == IPCONFIG_STATIC)
|
|
|
|
|
if (svGetV<int>(F("ipmode"), nullptr, F(WIFICFG_FILE)) == IPCONFIG_STATIC)
|
|
|
|
|
s.replace(F("{c}"), FPSTR(CHECKED));
|
|
|
|
|
else
|
|
|
|
|
s.replace(F("{c}"), F(""));
|
|
|
|
|
s.replace(F("{i}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))).toString());
|
|
|
|
|
s.replace(F("{m}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))).toString());
|
|
|
|
|
s.replace(F("{g}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))).toString());
|
|
|
|
|
s.replace(F("{d}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns))).toString());
|
|
|
|
|
s.replace(F("{i}"), IPAddress(svGetV<uint32_t>(F("ip"), nullptr, F(WIFICFG_FILE))).toString());
|
|
|
|
|
s.replace(F("{m}"), IPAddress(svGetV<uint32_t>(F("mask"), nullptr, F(WIFICFG_FILE))).toString());
|
|
|
|
|
s.replace(F("{g}"), IPAddress(svGetV<uint32_t>(F("gate"), nullptr, F(WIFICFG_FILE))).toString());
|
|
|
|
|
s.replace(F("{d}"), IPAddress(svGetV<uint32_t>(F("dns"), nullptr, F(WIFICFG_FILE))).toString());
|
|
|
|
|
|
|
|
|
|
uint8_t chan = EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel));
|
|
|
|
|
uint8_t chan = svGetV<int>(F("channel"), nullptr, F(WIFICFG_FILE));
|
|
|
|
|
if ((chan < 1) || (chan > 13))
|
|
|
|
|
chan = 1; // neplatne cislo kanalu nahradime nejnizsim
|
|
|
|
|
s.replace(F("{ch}"), String(chan));
|
|
|
|
@ -567,43 +519,45 @@ void WiFiConfig::_handleSetAP(void)
|
|
|
|
|
str.reserve(128);
|
|
|
|
|
str = server->arg(F("_s"));
|
|
|
|
|
if (str.length() > 0) {
|
|
|
|
|
EES_storeString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), str);
|
|
|
|
|
|
|
|
|
|
strEncode(str);
|
|
|
|
|
svSetV(F("ssid"), str, F(WIFICFG_FILE));
|
|
|
|
|
str = server->arg(F("_p"));
|
|
|
|
|
EES_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str);
|
|
|
|
|
strEncode(str);
|
|
|
|
|
svSetV(F("pass"), str, F(WIFICFG_FILE));
|
|
|
|
|
|
|
|
|
|
str = server->arg(F("_a"));
|
|
|
|
|
if (str.length() > 0) {
|
|
|
|
|
mode = WIFIMODE_AP; // rezim AP
|
|
|
|
|
str = server->arg(F("_ch")); // kanal AP
|
|
|
|
|
EEPROM.write(configBase + offsetof(wificonfigarea_t, apchannel), (uint8_t)str.toInt());
|
|
|
|
|
svSetV<int>(F("channel"), str.toInt(), F(WIFICFG_FILE));
|
|
|
|
|
} else
|
|
|
|
|
mode = WIFIMODE_STA; // rezim STA
|
|
|
|
|
EEPROM.write(configBase + offsetof(wificonfigarea_t, mode), mode);
|
|
|
|
|
svSetV(F("mode"), mode, F(WIFICFG_FILE));
|
|
|
|
|
|
|
|
|
|
str = server->arg(F("_st"));
|
|
|
|
|
if (0 == str.length())
|
|
|
|
|
EEPROM.write(configBase + offsetof(wificonfigarea_t, ip), IPCONFIG_DHCP); // mame DHCP dynamickou konfiguraci
|
|
|
|
|
svSetV<int>(F("ipmode"), IPCONFIG_DHCP, F(WIFICFG_FILE)); // mame DHCP dynamickou konfiguraci
|
|
|
|
|
else {
|
|
|
|
|
// staticka ip konfigurace
|
|
|
|
|
IPAddress ipa;
|
|
|
|
|
|
|
|
|
|
EEPROM.write(configBase + offsetof(wificonfigarea_t, ip), IPCONFIG_STATIC);
|
|
|
|
|
svSetV<int>(F("ipmode"), IPCONFIG_STATIC, F(WIFICFG_FILE)); // mame statickou konfiguraci
|
|
|
|
|
str = server->arg(F("_i"));
|
|
|
|
|
ipa.fromString(str);
|
|
|
|
|
setEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr), (uint32_t) ipa);
|
|
|
|
|
svSetV<uint32_t>(F("ip"), (uint32_t)ipa, F(WIFICFG_FILE));
|
|
|
|
|
|
|
|
|
|
str = server->arg(F("_m"));
|
|
|
|
|
ipa.fromString(str);
|
|
|
|
|
setEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask), (uint32_t) ipa);
|
|
|
|
|
svSetV<uint32_t>(F("mask"), (uint32_t)ipa, F(WIFICFG_FILE));
|
|
|
|
|
|
|
|
|
|
str = server->arg(F("_g"));
|
|
|
|
|
ipa.fromString(str);
|
|
|
|
|
setEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway), (uint32_t) ipa);
|
|
|
|
|
svSetV<uint32_t>(F("gw"), (uint32_t)ipa, F(WIFICFG_FILE));
|
|
|
|
|
|
|
|
|
|
str = server->arg(F("_d"));
|
|
|
|
|
ipa.fromString(str);
|
|
|
|
|
setEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns), (uint32_t) ipa);
|
|
|
|
|
svSetV<uint32_t>(F("dns"), (uint32_t)ipa, F(WIFICFG_FILE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Uzivatelske parametry
|
|
|
|
@ -614,7 +568,6 @@ void WiFiConfig::_handleSetAP(void)
|
|
|
|
|
up->setNewValue(server->arg(i).c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
EEPROM.commit(); // skutecne ulozime data
|
|
|
|
|
}
|
|
|
|
|
server->send_P(200, TEXTHTML, PAGE_SAVED);
|
|
|
|
|
delay(2000); // cekame na odeslani dat
|
|
|
|
@ -738,16 +691,20 @@ bool WiFiConfig::_testWifi(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));
|
|
|
|
|
encString s = svGetV<String>(F("ssid"), nullptr, F(WIFICFG_FILE));
|
|
|
|
|
s.decode();
|
|
|
|
|
encString pass = svGetV<String>(F("pass"), nullptr, F(WIFICFG_FILE));
|
|
|
|
|
pass.decode();
|
|
|
|
|
|
|
|
|
|
switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) {
|
|
|
|
|
switch (svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE))) {
|
|
|
|
|
case WIFIMODE_STA: {
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG("STA mode.\r\n");
|
|
|
|
|
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) {
|
|
|
|
|
if (IPCONFIG_STATIC == svGetV<int>(F("ipmode"), nullptr, F(WIFICFG_FILE))) {
|
|
|
|
|
|
|
|
|
|
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))));
|
|
|
|
|
WiFi.config(IPAddress(svGetV<uint32_t>(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV<uint32_t>(F("gw"), nullptr, F(WIFICFG_FILE))),
|
|
|
|
|
IPAddress(svGetV<uint32_t>(F("mask"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV<uint32_t>(F("dns"), nullptr, F(WIFICFG_FILE))));
|
|
|
|
|
delay(100);
|
|
|
|
|
}
|
|
|
|
|
WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta
|
|
|
|
@ -768,17 +725,18 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
|
|
|
|
|
s.replace(F("?"), String(lmac));
|
|
|
|
|
}
|
|
|
|
|
WiFi.mode(WIFI_AP); // startujeme AP
|
|
|
|
|
int chan = svGetV<int>(F("channel"), nullptr, F(WIFICFG_FILE));
|
|
|
|
|
if (pass.length()) {
|
|
|
|
|
// je zadane heslo do AP
|
|
|
|
|
WiFi.softAP(s.c_str(), pass.c_str(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
|
|
|
|
|
WiFi.softAP(s.c_str(), pass.c_str(), chan);
|
|
|
|
|
} else {
|
|
|
|
|
// otevreny AP
|
|
|
|
|
WiFi.softAP(s.c_str(), NULL, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
|
|
|
|
|
WiFi.softAP(s.c_str(), NULL, chan);
|
|
|
|
|
}
|
|
|
|
|
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) {
|
|
|
|
|
if (IPCONFIG_STATIC == svGetV<int>(F("ipmode"), nullptr, F(WIFICFG_FILE))) {
|
|
|
|
|
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))));
|
|
|
|
|
WiFi.softAPConfig(IPAddress(svGetV<uint32_t>(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV<uint32_t>(F("gw"), nullptr, F(WIFICFG_FILE))),
|
|
|
|
|
IPAddress(svGetV<uint32_t>(F("mask"), nullptr, F(WIFICFG_FILE))));
|
|
|
|
|
}
|
|
|
|
|
if (cb)
|
|
|
|
|
cb(WCS_CONNECTED);
|
|
|
|
@ -811,36 +769,38 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
|
|
|
|
|
return false; // pripojeni se nezdarilo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificonfig_cb cb)
|
|
|
|
|
wificonfigresult_t WiFiConfig::begin(int forceConfigure, wificonfig_cb cb)
|
|
|
|
|
{
|
|
|
|
|
wificonfigresult_t result = WCR_OK; // predpokladame, ze se pripojeni podari
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG("\r\n\r\n"); // oddeleni vypisu
|
|
|
|
|
|
|
|
|
|
configBase = configarea; // pocatek konfigurace v EEPROM
|
|
|
|
|
if (0 == forceConfigure) {
|
|
|
|
|
DEBUG_MSG("Force config.\r\n");
|
|
|
|
|
_status = 0xff;
|
|
|
|
|
result = _setupAP(cb);
|
|
|
|
|
} else {
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
if (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)) != WiFi.getMode()) {
|
|
|
|
|
if (svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE)) != WiFi.getMode()) {
|
|
|
|
|
// neshoduje se rezim - musime spustit konfiguracni AP (poskozena konfigurace)
|
|
|
|
|
DEBUG_MSG("Wrong config (%d, %d)\r\n", EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)), WiFi.getMode());
|
|
|
|
|
DEBUG_MSG("Wrong config (%d, %d)\r\n", svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE)), WiFi.getMode());
|
|
|
|
|
_status = 0xfe;
|
|
|
|
|
result = _setupAP(cb);
|
|
|
|
|
} else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) {
|
|
|
|
|
switch (svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE))) {
|
|
|
|
|
case WIFIMODE_STA: {
|
|
|
|
|
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))),
|
|
|
|
|
IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG("STA mode.\r\n");
|
|
|
|
|
SETHOSTNAME(WiFiDeviceName); // nastavime jmeno zarizeni
|
|
|
|
|
if (IPCONFIG_STATIC == svGetV<int>(F("ipmode"), nullptr, F(WIFICFG_FILE))) {
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG("Static configuration SET.\r\n");
|
|
|
|
|
WiFi.config(IPAddress(svGetV<uint32_t>(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV<uint32_t>(F("gw"), nullptr, F(WIFICFG_FILE))),
|
|
|
|
|
IPAddress(svGetV<uint32_t>(F("mask"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV<uint32_t>(F("dns"), nullptr, F(WIFICFG_FILE))));
|
|
|
|
|
delay(100);
|
|
|
|
|
}
|
|
|
|
|
if (WC_DONT_RUN_CONFIGAP == _timeout)
|
|
|
|
|
result = WCR_CONFIGAP_NOT_STARTED; // nemame spoustet konfiguracni AP - vracime se hned
|
|
|
|
|
else {
|
|
|
|
@ -854,10 +814,11 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificon
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case WIFIMODE_AP:
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG("AP mode.\r\n");
|
|
|
|
|
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))));
|
|
|
|
|
if (IPCONFIG_STATIC == svGetV<int>(F("ipmode"), nullptr, F(WIFICFG_FILE))) {
|
|
|
|
|
WiFi.softAPConfig(IPAddress(svGetV<uint32_t>(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV<uint32_t>(F("gw"), nullptr, F(WIFICFG_FILE))),
|
|
|
|
|
IPAddress(svGetV<uint32_t>(F("mask"), nullptr, F(WIFICFG_FILE))));
|
|
|
|
|
}
|
|
|
|
|
if (cb)
|
|
|
|
|
cb(WCS_CONNECTED); // signalizujeme pripojeni (zde se nic jineho stejne nestane...)
|
|
|
|
@ -865,7 +826,8 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificon
|
|
|
|
|
|
|
|
|
|
#if !defined(ESP8266)
|
|
|
|
|
default:
|
|
|
|
|
DEBUG_MSG("Wrong config (%d)\r\n", EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)));
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG("Wrong config (%d)\r\n", svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE)));
|
|
|
|
|
_status = 0xfe;
|
|
|
|
|
result = _setupAP(cb);
|
|
|
|
|
break;
|
|
|
|
@ -876,7 +838,7 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificon
|
|
|
|
|
return result; // mame vyreseno
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb = NULL)
|
|
|
|
|
wificonfigresult_t WiFiConfig::begin(int forceConfigure, int timeout, wificonfig_cb cb = NULL)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if ((timeout > 0) && (40 > timeout))
|
|
|
|
@ -886,25 +848,27 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int tim
|
|
|
|
|
_timeout = timeout;
|
|
|
|
|
if (_timeout > 0)
|
|
|
|
|
_time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
|
|
|
|
|
return begin(configarea, forceConfigure, cb); // spustime WiFi
|
|
|
|
|
return begin(forceConfigure, cb); // spustime WiFi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
void WiFiConfig::initConfig(const String &ssid, const String &pass, const int mode, const int ipcfg, const IPAddress &ip, const IPAddress &mask, const IPAddress &gw, const IPAddress &dns)
|
|
|
|
|
{
|
|
|
|
|
String es;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
|
es = ssid;
|
|
|
|
|
strEncode(es);
|
|
|
|
|
svSetV<String>(F("ssid"), es, F(WIFICFG_FILE));
|
|
|
|
|
es = pass;
|
|
|
|
|
strEncode(es);
|
|
|
|
|
svSetV<String>(F("pass"), es, F(WIFICFG_FILE));
|
|
|
|
|
svSetV(F("mode"), mode, F(WIFICFG_FILE));
|
|
|
|
|
svSetV(F("ipmode"), ipcfg, F(WIFICFG_FILE));
|
|
|
|
|
svSetV<int>(F("channel"), 3, F(WIFICFG_FILE));
|
|
|
|
|
svSetV<uint32_t>(F("ip"), (uint32_t)ip, F(WIFICFG_FILE));
|
|
|
|
|
svSetV<uint32_t>(F("mask"), (uint32_t)mask, F(WIFICFG_FILE));
|
|
|
|
|
svSetV<uint32_t>(F("gw"), (uint32_t)gw, F(WIFICFG_FILE));
|
|
|
|
|
svSetV<uint32_t>(F("dns"), (uint32_t)dns, F(WIFICFG_FILE));
|
|
|
|
|
_prepareWifi(); // nastavime novou WiFi konfiguraci
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -915,22 +879,26 @@ void WiFiConfig::_prepareWifi(void)
|
|
|
|
|
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));
|
|
|
|
|
String s = svGetV<String>(F("ssid"), nullptr, F(WIFICFG_FILE));
|
|
|
|
|
strDecode(s);
|
|
|
|
|
String pass = svGetV<String>(F("pass"), nullptr, F(WIFICFG_FILE));
|
|
|
|
|
strDecode(pass);
|
|
|
|
|
int mode = svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE));
|
|
|
|
|
|
|
|
|
|
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))) {
|
|
|
|
|
if (IPCONFIG_STATIC == svGetV<int>(F("ipmode"), nullptr, F(WIFICFG_FILE))) {
|
|
|
|
|
|
|
|
|
|
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))));
|
|
|
|
|
WiFi.config(IPAddress(svGetV<uint32_t>(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV<uint32_t>(F("gw"), nullptr, F(WIFICFG_FILE))),
|
|
|
|
|
IPAddress(svGetV<uint32_t>(F("mask"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV<uint32_t>(F("dns"), nullptr, F(WIFICFG_FILE))));
|
|
|
|
|
delay(100);
|
|
|
|
|
}
|
|
|
|
|
WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta
|
|
|
|
@ -952,27 +920,37 @@ void WiFiConfig::_prepareWifi(void)
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case WIFIMODE_AP:
|
|
|
|
|
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
|
|
|
|
|
int chan = svGetV<int>(F("channel"), nullptr, F(WIFICFG_FILE));
|
|
|
|
|
if (pass.length()) {
|
|
|
|
|
// je zadane heslo do AP
|
|
|
|
|
WiFi.softAP(s.c_str(), pass.c_str(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
|
|
|
|
|
WiFi.softAP(s.c_str(), pass.c_str(), chan);
|
|
|
|
|
} else {
|
|
|
|
|
// otevreny AP
|
|
|
|
|
WiFi.softAP(s.c_str(), NULL, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
|
|
|
|
|
WiFi.softAP(s.c_str(), NULL, chan);
|
|
|
|
|
}
|
|
|
|
|
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))));
|
|
|
|
|
if (IPCONFIG_STATIC == svGetV<int>(F("ipmode"), nullptr, F(WIFICFG_FILE))) {
|
|
|
|
|
delay(100); // kvuli ESP32 - cekame na start AP
|
|
|
|
|
WiFi.softAPConfig(IPAddress(svGetV<uint32_t>(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV<uint32_t>(F("gw"), nullptr, F(WIFICFG_FILE))),
|
|
|
|
|
IPAddress(svGetV<uint32_t>(F("mask"), nullptr, F(WIFICFG_FILE))));
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|