From b608d3609642fe3b97fcf1f935daea82c19018df Mon Sep 17 00:00:00 2001 From: Pablo2048 Date: Thu, 4 Mar 2021 17:36:43 +0100 Subject: [PATCH 1/3] Upravy pro praci se sysvars --- src/WiFiConfig.cpp | 234 +++++++++++++++++++++------------------------ src/WiFiConfig.h | 50 ---------- 2 files changed, 108 insertions(+), 176 deletions(-) diff --git a/src/WiFiConfig.cpp b/src/WiFiConfig.cpp index 46824ce..aa9e437 100644 --- a/src/WiFiConfig.cpp +++ b/src/WiFiConfig.cpp @@ -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 // duvod resetu (https://github.com/espressif/arduino-esp32/issues/449) #endif #include "WiFiConfig.h" -#include #include -#include +#include #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; #if defined(ESP8266) static std::unique_ptr 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(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(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(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(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(F("ip"), nullptr, F(WIFICFG_FILE))).toString()); + s.replace(F("{m}"), IPAddress(svGetV(F("mask"), nullptr, F(WIFICFG_FILE))).toString()); + s.replace(F("{g}"), IPAddress(svGetV(F("gate"), nullptr, F(WIFICFG_FILE))).toString()); + s.replace(F("{d}"), IPAddress(svGetV(F("dns"), nullptr, F(WIFICFG_FILE))).toString()); - uint8_t chan = EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)); + uint8_t chan = svGetV(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(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(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(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(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(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(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(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(F("ssid"), nullptr, F(WIFICFG_FILE)); + s.decode(); + encString pass = svGetV(F("pass"), nullptr, F(WIFICFG_FILE)); + pass.decode(); - switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) { + switch (svGetV(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(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(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV(F("gw"), nullptr, F(WIFICFG_FILE))), + IPAddress(svGetV(F("mask"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV(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(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(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(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV(F("gw"), nullptr, F(WIFICFG_FILE))), + IPAddress(svGetV(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(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(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(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(F("ipmode"), nullptr, F(WIFICFG_FILE))) { + + DEBUG_MSG("Static configuration SET.\r\n"); + WiFi.config(IPAddress(svGetV(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV(F("gw"), nullptr, F(WIFICFG_FILE))), + IPAddress(svGetV(F("mask"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV(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(F("ipmode"), nullptr, F(WIFICFG_FILE))) { + WiFi.softAPConfig(IPAddress(svGetV(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV(F("gw"), nullptr, F(WIFICFG_FILE))), + IPAddress(svGetV(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(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(F("ssid"), es, F(WIFICFG_FILE)); + es = pass; + es.encrypt(); + svSetV(F("pass"), es, F(WIFICFG_FILE)); + svSetV(F("mode"), mode, F(WIFICFG_FILE)); + svSetV(F("ipmode"), ipcfg, F(WIFICFG_FILE)); + svSetV(F("channel"), 3, F(WIFICFG_FILE)); + svSetV(F("ip"), (uint32_t)ip, F(WIFICFG_FILE)); + svSetV(F("mask"), (uint32_t)mask, F(WIFICFG_FILE)); + svSetV(F("gw"), (uint32_t)gw, F(WIFICFG_FILE)); + svSetV(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(F("ssid"), nullptr, F(WIFICFG_FILE)); + s.decode(); + encString pass = svGetV(F("pass"), nullptr, F(WIFICFG_FILE)); + pass.decode(); + int mode = svGetV(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(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(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV(F("gw"), nullptr, F(WIFICFG_FILE))), + IPAddress(svGetV(F("mask"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV(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(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(F("ipmode"), nullptr, F(WIFICFG_FILE))) { + delay(100); // kvuli ESP32 - cekame na start AP + WiFi.softAPConfig(IPAddress(svGetV(F("ip"), nullptr, F(WIFICFG_FILE))), IPAddress(svGetV(F("gw"), nullptr, F(WIFICFG_FILE))), + IPAddress(svGetV(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 diff --git a/src/WiFiConfig.h b/src/WiFiConfig.h index f4f644c..a610b6b 100644 --- a/src/WiFiConfig.h +++ b/src/WiFiConfig.h @@ -37,23 +37,8 @@ #endif #include -#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: From 1106aa060a6fadaf54b7779e6c8ba04d15cfff9c Mon Sep 17 00:00:00 2001 From: Pavel Brychta Date: Fri, 5 Mar 2021 07:41:14 +0100 Subject: [PATCH 2/3] Zprovozneni prikladu --- .../WiFiConfig_simple_test.ino | 53 +++++-------------- library.json | 2 +- library.properties | 2 +- src/WiFiConfig.cpp | 50 ++++++++--------- src/WiFiConfig.h | 8 ++- 5 files changed, 42 insertions(+), 73 deletions(-) diff --git a/examples/WiFiConfig_simple_test/WiFiConfig_simple_test.ino b/examples/WiFiConfig_simple_test/WiFiConfig_simple_test.ino index 68dff52..f595a91 100644 --- a/examples/WiFiConfig_simple_test/WiFiConfig_simple_test.ino +++ b/examples/WiFiConfig_simple_test/WiFiConfig_simple_test.ino @@ -8,11 +8,8 @@ # include #endif #include -#include #include -#include - -#define EEPROM_MAGIC 0xdeadbeef +#include // Plati pro desticku Witty s modulem ESP-12E #define PIN_FORCE_CONFIG 4 @@ -20,34 +17,24 @@ //#define DEBUG_OUT(a) {} #define DEBUG_OUT(a) Serial.print(a) -// Definice obsazeni EEPROM -#define elementSize(type, element) sizeof(((type *)0)->element) -#define countof(a) (sizeof(a) / sizeof(a[0])) -typedef struct { - uint32_t magic; // kontrola spravneho obsahu EEPROM - char devname[32 + 2]; // jmeno zarizeni (pro NBNS plati jen 16 znaku) -} eepromconfig_t; - -#define EEPROM_SIZE (sizeof(wificonfigarea_t) + sizeof(eepromconfig_t) + 10) // velikost EEPROM oblasti (POZOR!!! zbytecnym zvetsovanim se zaroven zmensuje velikost RAM kvuli zrcadlu!!!) -#define EEPROM_WIFICONFIG_ORIGIN (EEPROM_SIZE - sizeof(wificonfigarea_t)) // pocatek oblasti, pouzivane WiFiConfig (alokovano odzadu) -#define EEPROM_CONFIG_ORIGIN (0) - -char WiFiDeviceName[elementSize(eepromconfig_t, devname)]; // misto pro jmeno zarizeni (dodane do DNS, DHCP NBNS apod...) +char WiFiDeviceName[32]; // misto pro jmeno zarizeni (dodane do DNS, DHCP NBNS apod...) #if defined(ARDUINO_ARCH_ESP8266) ESP8266WebServer wwwserver(80); // webovy server #else WebServer wwwserver(80); // webovy server #endif -String content; + +static char MAINPAGE[] PROGMEM = R"=====( + + Hello world from ESP8266 +

