Upravy pro praci se sysvars
This commit is contained in:
		@@ -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;
 | 
			
		||||
@@ -174,58 +177,6 @@ uint8_t * getOurMAC(uint8_t *mac)
 | 
			
		||||
        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;
 | 
			
		||||
@@ -476,7 +427,7 @@ void WiFiConfig::_handleRoot(void)
 | 
			
		||||
void WiFiConfig::_handleDisplayAP(void)
 | 
			
		||||
{
 | 
			
		||||
    String s;
 | 
			
		||||
    String v;
 | 
			
		||||
    encString v;
 | 
			
		||||
    String content;
 | 
			
		||||
 | 
			
		||||
    _time = millis() + (360 * 1000); // spocitame si novy cas, kdy budeme modul restartovat (6 minut)
 | 
			
		||||
@@ -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<encString>(F("ssid"), nullptr, F(WIFICFG_FILE));
 | 
			
		||||
    v.decode();
 | 
			
		||||
    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<encString>(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,49 @@ 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);
 | 
			
		||||
        encString dummy;
 | 
			
		||||
 | 
			
		||||
        dummy = str;
 | 
			
		||||
        dummy.encode();
 | 
			
		||||
        svSetV(F("ssid"), dummy, F(WIFICFG_FILE));
 | 
			
		||||
 | 
			
		||||
        str = server->arg(F("_p"));
 | 
			
		||||
        EES_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str);
 | 
			
		||||
        dummy = str;
 | 
			
		||||
        dummy.encode();
 | 
			
		||||
        svSetV(F("pass"), dummy, 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 +572,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 +695,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<encString>(F("ssid"), nullptr, F(WIFICFG_FILE));
 | 
			
		||||
    s.decode();
 | 
			
		||||
    encString pass = svGetV<encString>(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 +729,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);
 | 
			
		||||
@@ -824,23 +786,26 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificon
 | 
			
		||||
        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 +819,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 +831,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;
 | 
			
		||||
@@ -889,22 +856,24 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int tim
 | 
			
		||||
    return begin(configarea, 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)
 | 
			
		||||
{
 | 
			
		||||
    encString 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;
 | 
			
		||||
    es.encrypt();
 | 
			
		||||
    svSetV<String>(F("ssid"), es, F(WIFICFG_FILE));
 | 
			
		||||
    es = pass;
 | 
			
		||||
    es.encrypt();
 | 
			
		||||
    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 +884,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));
 | 
			
		||||
    encString s = svGetV<encString>(F("ssid"), nullptr, F(WIFICFG_FILE));
 | 
			
		||||
    s.decode();
 | 
			
		||||
    encString pass = svGetV<encString>(F("pass"), nullptr, F(WIFICFG_FILE));
 | 
			
		||||
    pass.decode();
 | 
			
		||||
    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
 | 
			
		||||
@@ -953,26 +926,35 @@ void WiFiConfig::_prepareWifi(void)
 | 
			
		||||
    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
 | 
			
		||||
        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
 | 
			
		||||
 
 | 
			
		||||
@@ -37,23 +37,8 @@
 | 
			
		||||
#endif
 | 
			
		||||
#include <memory>
 | 
			
		||||
 | 
			
		||||
#define elementSize(type, element) sizeof(((type *)0)->element)
 | 
			
		||||
 | 
			
		||||
#define WC_DONT_RUN_CONFIGAP -1 // priznak, ze si neprejeme spoustet konfiguracni AP (uziva se misto parametru timeout). Urceno pro bateriove napajene pristroje
 | 
			
		||||
 | 
			
		||||
// Struktura konfigurace, ulozena v EEPROM
 | 
			
		||||
typedef struct {
 | 
			
		||||
    uint8_t mode; // rezim prace AP/STA
 | 
			
		||||
    uint8_t ip; // konfigurace ip (staticka/DHCP)
 | 
			
		||||
    char ssid[32 + 2]; // SSID site
 | 
			
		||||
    char pass[64 + 2]; // heslo
 | 
			
		||||
    uint32_t ipaddr; // ip adresa v pripade staticke konfigurace
 | 
			
		||||
    uint32_t netmask; // sitova maska v pripade staticke konfigurace
 | 
			
		||||
    uint32_t gateway; // sitova brana v pripade staticke konfigurace
 | 
			
		||||
    uint32_t dns; // ip adresa DNS serveru v pripade staticke konfigurace
 | 
			
		||||
    uint8_t apchannel; // kanal, na kterem pracuje AP (pokud je zapnuty rezim AP)
 | 
			
		||||
} wificonfigarea_t;
 | 
			
		||||
 | 
			
		||||
// Parametr, predany uzivatelske callback funkci, urceny pro aplikacni vizualizaci stavu konfigurace a pripojeni
 | 
			
		||||
typedef enum {
 | 
			
		||||
    WCS_CONNECTSTART = 0, // zacatek pokusu o pripojeni k ulozene konfiguraci
 | 
			
		||||
@@ -100,24 +85,6 @@ typedef void (*storeparam_cb)(const char *newvalue); // callback pro ulozeni uzi
 | 
			
		||||
# define SETUP_CHANNEL 3
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *  \brief Ziskani retezce z EEPROM ze zadaneho offsetu
 | 
			
		||||
 *
 | 
			
		||||
 *  \param [in] start Ofset zacatku retezce
 | 
			
		||||
 *  \param [in] len Delka retezce
 | 
			
		||||
 *  \return Vycteny retezec
 | 
			
		||||
 */
 | 
			
		||||
String getEEPROMString(unsigned int start, size_t len);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *  \brief Ulozeni retezce do EEPROM
 | 
			
		||||
 *
 | 
			
		||||
 *  \param [in] start Ofset zacatku ukladani
 | 
			
		||||
 *  \param [in] len Maximalni delka ulozeneho retezce
 | 
			
		||||
 *  \param [in] string Ukladany retezec
 | 
			
		||||
 */
 | 
			
		||||
void setEEPROMString(unsigned int start, size_t len, String &string);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *  \brief Ziskani nasi ip adresy
 | 
			
		||||
 *
 | 
			
		||||
@@ -135,23 +102,6 @@ IPAddress getOurIP(void);
 | 
			
		||||
 */
 | 
			
		||||
uint8_t * getOurMAC(uint8_t *mac);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief      Ziskani 32 bitoveho cisla z EEPROM
 | 
			
		||||
 *
 | 
			
		||||
 * @param[in]  start  Pocatecni adresa, ze ktere se ziskava cislo.
 | 
			
		||||
 *
 | 
			
		||||
 * @return     Ziskane cislo.
 | 
			
		||||
 */
 | 
			
		||||
uint32_t getEEPROMuint32(int start);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief      Ulozeni 32 bitoveho cisla do EEPROM
 | 
			
		||||
 *
 | 
			
		||||
 * @param[in]  start  Pocatecni adresa, kam cislo ukladame.
 | 
			
		||||
 * @param[in]  val    Ukladana hodnota
 | 
			
		||||
 */
 | 
			
		||||
void setEEPROMuint32(int start, uint32_t val);
 | 
			
		||||
 | 
			
		||||
class WiFiConfigUsrParameter
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user