Merge pull request 'LittleFS' (#10) from LittleFS into master

Reviewed-on: #10
This commit is contained in:
Pavel Brychta 2021-06-21 17:19:55 +02:00
commit 5c382257e9
5 changed files with 132 additions and 231 deletions

View File

@ -8,11 +8,8 @@
# include <NetBIOS.h> # include <NetBIOS.h>
#endif #endif
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <EEPROM.h>
#include <WiFiConfig.h> #include <WiFiConfig.h>
#include <encipheredEEPROMStrings.h> #include <sysvars.hpp>
#define EEPROM_MAGIC 0xdeadbeef
// Plati pro desticku Witty s modulem ESP-12E // Plati pro desticku Witty s modulem ESP-12E
#define PIN_FORCE_CONFIG 4 #define PIN_FORCE_CONFIG 4
@ -20,34 +17,24 @@
//#define DEBUG_OUT(a) {} //#define DEBUG_OUT(a) {}
#define DEBUG_OUT(a) Serial.print(a) #define DEBUG_OUT(a) Serial.print(a)
// Definice obsazeni EEPROM char WiFiDeviceName[32]; // misto pro jmeno zarizeni (dodane do DNS, DHCP NBNS apod...)
#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...)
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
ESP8266WebServer wwwserver(80); // webovy server ESP8266WebServer wwwserver(80); // webovy server
#else #else
WebServer wwwserver(80); // webovy server WebServer wwwserver(80); // webovy server
#endif #endif
String content;
static char MAINPAGE[] PROGMEM = R"=====(<!DOCTYPE HTML>
<html>
Hello world from ESP8266
<p>
</html>
)=====";
static void handleRoot(void) static void handleRoot(void)
{ {
content = F("<!DOCTYPE HTML>\n<html>Hello world from ESP8266"); wwwserver.send_P(200, "text/html", MAINPAGE);
content += F("<p>");
content += F("</html>");
wwwserver.send(200, "text/html", content);
} }
void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state) void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state)
@ -82,7 +69,7 @@ void ICACHE_FLASH_ATTR saveDevname(const char *param)
{ {
String p = 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() void ICACHE_FLASH_ATTR setup()
@ -90,28 +77,16 @@ void ICACHE_FLASH_ATTR setup()
WiFiConfig wifi; // konfigurace ESP modulu WiFiConfig wifi; // konfigurace ESP modulu
WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname); WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname);
EEPROM.begin(EEPROM_SIZE); // zahajujeme praci s EEPROM LittleFS.begin();
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();
}
pinMode(PIN_FORCE_CONFIG, INPUT_PULLUP); // pin, co slouzi jako vstup tlacitka pinMode(PIN_FORCE_CONFIG, INPUT_PULLUP); // pin, co slouzi jako vstup tlacitka
int fc = digitalRead(PIN_FORCE_CONFIG); // pozadavek na vynucene vyvolani konfigurace int fc = digitalRead(PIN_FORCE_CONFIG); // pozadavek na vynucene vyvolani konfigurace
Serial.begin(115200); Serial.begin(115200);
String dn = EES_readString(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, devname), elementSize(eepromconfig_t, devname)); String dn = svGetV<String>(F("devname"));
strcpy(WiFiDeviceName, dn.c_str()); strcpy(WiFiDeviceName, dn.c_str());
wifi.addParameter(&devname); 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(); ESP.restart();
wwwserver.on("/", handleRoot); wwwserver.on("/", handleRoot);

View File

@ -12,7 +12,7 @@
"type": "git", "type": "git",
"url": "https://git.xpablo.cz/pablo2048/WiFiConfig.git" "url": "https://git.xpablo.cz/pablo2048/WiFiConfig.git"
}, },
"version": "6.4.0", "version": "7.0.0",
"license": "MIT", "license": "MIT",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": ["espressif8266", "espressif32"], "platforms": ["espressif8266", "espressif32"],

View File

@ -1,5 +1,5 @@
name=WiFiConfig name=WiFiConfig
version=6.4.0 version=7.0.0
author=Pavel Brychta author=Pavel Brychta
maintainer=Pavel Brychta <pablo@xpablo.cz> maintainer=Pavel Brychta <pablo@xpablo.cz>
sentence=Enables seamless module configuration. sentence=Enables seamless module configuration.

View File

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

View File

@ -37,23 +37,8 @@
#endif #endif
#include <memory> #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 #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 // Parametr, predany uzivatelske callback funkci, urceny pro aplikacni vizualizaci stavu konfigurace a pripojeni
typedef enum { typedef enum {
WCS_CONNECTSTART = 0, // zacatek pokusu o pripojeni k ulozene konfiguraci 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 # define SETUP_CHANNEL 3
#endif #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 * \brief Ziskani nasi ip adresy
* *
@ -135,23 +102,6 @@ IPAddress getOurIP(void);
*/ */
uint8_t * getOurMAC(uint8_t *mac); 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 class WiFiConfigUsrParameter
{ {
public: public:
@ -180,10 +130,10 @@ class WiFiConfig
{ {
public: public:
WiFiConfig(): _params(NULL), _timeout(0) {} WiFiConfig(): _params(NULL), _timeout(0) {}
wificonfigresult_t begin(int configarea, int forceConfigure, wificonfig_cb cb); wificonfigresult_t begin(int forceConfigure, wificonfig_cb cb);
wificonfigresult_t begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb); wificonfigresult_t begin(int forceConfigure, int timeout, wificonfig_cb cb);
void addParameter(WiFiConfigUsrParameter *p); 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: private:
void _prepareWifi(void); void _prepareWifi(void);
@ -195,8 +145,6 @@ private:
void _handleNotFound(); // CaptivePortal redirector void _handleNotFound(); // CaptivePortal redirector
void _handleRoot(); // jen jednoducha stranka kvuli CaptivePortalu umoznuje prejit na spravnou stranku (ale nedela to...) void _handleRoot(); // jen jednoducha stranka kvuli CaptivePortalu umoznuje prejit na spravnou stranku (ale nedela to...)
bool _testWifi(wificonfig_cb cb); 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 *_searchUsrParameter(const char *name);
WiFiConfigUsrParameter *_params; // ukazatel na posledni zadany uzivatelsky parametr WiFiConfigUsrParameter *_params; // ukazatel na posledni zadany uzivatelsky parametr