+ +)====="; static void handleRoot(void) { - content = F("\nHello world from ESP8266"); - content += F("

"); - content += F(""); - - wwwserver.send(200, "text/html", content); + wwwserver.send_P(200, "text/html", MAINPAGE); } void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state) @@ -82,7 +69,7 @@ void ICACHE_FLASH_ATTR saveDevname(const char *param) { String p = param; - EES_storeString(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, devname), elementSize(eepromconfig_t, devname), p); + svSetV(F("devname"), p); } void ICACHE_FLASH_ATTR setup() @@ -90,28 +77,16 @@ void ICACHE_FLASH_ATTR setup() WiFiConfig wifi; // konfigurace ESP modulu WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname); - EEPROM.begin(EEPROM_SIZE); // zahajujeme praci s EEPROM - - uint32_t magic; - - EEPROM.get(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, magic), magic); - if (EEPROM_MAGIC != magic) { - // EEPROM ma spatny obsah - for (unsigned int i = 0; i < EEPROM_SIZE; i++) - EEPROM.write(i, 0); - magic = EEPROM_MAGIC; - EEPROM.put(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, magic), magic); - EEPROM.commit(); - } + LittleFS.begin(); pinMode(PIN_FORCE_CONFIG, INPUT_PULLUP); // pin, co slouzi jako vstup tlacitka int fc = digitalRead(PIN_FORCE_CONFIG); // pozadavek na vynucene vyvolani konfigurace Serial.begin(115200); - String dn = EES_readString(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, devname), elementSize(eepromconfig_t, devname)); + String dn = svGetV(F("devname")); strcpy(WiFiDeviceName, dn.c_str()); wifi.addParameter(&devname); - if (WCR_OK != wifi.begin(EEPROM_WIFICONFIG_ORIGIN, fc, wcb)) // startujeme pripojeni + if (WCR_OK != wifi.begin(fc, wcb)) // startujeme pripojeni ESP.restart(); wwwserver.on("/", handleRoot); diff --git a/library.json b/library.json index dcb5b20..8e94e8b 100644 --- a/library.json +++ b/library.json @@ -12,7 +12,7 @@ "type": "git", "url": "https://git.xpablo.cz/pablo2048/WiFiConfig.git" }, - "version": "6.4.0", + "version": "7.0.0", "license": "MIT", "frameworks": "arduino", "platforms": ["espressif8266", "espressif32"], diff --git a/library.properties b/library.properties index f6bd1e8..914682c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=WiFiConfig -version=6.4.0 +version=7.0.0 author=Pavel Brychta maintainer=Pavel Brychta sentence=Enables seamless module configuration. diff --git a/src/WiFiConfig.cpp b/src/WiFiConfig.cpp index aa9e437..524778a 100644 --- a/src/WiFiConfig.cpp +++ b/src/WiFiConfig.cpp @@ -171,7 +171,7 @@ IPAddress getOurIP(void) uint8_t * getOurMAC(uint8_t *mac) { - if (WIFIMODE_STA == EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) + if (WIFIMODE_STA == svGetV(F("mode"), nullptr, F(WIFICFG_FILE))) return WiFi.macAddress(mac); else return WiFi.softAPmacAddress(mac); @@ -427,7 +427,7 @@ void WiFiConfig::_handleRoot(void) void WiFiConfig::_handleDisplayAP(void) { String s; - encString v; + String v; String content; _time = millis() + (360 * 1000); // spocitame si novy cas, kdy budeme modul restartovat (6 minut) @@ -459,13 +459,13 @@ void WiFiConfig::_handleDisplayAP(void) } } s = FPSTR(PAGE_INDEX2); - v = svGetV(F("ssid"), nullptr, F(WIFICFG_FILE)); - v.decode(); + v = svGetV(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 = svGetV(F("pass"), nullptr, F(WIFICFG_FILE)); + v = svGetV(F("pass"), nullptr, F(WIFICFG_FILE)); if (v.length()) s.replace(F("{p}"), String(FPSTR(VALUE)) + v + F("'")); else @@ -519,16 +519,12 @@ void WiFiConfig::_handleSetAP(void) str.reserve(128); str = server->arg(F("_s")); if (str.length() > 0) { - encString dummy; - - dummy = str; - dummy.encode(); - svSetV(F("ssid"), dummy, F(WIFICFG_FILE)); + strEncode(str); + svSetV(F("ssid"), str, F(WIFICFG_FILE)); str = server->arg(F("_p")); - dummy = str; - dummy.encode(); - svSetV(F("pass"), dummy, F(WIFICFG_FILE)); + strEncode(str); + svSetV(F("pass"), str, F(WIFICFG_FILE)); str = server->arg(F("_a")); if (str.length() > 0) { @@ -695,9 +691,9 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb) // nakonfigurujeme ESP dle nove nastavenych parametru WiFi.disconnect(); // vsechno odpojime - encString s = svGetV(F("ssid"), nullptr, F(WIFICFG_FILE)); + encString s = svGetV(F("ssid"), nullptr, F(WIFICFG_FILE)); s.decode(); - encString pass = svGetV(F("pass"), nullptr, F(WIFICFG_FILE)); + encString pass = svGetV(F("pass"), nullptr, F(WIFICFG_FILE)); pass.decode(); switch (svGetV(F("mode"), nullptr, F(WIFICFG_FILE))) { @@ -773,13 +769,12 @@ 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; @@ -843,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)) @@ -853,19 +848,19 @@ 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(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; + String es; DEBUG_MSG("initConfig %d\r\n", mode); es = ssid; - es.encrypt(); + strEncode(es); svSetV(F("ssid"), es, F(WIFICFG_FILE)); es = pass; - es.encrypt(); + strEncode(es); svSetV(F("pass"), es, F(WIFICFG_FILE)); svSetV(F("mode"), mode, F(WIFICFG_FILE)); svSetV(F("ipmode"), ipcfg, F(WIFICFG_FILE)); @@ -884,10 +879,10 @@ void WiFiConfig::_prepareWifi(void) WiFi.disconnect(); // vsechno odpojime delay(100); - encString s = svGetV(F("ssid"), nullptr, F(WIFICFG_FILE)); - s.decode(); - encString pass = svGetV(F("pass"), nullptr, F(WIFICFG_FILE)); - pass.decode(); + String s = svGetV(F("ssid"), nullptr, F(WIFICFG_FILE)); + strDecode(s); + String pass = svGetV(F("pass"), nullptr, F(WIFICFG_FILE)); + strDecode(pass); int mode = svGetV(F("mode"), nullptr, F(WIFICFG_FILE)); switch (mode) { @@ -925,7 +920,7 @@ void WiFiConfig::_prepareWifi(void) } break; - case WIFIMODE_AP: + case WIFIMODE_AP:{ DEBUG_MSG("AP mode.\r\n"); if (s.endsWith(F("?"))) { @@ -949,6 +944,7 @@ void WiFiConfig::_prepareWifi(void) IPAddress(svGetV(F("mask"), nullptr, F(WIFICFG_FILE)))); } break; + } default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???) diff --git a/src/WiFiConfig.h b/src/WiFiConfig.h index a610b6b..89ae6dc 100644 --- a/src/WiFiConfig.h +++ b/src/WiFiConfig.h @@ -130,10 +130,10 @@ class WiFiConfig { public: WiFiConfig(): _params(NULL), _timeout(0) {} - wificonfigresult_t begin(int configarea, int forceConfigure, wificonfig_cb cb); - wificonfigresult_t begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb); + wificonfigresult_t begin(int forceConfigure, wificonfig_cb cb); + wificonfigresult_t begin(int forceConfigure, int timeout, wificonfig_cb cb); void addParameter(WiFiConfigUsrParameter *p); - 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); + void initConfig(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(void); @@ -145,8 +145,6 @@ private: void _handleNotFound(); // CaptivePortal redirector void _handleRoot(); // jen jednoducha stranka kvuli CaptivePortalu umoznuje prejit na spravnou stranku (ale nedela to...) bool _testWifi(wificonfig_cb cb); - bool _storeString(unsigned int start, size_t maxlen, String &string); // sifrovany retezec, max 255 znaku - String _readString(unsigned int start, size_t maxlen); // sifrovany retezec, max. 255 znaku WiFiConfigUsrParameter *_searchUsrParameter(const char *name); WiFiConfigUsrParameter *_params; // ukazatel na posledni zadany uzivatelsky parametr From 3f433aedc8446bd158c10cfd6cac88fc2f140c87 Mon Sep 17 00:00:00 2001 From: Pavel Brychta Date: Fri, 5 Mar 2021 07:51:20 +0100 Subject: [PATCH 3/3] Lepsi popis chyby konfigurace --- src/WiFiConfig.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WiFiConfig.cpp b/src/WiFiConfig.cpp index 524778a..f88f7b1 100644 --- a/src/WiFiConfig.cpp +++ b/src/WiFiConfig.cpp @@ -388,7 +388,7 @@ void WiFiConfig::_handleRoot(void) break; case 0xfe: - content.concat(F("Špatná EEPROM")); + content.concat(F("Špatná konfigurace")); break; case 0xff: