Zaklad pro verzi 7.x.x - Pouziva knihovnu sysvars, takze neni zavisla na zadnem druhu souboroveho systemu

This commit is contained in:
Pavel Brychta 2021-06-21 17:22:19 +02:00
parent 5c382257e9
commit 8e857a1ae2
4 changed files with 524 additions and 499 deletions

91
history.txt Normal file
View File

@ -0,0 +1,91 @@
/*
* 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
*
* v6.3.4-23.3.2020 - staticka konfigurace v AP rezimu funkcni
*
* v6.3.3-27.10.2019 - volani WS_CONNECTED i v pripade, ze bezi AP
*
* v6.3.2- 4.10.2019 - Informace o aplikaci a jeji verzi do footeru, zrychlen restart z 10 na 5 sekund, vylepsena stranka pro reset
* verze pro ESP32 zobrazuje neco malo informaci o stavu pripojeni a duvodu vstupu do WifiConfig
*
* V6.3.1- 1.9.2019 - Odstraneni HTML5 validatoru - delaly akorat problemy a nedala se ulozit konfigurace i kdyz byly polozky skryty
*
* V6.3 - 12.8.2019 - IP adresa Captive portalu je zmenena na 172.217.28.1 kvuli funkcnosti na Android zarizenich (viz. zmena zde https://github.com/esp8266/Arduino/blob/master/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino)
* ladici vypis pri zachyceni presmerovani v Captive portalu.
*
* V6.2 - 12.12.2018- Nastaveni jmena zarizeni nadale NENI soucasti WiFiConfig a je treba ho pridat jako uzivatelsky parametr. Je to proto, protoze
* se ukazalo, ze neni dobre z externiho kodu sahat do vnitrni struktury WiFiConfig (napr. pri ukladani noveho jmena).
*
* V6.1 - 11.12.2018- Sifrovani retezcu vytazeno mimo WiFiConfig do knihovny encipheredEEPROMStrings (dostupna na gitu) kvuli pristupu i jinych modulu
*
* V6.0 - 20.11.2018- Verze i pro ESP32
*
* V5.9 - 18.7.2018 - Retezce, ukladane do EEPROM jsou primitivnim zpusobem sifrovany pro ztizeni odhaleni SSID a hesla pri dumpu pameti
*
* V5.8 - 5.7.2018 - Opraveno spatne zobrazovani sily signalu v prehledu WiFi siti (spatne barvy)
*
* V5.7 - 4.7.2018 - BugFix - staticka konfigurace ip adresy se neuplatnila - vypada to, jako kdyby SDK ukladalo SSID a Password, ale uz neuklada
* konfiguraci ip adres, takze je treba je vzdycky nastavit znovu
*
* V5.6 - 1.7.2018 - BugFix - odstraneno volani _plusDecode, protoze parametry jsou nyni predavany v 'ciste' forme uz z weboveho serveru a tato metoda znehodnotila retezce, ktere obsahovaly ynak '+'
*
* V5.5 - 3.6.2018 - Flash Chip ID a ESP Chip ID jsou zobrazeny hexadecimalne v prehledove strance
*
* V5.4 - 9.1.2018 - na titulni stranku pridan duvod, proc je spusteny konfiguracni AP. Usnadni to diagnostiku pripadu, kdy se ESP nechce pripojit k AP.
*
* V5.3 - 8.12.2017 - prepracovano presmerovani v Captive portalu, DEBUG_MSG makro upraveno na posledni pouzivanou verzi, scitani retezcu prepracovano na .concat. Pripsany Informace o modulu, reakce na Reset
*
* V5.2 - 20.4.2017 - Pokud je pouzite v timeoutu WC_DONT_RUN_CONFIGAP tak se vracime okamzite a necekame na pripojeni k WiFi (take nevolame zadnou callback metodu).
* Navratovy kod je pak WCR_CONFIGAP_NOT_STARTED .
*
* V5.1 - 3.4.2017 - Webove rozhrani upravene pro snadnejsi ovladani na telefonu/tabletu, sila signalu AP zobrazena jako barevne odliseny indikator.
*
* V5.0 - 29.10.2016- Vyuziti vlastnosti ESP, ktere si uklada wifi konfiguraci do sve vlastni flash oblasti, takze nemusim vse konfigurovat znovu po startu - doufam, ze se tim zrychli asociace s AP
* kvuli bateriove napajenym pristrojum.
*
* V4.7 - 30.8.2016 - Metody .begin se nyni vraci s wificonfigresult_t (podarilo se pripojit k AP, nepodarilo se pripojit a vyprsel timeout pro konfiguracni AP, nepodarilo se pripojit, ale je zakazane spousteni konfiguracniho AP).
* ESP NYNI NENI RESETOVAN PO VYPRSENI TIMEOUTU!!!! Je to kvuli bateriovym cidlum a DeepSleep rezimu v situaci, kdy zmizi AP, ke kteremu se bezne pripojujeme
* Odstraneno odesilani reakce na generate_204 (viz. https://github.com/tzapu/WiFiManager/issues/114)
* Zbytek MAC adresy v pripadnem SSID je nyni vzdy velkymi pismeny (UAA Unique Address Assignment)
* Kvalita signalu jednotlivych AP je nyni uvadena v % namisto uzivatelum nic nerikajicich -dBm
* Pokud do polozky timeout v .begin() metode zadame hodnotu WC_DONT_RUN_CONFIGAP, tak i po neuspesnem pokusu o pripojeni do site NENI spusteny konfiguracni AP (bateriove pristroje, stale bezici pristroje)
*
* V4.6 - 26.7.2016 - Moznost nastavit cislo kanalu pro rezim AP, nove eye-candy rozhrani.
*
* V4.5 - 20.7.2016 - Moznost konfigurovat cislo kanalu pro nastavovaci AP (default je 3), po nacteni konfiguracni stranky je nastaveny timeout na 6 minut, pokud je ve jmenu SSID pro konfiguracni
* AP znak ? (otaznik), tak je tento nahrazen HEXASCII reprezentaci 3 poslednich bytu MAC adresy AP (POZOR!!! je jina, nez STA!!!)
*
* V4.4 - 10.7.2016 - Zmena nazvu vsech privatnich metod (doplnene uvodni podtrzitko _). Pokud tam nebylo, tak napriklad nesel prelozit skript s handleNotFound pokud nebyl dopredne deklarovany (asi zmatek v Arduino preprocesoru)
*
* V4.3 - 23.6.2016 - Kazde nacteni webove stranky prodluzuje pripadny timeout pro restart zarizeni.
*
* V4.2 - 10.6.2016 - DNSServer a WebServer ukazatele jsou nyni staticke primo v modulu WiFiConfig. Predchozi verze neumoznila soucasne pouziti s knihovnami ESPAsyncWebServer z duvodu kolize nekterych definic HTTP metod
*
* V4.1 - 18.5.2016 - Nenechame SDK ukladat WiFi konfiguraci do Flash (setrime prepisy)
*
* V4.0 - 10.4.2016 - pridana moznost staticke konfigurace ip parametru. POZOR!!!! ukladani dat do EEPROM (verejne funkce) nyni nevolaji automaticky commit() kvuli snizeni poctu prepisu Flash EEPROM
* pridana moznost timeoutu pro konfiguracni AP. Pokud timeout vyprsi, tak se zarizeni automaticky restartuje, coz vyvola novy pokus o pripojeni.
*
* V3.0 - 27.2.2016 - pridana presmerovavaci stranka kvuli Captive portalu, doplneno servirovani favicon.ico, doplnena moznost zadavani uzivatelskych parametru, www server a DNS server jsou dynamicky vytvareny
*
* V2.4 - 7.2.2016 - upraveno casovani pri pripojovani k AP (zmizel delay(100))
*
* V2.3 - 5.2.2016 - pridano zverejneni jmena hosta a nastaveni tohoto jmena pro DHCP apod.
*
* V2.2 - 19.1.2016 - pridan Captive portal pri konfiguracnim AP.
*
* V2.1 - 20.9.2015 - pridano zadavani jmena zarizeni. Modul ho sice nevyuziva, ale aplikace ano a v pripade DHCP se dost hodi.
*
* V2.0 - 30.8.2015 - Podstatnym zpusobem vylepsena signalizace vnitrniho stavu, presunuto ulozeni rezimu (commit je uz zbytecny - udela se pri ukladani retezcu - uspora kodu/setreni prepisu pameti Flash)
*
* V1.2 - 23.8.2015 - BugFix - pridan chybejici eeprom.commit() po ulozeni rezimu prace
*
* V1.1 - 5.8.2015 - pridana moznost volani callback metody pri behu konfiguracniho AP (signalizace stavu uzivatelskym zpusobem)
*
* V1.0 - publikace na www.xpablo.cz
*
*/

View File

@ -1,112 +1,10 @@
/* #if defined(ESP32)
* 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
*
* v6.3.4-23.3.2020 - staticka konfigurace v AP rezimu funkcni
*
* v6.3.3-27.10.2019 - volani WS_CONNECTED i v pripade, ze bezi AP
*
* v6.3.2- 4.10.2019 - Informace o aplikaci a jeji verzi do footeru, zrychlen restart z 10 na 5 sekund, vylepsena stranka pro reset
* verze pro ESP32 zobrazuje neco malo informaci o stavu pripojeni a duvodu vstupu do WifiConfig
*
* V6.3.1- 1.9.2019 - Odstraneni HTML5 validatoru - delaly akorat problemy a nedala se ulozit konfigurace i kdyz byly polozky skryty
*
* V6.3 - 12.8.2019 - IP adresa Captive portalu je zmenena na 172.217.28.1 kvuli funkcnosti na Android zarizenich (viz. zmena zde https://github.com/esp8266/Arduino/blob/master/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino)
* ladici vypis pri zachyceni presmerovani v Captive portalu.
*
* V6.2 - 12.12.2018- Nastaveni jmena zarizeni nadale NENI soucasti WiFiConfig a je treba ho pridat jako uzivatelsky parametr. Je to proto, protoze
* se ukazalo, ze neni dobre z externiho kodu sahat do vnitrni struktury WiFiConfig (napr. pri ukladani noveho jmena).
*
* V6.1 - 11.12.2018- Sifrovani retezcu vytazeno mimo WiFiConfig do knihovny encipheredEEPROMStrings (dostupna na gitu) kvuli pristupu i jinych modulu
*
* V6.0 - 20.11.2018- Verze i pro ESP32
*
* V5.9 - 18.7.2018 - Retezce, ukladane do EEPROM jsou primitivnim zpusobem sifrovany pro ztizeni odhaleni SSID a hesla pri dumpu pameti
*
* V5.8 - 5.7.2018 - Opraveno spatne zobrazovani sily signalu v prehledu WiFi siti (spatne barvy)
*
* V5.7 - 4.7.2018 - BugFix - staticka konfigurace ip adresy se neuplatnila - vypada to, jako kdyby SDK ukladalo SSID a Password, ale uz neuklada
* konfiguraci ip adres, takze je treba je vzdycky nastavit znovu
*
* V5.6 - 1.7.2018 - BugFix - odstraneno volani _plusDecode, protoze parametry jsou nyni predavany v 'ciste' forme uz z weboveho serveru a tato metoda znehodnotila retezce, ktere obsahovaly ynak '+'
*
* V5.5 - 3.6.2018 - Flash Chip ID a ESP Chip ID jsou zobrazeny hexadecimalne v prehledove strance
*
* V5.4 - 9.1.2018 - na titulni stranku pridan duvod, proc je spusteny konfiguracni AP. Usnadni to diagnostiku pripadu, kdy se ESP nechce pripojit k AP.
*
* V5.3 - 8.12.2017 - prepracovano presmerovani v Captive portalu, DEBUG_MSG makro upraveno na posledni pouzivanou verzi, scitani retezcu prepracovano na .concat. Pripsany Informace o modulu, reakce na Reset
*
* V5.2 - 20.4.2017 - Pokud je pouzite v timeoutu WC_DONT_RUN_CONFIGAP tak se vracime okamzite a necekame na pripojeni k WiFi (take nevolame zadnou callback metodu).
* Navratovy kod je pak WCR_CONFIGAP_NOT_STARTED .
*
* V5.1 - 3.4.2017 - Webove rozhrani upravene pro snadnejsi ovladani na telefonu/tabletu, sila signalu AP zobrazena jako barevne odliseny indikator.
*
* V5.0 - 29.10.2016- Vyuziti vlastnosti ESP, ktere si uklada wifi konfiguraci do sve vlastni flash oblasti, takze nemusim vse konfigurovat znovu po startu - doufam, ze se tim zrychli asociace s AP
* kvuli bateriove napajenym pristrojum.
*
* V4.7 - 30.8.2016 - Metody .begin se nyni vraci s wificonfigresult_t (podarilo se pripojit k AP, nepodarilo se pripojit a vyprsel timeout pro konfiguracni AP, nepodarilo se pripojit, ale je zakazane spousteni konfiguracniho AP).
* ESP NYNI NENI RESETOVAN PO VYPRSENI TIMEOUTU!!!! Je to kvuli bateriovym cidlum a DeepSleep rezimu v situaci, kdy zmizi AP, ke kteremu se bezne pripojujeme
* Odstraneno odesilani reakce na generate_204 (viz. https://github.com/tzapu/WiFiManager/issues/114)
* Zbytek MAC adresy v pripadnem SSID je nyni vzdy velkymi pismeny (UAA Unique Address Assignment)
* Kvalita signalu jednotlivych AP je nyni uvadena v % namisto uzivatelum nic nerikajicich -dBm
* Pokud do polozky timeout v .begin() metode zadame hodnotu WC_DONT_RUN_CONFIGAP, tak i po neuspesnem pokusu o pripojeni do site NENI spusteny konfiguracni AP (bateriove pristroje, stale bezici pristroje)
*
* V4.6 - 26.7.2016 - Moznost nastavit cislo kanalu pro rezim AP, nove eye-candy rozhrani.
*
* V4.5 - 20.7.2016 - Moznost konfigurovat cislo kanalu pro nastavovaci AP (default je 3), po nacteni konfiguracni stranky je nastaveny timeout na 6 minut, pokud je ve jmenu SSID pro konfiguracni
* AP znak ? (otaznik), tak je tento nahrazen HEXASCII reprezentaci 3 poslednich bytu MAC adresy AP (POZOR!!! je jina, nez STA!!!)
*
* V4.4 - 10.7.2016 - Zmena nazvu vsech privatnich metod (doplnene uvodni podtrzitko _). Pokud tam nebylo, tak napriklad nesel prelozit skript s handleNotFound pokud nebyl dopredne deklarovany (asi zmatek v Arduino preprocesoru)
*
* V4.3 - 23.6.2016 - Kazde nacteni webove stranky prodluzuje pripadny timeout pro restart zarizeni.
*
* V4.2 - 10.6.2016 - DNSServer a WebServer ukazatele jsou nyni staticke primo v modulu WiFiConfig. Predchozi verze neumoznila soucasne pouziti s knihovnami ESPAsyncWebServer z duvodu kolize nekterych definic HTTP metod
*
* V4.1 - 18.5.2016 - Nenechame SDK ukladat WiFi konfiguraci do Flash (setrime prepisy)
*
* V4.0 - 10.4.2016 - pridana moznost staticke konfigurace ip parametru. POZOR!!!! ukladani dat do EEPROM (verejne funkce) nyni nevolaji automaticky commit() kvuli snizeni poctu prepisu Flash EEPROM
* pridana moznost timeoutu pro konfiguracni AP. Pokud timeout vyprsi, tak se zarizeni automaticky restartuje, coz vyvola novy pokus o pripojeni.
*
* V3.0 - 27.2.2016 - pridana presmerovavaci stranka kvuli Captive portalu, doplneno servirovani favicon.ico, doplnena moznost zadavani uzivatelskych parametru, www server a DNS server jsou dynamicky vytvareny
*
* V2.4 - 7.2.2016 - upraveno casovani pri pripojovani k AP (zmizel delay(100))
*
* V2.3 - 5.2.2016 - pridano zverejneni jmena hosta a nastaveni tohoto jmena pro DHCP apod.
*
* V2.2 - 19.1.2016 - pridan Captive portal pri konfiguracnim AP.
*
* V2.1 - 20.9.2015 - pridano zadavani jmena zarizeni. Modul ho sice nevyuziva, ale aplikace ano a v pripade DHCP se dost hodi.
*
* V2.0 - 30.8.2015 - Podstatnym zpusobem vylepsena signalizace vnitrniho stavu, presunuto ulozeni rezimu (commit je uz zbytecny - udela se pri ukladani retezcu - uspora kodu/setreni prepisu pameti Flash)
*
* V1.2 - 23.8.2015 - BugFix - pridan chybejici eeprom.commit() po ulozeni rezimu prace
*
* V1.1 - 5.8.2015 - pridana moznost volani callback metody pri behu konfiguracniho AP (signalizace stavu uzivatelskym zpusobem)
*
* V1.0 - publikace na www.xpablo.cz
*
* TODO:
* Prejit na pripojovani pomoci vnitrnich mechanizmu SDK ESP - zvysi se tim rychlost pripojeni kvuli bateriovym zarizenim
*/
#include <DNSServer.h>
#if defined(ESP8266)
# include <ESP8266WebServer.h>
#else
# include <WebServer.h>
# 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 <Ticker.h>
#include <sysvars.hpp> #include <sysvars.hpp>
#include "embHTML.h" #include "embHTML.h"
#include <encString.hpp>
#if not defined(WIFICFG_FILE)
# define WIFICFG_FILE "/wifi.json"
#endif
#if defined(ESP8266) #if defined(ESP8266)
// ESP8266 // ESP8266
@ -136,16 +34,9 @@ extern "C" {
extern char WiFiDeviceName[]; extern char WiFiDeviceName[];
static std::unique_ptr<DNSServer> dnsServer; static const char CHECKED[] PROGMEM = "checked='checked'";
#if defined(ESP8266)
static std::unique_ptr<ESP8266WebServer> server;
#else
static std::unique_ptr<WebServer> server;
#endif
static std::unique_ptr<Ticker> rsttick;
const char VALUE[] PROGMEM = "value='"; static bool _doReconfig = false; // TODO: mozna nejak elegantneji? Ale zase setrime RAM...
const char CHECKED[] PROGMEM = "checked='checked'";
IPAddress getOurIP(void) IPAddress getOurIP(void)
{ {
@ -254,288 +145,317 @@ void WiFiConfig::addParameter(WiFiConfigUsrParameter *p)
_params = p; _params = p;
} }
void WiFiConfig::_handleNotFound(void) String WiFiConfig::infoProcessor(const String & var)
{ {
DEBUG_MSG("Requested URI: %s\r\n", server->uri().c_str()); if (var == F("SDK")) {
#if defined(ESP8266)
return ESP.getFullVersion();
#else
// TODO
return ESP.getSdkVersion();
#endif
} else if (var == F("FS")) {
#if defined(ESP8266)
return String(ESP.getFlashChipRealSize() / 1024);
#else
// TODO
return String(ESP.getFlashChipSize());
#endif
} else if (var == F("SS")) {
return String(ESP.getSketchSize() / 1024);
} else if (var == F("FSS")) {
return String(ESP.getFreeSketchSpace() / 1024);
} else if (var == F("STAMAC")) {
uint8_t mac[6];
uint8_t* macread = WiFi.macAddress(mac);
char macaddress[20];
if (server->uri().endsWith(String(F("favicon.ico")))) { sprintf_P(macaddress, PSTR("%02x:%02x:%02x:%02x:%02x:%02x"), macread[0], macread[1], macread[2], macread[3], macread[4], macread[5]);
server->send_P(404, TEXTPLAIN, PSTR("Err")); return String(macaddress);
} else if (var == F("APMAC")) {
uint8_t mac[6];
char macaddress[20];
uint8_t *macread = WiFi.softAPmacAddress(mac);
sprintf_P(macaddress, PSTR("%02x:%02x:%02x:%02x:%02x:%02x"), macread[0], macread[1], macread[2], macread[3], macread[4], macread[5]);
return String(macaddress);
} else if (var == F("ESPID")) {
char buff[8];
sprintf_P(buff, PSTR("%06X"), ESP_getChipId());
return String(buff);
} else if (var == F("FID")){
#if defined(ESP8266)
char buff[10];
sprintf_P(buff, PSTR("%08X"), ESP.getFlashChipId());
return String(buff);
#endif
}
return String(F("???"));
}
String WiFiConfig::apProcessor(const String &var)
{
if (var == F("USER")) {
String result;
if (_params) {
WiFiConfigUsrParameter *up = _params;
char buff[128];
result.reserve(1024);
result = FPSTR(PAGE_PARAM_HDR);
while (NULL != up) {
sprintf_P(buff, PAGE_PARAM, up->getLabel(), up->getID(), up->getValueLength(), up->getValue());
result.concat(buff);
up = up->getNext();
}
}
return result;
} else if (var == F("SSID")) {
String result = svGetV<String>(F("ssid"), nullptr, F(WIFICFG_FILE));
strDecode(result);
return result;
} else if (var == F("PASS")) {
String result = svGetV<String>(F("pass"), nullptr, F(WIFICFG_FILE));
strDecode(result);
return result;
} else if (var == F("APMODE")) {
String result;
if (svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE)) == WIFIMODE_AP)
result = FPSTR(CHECKED);
return result;
} else if (var == F("APCHAN")) {
String result;
uint8_t chan = svGetV<int>(F("channel"), nullptr, F(WIFICFG_FILE));
if ((chan < 1) || (chan > 13))
chan = 1; // neplatne cislo kanalu nahradime nejnizsim
result = String(chan);
return result;
} else if (var == F("STATIC")) {
String result;
if (svGetV<int>(F("ipmode"), nullptr, F(WIFICFG_FILE)) == IPCONFIG_STATIC)
result = FPSTR(CHECKED);
return result;
} else if (var == F("IP")) {
String result;
result = _getIP(F("ip")).toString();
return result;
} else if (var == F("MASK")) {
String result;
result = _getIP(F("mask")).toString();
return result;
} else if (var == F("GATE")) {
String result;
result = _getIP(F("gate")).toString();
return result;
} else if(var == F("DNS")) {
String result;
result = _getIP(F("dns")).toString();
return result;
}
return String();
}
String WiFiConfig::rootProcessor(const String &var)
{
String content;
switch (_status) {
#if defined(ESP8266)
case STATION_IDLE:
content = F("Klid");
break;
case STATION_CONNECTING:
content = F("Připojování");
break;
case STATION_WRONG_PASSWORD:
content = F("Špatné heslo");
break;
case STATION_NO_AP_FOUND:
content = F("AP nenalezen");
break;
case STATION_CONNECT_FAIL:
content = F("Připojení selhalo");
break;
case STATION_GOT_IP:
content = F("Získaná ip");
break;
case 0xfe:
content = F("Špatná konfigurace");
break;
case 0xff:
content = F("Vynucená konfigurace");
break;
default:
content = F("Neznámý");
break;
#else
case WL_IDLE_STATUS:
content = F("Klid");
break;
case WL_NO_SSID_AVAIL:
content = F("AP nenalezen");
break;
case WL_CONNECT_FAILED:
content = F("Připojení selhalo");
break;
case 0xfe:
content = F("Špatná konfigurace");
break;
case 0xff:
content = F("Vynucená konfigurace");
break;
default:
content = F("Neznámý");
break;
#endif
}
return content;
}
void WiFiConfig::_handleNotFound(AsyncWebServerRequest *request)
{
DEBUG_MSG("Requested URI: %s\r\n", request->url().c_str());
if (request->url().endsWith(String(F("favicon.ico")))) {
request->send_P(404, TEXTPLAIN, PSTR("Err"));
} else { } else {
String redirect; String redirect;
redirect.reserve(256); redirect.reserve(256);
redirect = F("http://"); redirect = F("http://");
redirect.concat(server->client().localIP().toString()); redirect.concat(WiFi.softAPIP().toString());
redirect.concat(F("/index.htm")); redirect.concat(F("/index.htm"));
DEBUG_MSG("Redirection to: %s\r\n", redirect.c_str()); DEBUG_MSG("Redirection to: %s\r\n", redirect.c_str());
server->sendHeader(F("Location"), redirect, true); request->redirect(redirect);
server->send_P(302, TEXTPLAIN, PSTR("Redirect"));
} }
} }
void WiFiConfig::_handleReset(void) void WiFiConfig::_handleReset(AsyncWebServerRequest *request)
{ {
rsttick.reset(new(Ticker)); request->onDisconnect([]() {
rsttick->once_ms(700, []() {
ESP.restart(); ESP.restart();
}); });
server->send_P(200, TEXTHTML, PAGE_RESTART); request->send_P(200, TEXTHTML, PAGE_RESTART);
} }
void WiFiConfig::_handleInfo(void) void WiFiConfig::_handleScan(AsyncWebServerRequest *request)
{ {
String reply; String json;
char buff[32]; int n = WiFi.scanComplete();
reply.reserve(3000); json.reserve(1024);
json = F("{\"status\":");
reply = FPSTR(PAGE_INDEX1); json.concat(String(n));
reply.concat(F("<form><table><TR><TH>System Info<TH>")); json.concat(F(",\"ssids\":["));
if (n == WIFI_SCAN_FAILED) {
reply.concat(F("<TR><TD>Core Version:<TD>")); WiFi.scanNetworks(true);
#if defined(ESP8266) } else if (n) {
reply.concat(ESP.getFullVersion()); for (int i = 0; i < n; ++i) {
#else if (i)
// TODO json.concat(F(","));
reply.concat(ESP.getSdkVersion()); json.concat(("{\"rssi\":"));
#endif json.concat(String(WiFi.RSSI(i)));
json.concat(F(",\"ssid\":\""));
reply.concat(F("<TR><TD>Flash Size:<TD>")); json.concat(WiFi.SSID(i));
#if defined(ESP8266) json.concat(F("\",\"channel\":"));
reply.concat(ESP.getFlashChipRealSize() / 1024); json.concat(String(WiFi.channel(i)));
#else json.concat(F(",\"secure\":"));
// TODO json.concat(String(WiFi.encryptionType(i)));
reply.concat(ESP.getFlashChipSize()); json.concat(F("}"));
#endif }
reply.concat(F(" kB")); WiFi.scanDelete();
if (WiFi.scanComplete() == WIFI_SCAN_FAILED) {
#if defined(ESP8266) WiFi.scanNetworks(true);
reply.concat(F("<TR><TD>Sketch Size/Free:<TD>")); }
reply.concat(ESP.getSketchSize() / 1024); }
reply.concat(F(" kB / ")); json.concat(F("]}"));
reply.concat(ESP.getFreeSketchSpace() / 1024); request->send(200, FPSTR(TEXTJSON), json);
reply.concat(F(" kB"));
#endif
reply.concat(F("<TR><TD>STA MAC:<TD>"));
uint8_t mac[6];
uint8_t* macread = WiFi.macAddress(mac);
char macaddress[20];
sprintf_P(macaddress, PSTR("%02x:%02x:%02x:%02x:%02x:%02x"), macread[0], macread[1], macread[2], macread[3], macread[4], macread[5]);
reply.concat(macaddress);
reply.concat(F("<TR><TD>AP MAC:<TD>"));
macread = WiFi.softAPmacAddress(mac);
sprintf_P(macaddress, PSTR("%02x:%02x:%02x:%02x:%02x:%02x"), macread[0], macread[1], macread[2], macread[3], macread[4], macread[5]);
reply.concat(macaddress);
reply.concat(F("<TR><TD>ESP Chip ID:<TD>"));
sprintf_P(buff, PSTR("%06X"), ESP_getChipId());
reply.concat(buff);
#if defined(ESP8266)
reply.concat(F("<TR><TD>Flash Chip ID:<TD>"));
sprintf_P(buff, PSTR("%08X"), ESP.getFlashChipId());
reply.concat(buff);
#endif
#if defined(ESP8266)
uint8_t stations = wifi_softap_get_station_num();
reply.concat(F("<TR><TD>Clients:<TD>"));
reply.concat(stations);
#endif
reply.concat(F("</table></form>"));
server->send(200, FPSTR(TEXTHTML), reply);
} }
void WiFiConfig::_handleRoot(void) void WiFiConfig::_handleInfo(AsyncWebServerRequest *request)
{ {
String content;
content.reserve(3000); request->send_P(200, TEXTHTML, PAGE_INFO, std::bind(&WiFiConfig::infoProcessor, this, std::placeholders::_1));
content = FPSTR(PAGE_CAPTIVEPORTALCATCH); // 1. cast stranky }
// pridame informaci o stavu pokusu o pripojeni
content.concat(F("<div class=\"vl-info\">Pokus o připojení: "));
#if defined(ESP8266)
switch (_status) {
case STATION_IDLE:
content.concat(F("Klid"));
break;
case STATION_CONNECTING: void WiFiConfig::_handleRoot(AsyncWebServerRequest *request)
content.concat(F("Připojování")); {
break;
case STATION_WRONG_PASSWORD: request->send_P(200, TEXTHTML, PAGE_CAPTIVEPORTALCATCH, std::bind(&WiFiConfig::rootProcessor, this, std::placeholders::_1));
content.concat(F("Špatné heslo"));
break;
case STATION_NO_AP_FOUND:
content.concat(F("AP nenalezen"));
break;
case STATION_CONNECT_FAIL:
content.concat(F("Připojení selhalo"));
break;
case STATION_GOT_IP:
content.concat(F("Získaná ip"));
break;
case 0xfe:
content.concat(F("Špatná konfigurace"));
break;
case 0xff:
content.concat(F("Vynucená konfigurace"));
break;
default:
content.concat(F("Neznámý"));
break;
}
#else
switch (_status) {
case WL_CONNECT_FAILED:
content.concat(F("Připojení selhalo"));
break;
case 0xfe:
content.concat(F("Špatná EEPROM"));
break;
case 0xff:
content.concat(F("Vynucená konfigurace"));
break;
default:
content.concat(F("Neznámý"));
break;
}
#endif
content.concat(F("</div>"));
content.concat(FPSTR(PAGE_CAPTIVEPORTALCATCH2)); // 2. cast stranky (ukonceni)
server->send(200, FPSTR(TEXTHTML), content);
_time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat _time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
} }
void WiFiConfig::_handleDisplayAP(void) IPAddress WiFiConfig::_getIP(const String &from)
{ {
String s; uint32_t ipa;
String v; int result;
String content;
_time = millis() + (360 * 1000); // spocitame si novy cas, kdy budeme modul restartovat (6 minut) ipa = svGetV<uint32_t>(from, &result, F(WIFICFG_FILE));
content.reserve(3000); if (SV_OK != result)
content = FPSTR(PAGE_INDEX1); ipa = 0;
int n = WiFi.scanNetworks(); return IPAddress(ipa);
if (0 == n) {
content.concat(FPSTR(PAGE_NO_SSID));
} else {
for (int i = 0; i < n; ++i) {
int quality;
if (WiFi.RSSI(i) <= -100)
quality = 0;
else if (WiFi.RSSI(i) >= -50)
quality = 100;
else
quality = 2 * (WiFi.RSSI(i) + 100);
s = FPSTR(SSID_ITEM);
s.replace(F("{v}"), WiFi.SSID(i));
s.replace(F("{a}"), String(quality));
#if defined(ESP8266)
s.replace(F("{s}"), (ENC_TYPE_NONE == WiFi.encryptionType(i)) ? F("") : F("l"));
#else
s.replace(F("{s}"), (WIFI_AUTH_OPEN == WiFi.encryptionType(i)) ? F("") : F("l"));
#endif
content.concat(s);
}
}
s = FPSTR(PAGE_INDEX2);
v = svGetV<String>(F("ssid"), nullptr, F(WIFICFG_FILE));
strDecode(v);
if (v.length())
s.replace(F("{s}"), String(FPSTR(VALUE)) + v + F("'"));
else
s.replace(F("{s}"), F(""));
v = svGetV<String>(F("pass"), nullptr, F(WIFICFG_FILE));
if (v.length())
s.replace(F("{p}"), String(FPSTR(VALUE)) + v + F("'"));
else
s.replace(F("{p}"), F(""));
if (svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE)) == WIFIMODE_AP)
s.replace(F("{a}"), FPSTR(CHECKED));
else
s.replace(F("{a}"), F(""));
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(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 = 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));
content.concat(s);
// Uzivatelske parametry
if (_params) {
content.concat(FPSTR(PAGE_PARAM_HDR));
}
WiFiConfigUsrParameter *up = _params;
while (NULL != up) {
s = FPSTR(PAGE_PARAM);
s.replace(F("{t}"), up->getLabel());
s.replace(F("{n}"), up->getID());
s.replace(F("{l}"), String(up->getValueLength()));
s.replace(F("{v}"), up->getValue());
content.concat(s);
up = up->getNext();
}
content.concat(FPSTR(PAGE_END));
server->send(200, FPSTR(TEXTHTML), content);
} }
void WiFiConfig::_handleSetAP(void) void WiFiConfig::_handleDisplayAP(AsyncWebServerRequest *request)
{
request->send_P(200, TEXTHTML, PAGE_INDEX1, std::bind(&WiFiConfig::apProcessor, this, std::placeholders::_1));
_time = millis() + (360 * 1000); // spocitame si novy cas, kdy budeme modul restartovat (6 minut)
}
void WiFiConfig::_handleSetAP(AsyncWebServerRequest *request)
{ {
uint8_t mode;
String str; String str;
str.reserve(128); str.reserve(128);
str = server->arg(F("_s")); str = request->arg(F("_s"));
if (str.length() > 0) { if (str.length() > 0) {
uint8_t mode;
strEncode(str); strEncode(str);
svSetV(F("ssid"), str, F(WIFICFG_FILE)); svSetV(F("ssid"), str, F(WIFICFG_FILE));
str = server->arg(F("_p")); str = request->arg(F("_p"));
strEncode(str); strEncode(str);
svSetV(F("pass"), str, F(WIFICFG_FILE)); svSetV(F("pass"), str, F(WIFICFG_FILE));
str = server->arg(F("_a")); str = request->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 = request->arg(F("_ch")); // kanal AP
svSetV<int>(F("channel"), str.toInt(), F(WIFICFG_FILE)); svSetV<int>(F("channel"), str.toInt(), F(WIFICFG_FILE));
} else } else
mode = WIFIMODE_STA; // rezim STA mode = WIFIMODE_STA; // rezim STA
svSetV(F("mode"), mode, F(WIFICFG_FILE)); svSetV(F("mode"), mode, F(WIFICFG_FILE));
str = server->arg(F("_st")); str = request->arg(F("_st"));
if (0 == str.length()) if (0 == str.length())
svSetV<int>(F("ipmode"), IPCONFIG_DHCP, F(WIFICFG_FILE)); // mame DHCP dynamickou konfiguraci svSetV<int>(F("ipmode"), IPCONFIG_DHCP, F(WIFICFG_FILE)); // mame DHCP dynamickou konfiguraci
else { else {
@ -543,37 +463,36 @@ void WiFiConfig::_handleSetAP(void)
IPAddress ipa; IPAddress ipa;
svSetV<int>(F("ipmode"), IPCONFIG_STATIC, F(WIFICFG_FILE)); // mame statickou konfiguraci svSetV<int>(F("ipmode"), IPCONFIG_STATIC, F(WIFICFG_FILE)); // mame statickou konfiguraci
str = server->arg(F("_i")); str = request->arg(F("_i"));
ipa.fromString(str); ipa.fromString(str);
svSetV<uint32_t>(F("ip"), (uint32_t)ipa, F(WIFICFG_FILE)); svSetV<uint32_t>(F("ip"), (uint32_t)ipa, F(WIFICFG_FILE));
str = server->arg(F("_m")); str = request->arg(F("_m"));
ipa.fromString(str); ipa.fromString(str);
svSetV<uint32_t>(F("mask"), (uint32_t)ipa, F(WIFICFG_FILE)); svSetV<uint32_t>(F("mask"), (uint32_t)ipa, F(WIFICFG_FILE));
str = server->arg(F("_g")); str = request->arg(F("_g"));
ipa.fromString(str); ipa.fromString(str);
svSetV<uint32_t>(F("gw"), (uint32_t)ipa, F(WIFICFG_FILE)); svSetV<uint32_t>(F("gw"), (uint32_t)ipa, F(WIFICFG_FILE));
str = server->arg(F("_d")); str = request->arg(F("_d"));
ipa.fromString(str); ipa.fromString(str);
svSetV<uint32_t>(F("dns"), (uint32_t)ipa, F(WIFICFG_FILE)); svSetV<uint32_t>(F("dns"), (uint32_t)ipa, F(WIFICFG_FILE));
} }
// Uzivatelske parametry // Uzivatelske parametry
for (int i = 0; i < server->args(); i++) { for (unsigned i = 0; i < request->args(); i++) {
if (!server->argName(i).startsWith(F("_"))) { // vnitrni parametry WiFiConfig modulu zacinaji _, takze ty muzeme ignorovat if (!request->argName(i).startsWith(F("_"))) { // vnitrni parametry WiFiConfig modulu zacinaji _, takze ty muzeme ignorovat
WiFiConfigUsrParameter *up = _searchUsrParameter(server->argName(i).c_str()); WiFiConfigUsrParameter *up = _searchUsrParameter(request->argName(i).c_str());
if (NULL != up) if (NULL != up)
up->setNewValue(server->arg(i).c_str()); up->setNewValue(request->arg(i).c_str());
} }
} }
} }
server->send_P(200, TEXTHTML, PAGE_SAVED); request->onDisconnect([]() {
delay(2000); // cekame na odeslani dat _doReconfig = true; // aktivujeme novou konfiguraci a restartujeme ESP
// nakonfigurujeme ESP dle nove nastavenych parametru });
_prepareWifi(); request->send_P(200, TEXTHTML, PAGE_SAVED);
ESP.restart();
} }
// Start WiFi v rezimu AP pro nastaveni modulu // Start WiFi v rezimu AP pro nastaveni modulu
@ -583,11 +502,9 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
WiFi.persistent(false); // chranime flash pred zbytecnymi prepisy WiFi.persistent(false); // chranime flash pred zbytecnymi prepisy
dnsServer.reset(new DNSServer()); dnsServer.reset(new DNSServer());
#if defined(ESP8266) server = new AsyncWebServer(80);
server.reset(new ESP8266WebServer(80));
#else server->reset();
server.reset(new WebServer(80));
#endif
/* Soft AP network parameters */ /* Soft AP network parameters */
IPAddress apIP(172, 217, 28, 1); IPAddress apIP(172, 217, 28, 1);
@ -624,17 +541,18 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
if (cb) if (cb)
cb(WCS_CONFIGSTART); // signalizujeme start konfiguracniho serveru cb(WCS_CONFIGSTART); // signalizujeme start konfiguracniho serveru
// Nastavime handlery weboveho serveru pro konfiguraci // Nastavime handlery weboveho serveru pro konfiguraci
server->onNotFound(std::bind(&WiFiConfig::_handleNotFound, this)); server->onNotFound(std::bind(&WiFiConfig::_handleNotFound, this, std::placeholders::_1));
server->on(F("/config"), std::bind(&WiFiConfig::_handleDisplayAP, this)); server->on(PSTR("/config"), std::bind(&WiFiConfig::_handleDisplayAP, this, std::placeholders::_1));
server->on(F("/s"), std::bind(&WiFiConfig::_handleSetAP, this)); server->on(PSTR("/scan"), std::bind(&WiFiConfig::_handleScan, this, std::placeholders::_1));
server->on(F("/r"), std::bind(&WiFiConfig::_handleReset, this)); server->on(PSTR("/s"), std::bind(&WiFiConfig::_handleSetAP, this, std::placeholders::_1));
server->on(F("/i"), std::bind(&WiFiConfig::_handleInfo, this)); server->on(PSTR("/r"), std::bind(&WiFiConfig::_handleReset, this, std::placeholders::_1));
server->on(F("/index.htm"), std::bind(&WiFiConfig::_handleRoot, this)); server->on(PSTR("/i"), std::bind(&WiFiConfig::_handleInfo, this, std::placeholders::_1));
server->on(PSTR("/index.htm"), std::bind(&WiFiConfig::_handleRoot, this, std::placeholders::_1));
#ifdef USE_WIFICONFIG_OTAUPDATE #ifdef USE_WIFICONFIG_OTAUPDATE
server->on(F("/u"), []() { server->on(PSTR("/u"), []() {
server->send_P(200, TEXTHTML, PAGE_UPDATE); server->send_P(200, TEXTHTML, PAGE_UPDATE);
}); });
server->on(F("/update"), HTTP_POST, []() { server->on(PSTR("/update"), HTTP_POST, []() {
server->sendHeader(F("Connection"), F("close")); server->sendHeader(F("Connection"), F("close"));
server->send(200, FPSTR(TEXTPLAIN), (Update.hasError()) ? F("FAIL") : F("OK")); server->send(200, FPSTR(TEXTPLAIN), (Update.hasError()) ? F("FAIL") : F("OK"));
ESP.restart(); ESP.restart();
@ -661,8 +579,20 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
}); });
#endif #endif
server->begin(); // startujeme webovy server server->begin(); // startujeme webovy server
// Asynchronne ziskame seznam okolnich AP prakticky okamzite po startu konfiguracniho AP
int n = WiFi.scanComplete();
if (n == WIFI_SCAN_FAILED) {
WiFi.scanNetworks(true);
}
while (1) { while (1) {
server->handleClient(); // osetrujeme praci serveru if (_doReconfig) {
// mame aktivovat novou konfiguraci
_prepareWifi();
ESP.restart();
}
if (cb) if (cb)
cb(WCS_CONFIGWAIT); // volame uzivatelsky callback (napr. signalizace) cb(WCS_CONFIGWAIT); // volame uzivatelsky callback (napr. signalizace)
dnsServer->processNextRequest(); dnsServer->processNextRequest();
@ -678,7 +608,7 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
} }
// tady by asi melo byt zastaveni weboveho a dns serveru... // tady by asi melo byt zastaveni weboveho a dns serveru...
dnsServer->stop(); dnsServer->stop();
server->stop(); server->end();
return WCR_TIMEOUT; // nepripojeno, vyprsel timeout konfiguracniho AP return WCR_TIMEOUT; // nepripojeno, vyprsel timeout konfiguracniho AP
} }
@ -688,64 +618,7 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
uint32_t startt = millis(); uint32_t startt = millis();
#if !defined(ESP8266) #if !defined(ESP8266)
// nakonfigurujeme ESP dle nove nastavenych parametru _prepareWifi(); // nakonfigurujeme ESP32 dle nastavenych parametru
WiFi.disconnect(); // vsechno odpojime
encString s = svGetV<String>(F("ssid"), nullptr, F(WIFICFG_FILE));
s.decode();
encString pass = svGetV<String>(F("pass"), nullptr, F(WIFICFG_FILE));
pass.decode();
switch (svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE))) {
case WIFIMODE_STA: {
DEBUG_MSG("STA mode.\r\n");
if (IPCONFIG_STATIC == svGetV<int>(F("ipmode"), nullptr, F(WIFICFG_FILE))) {
DEBUG_MSG("Static configuration.\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);
}
WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta
WiFi.begin(s.c_str(), pass.c_str());
DEBUG_MSG("STA params: %s, %s\r\n", s.c_str(), pass.c_str());
if (strlen(WiFiDeviceName)) {
WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni
}
}
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(), chan);
} else {
// otevreny AP
WiFi.softAP(s.c_str(), NULL, chan);
}
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))));
}
if (cb)
cb(WCS_CONNECTED);
break;
default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???)
DEBUG_MSG("Mode Error!!\r\n");
break;
}
#endif #endif
DEBUG_MSG("Trying to connect.\r\n"); DEBUG_MSG("Trying to connect.\r\n");
@ -851,11 +724,26 @@ wificonfigresult_t WiFiConfig::begin(int forceConfigure, int timeout, wificonfig
return begin(forceConfigure, cb); // spustime WiFi return begin(forceConfigure, cb); // spustime WiFi
} }
#if defined(ESP32)
static const char INITTXT[] = "{}";
#endif
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) 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; String es;
DEBUG_MSG("initConfig %d\r\n", mode); DEBUG_MSG("initConfig %d\r\n", mode);
if (!USEDFS.exists(F(WIFICFG_FILE))) {
DEBUG_MSG("Create file...\r\n");
File f;
f = USEDFS.open(F(WIFICFG_FILE), "w");
#if defined(ESP8266)
f.write("{}");
#else
f.write((const uint8_t *)INITTXT, strlen(INITTXT));
#endif
f.close();
}
es = ssid; es = ssid;
strEncode(es); strEncode(es);
svSetV<String>(F("ssid"), es, F(WIFICFG_FILE)); svSetV<String>(F("ssid"), es, F(WIFICFG_FILE));
@ -875,6 +763,7 @@ void WiFiConfig::initConfig(const String &ssid, const String &pass, const int mo
void WiFiConfig::_prepareWifi(void) void WiFiConfig::_prepareWifi(void)
{ {
DEBUG_MSG("Prepare wifi...\r\n");
WiFi.persistent(true); // chceme, aby si modul zapamatoval konfiguraci WiFi.persistent(true); // chceme, aby si modul zapamatoval konfiguraci
WiFi.disconnect(); // vsechno odpojime WiFi.disconnect(); // vsechno odpojime
delay(100); delay(100);
@ -885,6 +774,8 @@ void WiFiConfig::_prepareWifi(void)
strDecode(pass); strDecode(pass);
int mode = svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE)); int mode = svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE));
DEBUG_MSG("SSID %s, Pass %s, Mode %d\r\n", s.c_str(), pass.c_str(), mode);
switch (mode) { switch (mode) {
case WIFIMODE_STA: { case WIFIMODE_STA: {

View File

@ -36,9 +36,15 @@
# include <WiFi.h> # include <WiFi.h>
#endif #endif
#include <memory> #include <memory>
#include <ESPAsyncWebServer.h>
#include <DNSServer.h>
#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
#if not defined(WIFICFG_FILE)
# define WIFICFG_FILE "/wifi.json"
#endif
// 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
@ -133,19 +139,26 @@ public:
wificonfigresult_t begin(int forceConfigure, wificonfig_cb cb); wificonfigresult_t begin(int forceConfigure, wificonfig_cb cb);
wificonfigresult_t begin(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(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 = (uint32_t)0, const IPAddress &mask = (uint32_t)0, const IPAddress &gw = (uint32_t)0, const IPAddress &dns = (uint32_t)0);
private: private:
AsyncWebServer *server;
std::unique_ptr<DNSServer> dnsServer;
void _prepareWifi(void); void _prepareWifi(void);
wificonfigresult_t _setupAP(wificonfig_cb cb); wificonfigresult_t _setupAP(wificonfig_cb cb);
void _handleDisplayAP(void); void _handleDisplayAP(AsyncWebServerRequest *request);
void _handleSetAP(void); void _handleSetAP(AsyncWebServerRequest *request);
void _handleInfo(void); void _handleInfo(AsyncWebServerRequest *request);
void _handleReset(void); void _handleReset(AsyncWebServerRequest *request);
void _handleNotFound(); // CaptivePortal redirector void _handleNotFound(AsyncWebServerRequest *request); // CaptivePortal redirector
void _handleRoot(); // jen jednoducha stranka kvuli CaptivePortalu umoznuje prejit na spravnou stranku (ale nedela to...) void _handleRoot(AsyncWebServerRequest *request); // jen jednoducha stranka kvuli CaptivePortalu umoznuje prejit na spravnou stranku (ale nedela to...)
void _handleScan(AsyncWebServerRequest *request);
bool _testWifi(wificonfig_cb cb); bool _testWifi(wificonfig_cb cb);
WiFiConfigUsrParameter *_searchUsrParameter(const char *name); WiFiConfigUsrParameter *_searchUsrParameter(const char *name);
IPAddress _getIP(const String &from);
String rootProcessor(const String &var);
String apProcessor(const String &var);
String infoProcessor(const String & var);
WiFiConfigUsrParameter *_params; // ukazatel na posledni zadany uzivatelsky parametr WiFiConfigUsrParameter *_params; // ukazatel na posledni zadany uzivatelsky parametr
int _timeout; // timeout pri cekani na konfiguraci int _timeout; // timeout pri cekani na konfiguraci

View File

@ -9,16 +9,17 @@
static const char TEXTHTML[] PROGMEM = "text/html"; static const char TEXTHTML[] PROGMEM = "text/html";
static const char TEXTPLAIN[] PROGMEM = "text/plain"; static const char TEXTPLAIN[] PROGMEM = "text/plain";
static const char TEXTJSON[] PROGMEM = "text/json";
static const char PAGE_INDEX1[] PROGMEM = R"=====( static const char PAGE_INDEX1[] PROGMEM = R"=====(<!DOCTYPE html>
<!DOCTYPE html><html lang="en"><head><meta charset= "utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <html lang="en"><head><meta charset= "utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>)=====" WIFICONFIG_WEBTITLE R"=====(</title> <title>)=====" WIFICONFIG_WEBTITLE R"=====(</title>
<style> <style>
.c{text-align: center;} .c{text-align: center;}
div,input{padding:5px;font-size:1em;} div,input{padding:5px;font-size:1em;}
input{width:95%;} input{width:95%%;}
body{text-align: center;font-family:verdana;} body{text-align: center;font-family:verdana;}
button{border:0;border-radius:0.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;} button{border:0;border-radius:0.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%%;}
.q{float: right;width: 64px;text-align: right;} .q{float: right;width: 64px;text-align: right;}
.l{background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAALVBMVEX///8EBwfBwsLw8PAzNjaCg4NTVVUjJiZDRUUUFxdiZGSho6OSk5Pg4eFydHTCjaf3AAAAZElEQVQ4je2NSw7AIAhEBamKn97/uMXEGBvozkWb9C2Zx4xzWykBhFAeYp9gkLyZE0zIMno9n4g19hmdY39scwqVkOXaxph0ZCXQcqxSpgQpONa59wkRDOL93eAXvimwlbPbwwVAegLS1HGfZAAAAABJRU5ErkJggg==") no-repeat left center;background-size: 1em;} .l{background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAALVBMVEX///8EBwfBwsLw8PAzNjaCg4NTVVUjJiZDRUUUFxdiZGSho6OSk5Pg4eFydHTCjaf3AAAAZElEQVQ4je2NSw7AIAhEBamKn97/uMXEGBvozkWb9C2Zx4xzWykBhFAeYp9gkLyZE0zIMno9n4g19hmdY39scwqVkOXaxph0ZCXQcqxSpgQpONa59wkRDOL93eAXvimwlbPbwwVAegLS1HGfZAAAAABJRU5ErkJggg==") no-repeat left center;background-size: 1em;}
#rssiBar{width: 100px;height: 15px;background: #fff;} #rssiBar{width: 100px;height: 15px;background: #fff;}
@ -30,65 +31,63 @@ function hAP(){if (document.getElementById('_a').checked){document.getElementByI
function hSC(){if (document.getElementById('_st').checked){document.getElementById('staticip').style.display = 'block';}else{document.getElementById('staticip').style.display = 'none';}} function hSC(){if (document.getElementById('_st').checked){document.getElementById('staticip').style.display = 'block';}else{document.getElementById('staticip').style.display = 'none';}}
</script> </script>
</head><body><h1>)=====" WIFICONFIG_TITLE R"=====(</h1><div style='text-align:left;display:inline-block;min-width:260px;'> </head><body><h1>)=====" WIFICONFIG_TITLE R"=====(</h1><div style='text-align:left;display:inline-block;min-width:260px;'>
)====="; <div id="ssids">
</div>
/* Polozky nalezenych SSID
{v} - SSID
{a} - RSSI (%)
{s} - bud nic, nebo 'l'>
Rozsahy otestovany zde https://css-tricks.com/html5-meter-element/
*/
static const char SSID_ITEM[] PROGMEM = R"=====(
<div><a href='#p' onclick='c(this)'>{v}</a>&nbsp;<span class='q {s}'><meter value="{a}" min="0" low="30" optimum="70" high="60" max="100" style="width: 70%;"></meter></span></div>
)=====";
/* Zacatek formulare pro vyplneni
{s} - SSID
{p} - password
{a} - AP mode (checkbox)
{ch}- cislo kanalu AP
{c} - staticka IP konfigurace (checkbox)
{i} - ip adresa
{m} - maska
{g} - brana
{d} - dns server
*/
static const char PAGE_INDEX2[] PROGMEM = R"=====(
<form method='post' action='s'> <form method='post' action='s'>
<label>SSID<br><input id='_s' name='_s' maxlength=32 required placeholder='SSID' {s}></label><br> <label>SSID<br><input id='_s' name='_s' maxlength=32 required placeholder='SSID' value="%SSID%"></label><br>
<label>Heslo<br><input id='_p' name='_p' maxlength=64 {p} type='password' placeholder='heslo'></label><br> <label>Heslo<br><input id='_p' name='_p' onfocus="this.select();" maxlength=64 value="%PASS%" type='password' placeholder='heslo'></label><br>
<div> <div>
<label><input id='_a' name='_a' type='checkbox' style="width: 10%; float: left; margin-top: 5px;" onclick='hAP();' {a}>Režim AP</label> <label><input id='_a' name='_a' type='checkbox' style="width: 10%%; float: left; margin-top: 5px;" onclick='hAP();' %APMODE%>Režim AP</label>
<div id="apconfig"> <div id="apconfig">
<label>Kanál<br><input type="number" name="_ch" min="1" max="13" title='cislo kanalu v intervalu 1-13' value='{ch}'></label><br> <label>Kanál<br><input type="number" name="_ch" min="1" max="13" title='cislo kanalu v intervalu 1-13' value='%APCHAN%'></label><br>
</div> </div>
</div> </div>
<div> <div>
<label><input id='_st' name='_st' type='checkbox' style="width: 10%; float: left; margin-top: 5px;" onclick='hSC();' {c}>Statická IP konfigurace</label> <label><input id='_st' name='_st' type='checkbox' style="width: 10%%; float: left; margin-top: 5px;" onclick='hSC();' %STATIC%>Statická IP konfigurace</label>
<div id="staticip"> <div id="staticip">
<label>IP adresa<br><input type="text" name="_i" value='{i}'></label><br> <label>IP adresa<br><input type="text" name="_i" value='%IP%'></label><br>
<label>Síťová maska<br><input type="text" name="_m" value='{m}'></label><br> <label>Síťová maska<br><input type="text" name="_m" value='%MASK%'></label><br>
<label>Síťová brána<br><input type="text" name="_g" value='{g}'></label><br> <label>Síťová brána<br><input type="text" name="_g" value='%GATE%'></label><br>
<label>Server DNS<br><input type="text" name="_d" value='{d}'></label><br> <label>Server DNS<br><input type="text" name="_d" value='%DNS%'></label><br>
</div> </div>
</div> </div>
<script>hAP();hSC();</script> %USER%
)=====";
// Uzivatelsky pridane polozky
static const char PAGE_PARAM_HDR[] PROGMEM = R"=====(
<hr>
<h2>Uživatelské položky</h2>
)=====";
static const char PAGE_PARAM[] PROGMEM = R"=====(
<label>{t}<br><input name='{n}' maxlength='{l}' value='{v}'></label><br>
)=====";
// Konec formulare a stranky
static const char PAGE_END[] PROGMEM = R"=====(
<br> <br>
<button type='submit'>Uložit a restartovat</button></form> <button type='submit'>Uložit a restartovat</button></form>
<script>
window.onload = function() {
hAP();
hSC();
var req = new XMLHttpRequest();
req.open("GET", "/scan", true);
req.onreadystatechange = function() {
if (req.readyState != 4 || req.status != 200)
return;
ssids = document.getElementById('ssids');
data = JSON.parse(req.responseText);
status = data.status;
html = "";
if (status > 0) {
for (i= 0 ; i < status; ++i) {
lock = "";
if (data.ssids[i].secure != 0)
lock = "l";
value = data.ssids[i].rssi;
if (value <= -100)
value = 0;
else if (value >= -50)
value = 100;
else
value = 2 * (value + 100);
line = "<div><a href='#p' onclick='c(this)'>" + data.ssids[i].ssid + "</a>&nbsp;<span class='q " + lock + "'><meter value='" + value + "' min='0' low='30' optimum='70' high='60' max='100' style='width: 70%%;'></meter></span></div>";
html += line;
}
ssids.innerHTML = html;
}
};
req.send();
};
</script>
</div>)=====" </div>)====="
#if defined APP_NAME && defined APP_VERSION #if defined APP_NAME && defined APP_VERSION
R"=====(<div style='text-align:right;font-size:11px;'><hr/>)=====" R"=====(<div style='text-align:right;font-size:11px;'><hr/>)====="
@ -102,6 +101,17 @@ R"=====(<div style='text-align:right;font-size:11px;'><hr/><a href='http://xpabl
#endif #endif
R"=====(</body></html> R"=====(</body></html>
)====="; )=====";
// Uzivatelsky pridane polozky
static const char PAGE_PARAM_HDR[] PROGMEM = R"=====(
<hr>
<h2>Uživatelské položky</h2>
)=====";
static const char PAGE_PARAM[] PROGMEM = R"=====(
<label>%s<br><input name='%s' maxlength='%d' value='%s'></label><br>
)=====";
/* /*
<div style='text-align:right;font-size:11px;'><hr/><a href='http://xpablo.cz' target='_blank' style='color:#aaa;'>WiFiConfig by xPablo</a></div> <div style='text-align:right;font-size:11px;'><hr/><a href='http://xpablo.cz' target='_blank' style='color:#aaa;'>WiFiConfig by xPablo</a></div>
*/ */
@ -118,13 +128,13 @@ static const char PAGE_SAVED[] PROGMEM = R"=====(
<style>div,input {margin-bottom: 5px;}body{width:200px;display:block;margin-left:auto;margin-right:auto;}</style> <style>div,input {margin-bottom: 5px;}body{width:200px;display:block;margin-left:auto;margin-right:auto;}</style>
</head><body> </head><body>
Uloženo do EEPROM...<br/> Uloženo do EEPROM...<br/>
Restart za 5 sekund. Restart za několik sekund.
</body></html> </body></html>
)====="; )=====";
static const char PAGE_CAPTIVEPORTALCATCH[] PROGMEM = R"=====( static const char PAGE_CAPTIVEPORTALCATCH[] PROGMEM = R"=====(<!DOCTYPE html>
<!DOCTYPE html><html lang="cs"><head><meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/><title>xPablo.cz Setup</title> <html lang="cs"><head><meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/><title>xPablo.cz Setup</title>
<style>.c{text-align: center;} div,input{padding:5px;font-size:1em;} input{width:95%;} body{text-align: center;font-family:verdana;} button{border:0;border-radius:0.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;} .q{float: right;width: 64px;text-align: right;} <style>.c{text-align: center;} div,input{padding:5px;font-size:1em;} input{width:95%%;} body{text-align: center;font-family:verdana;} button{border:0;border-radius:0.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%%;} .q{float: right;width: 64px;text-align: right;}
.vl-info {border-left: thick solid #1fa3ec;background: #cee6ff; text-align:left; display:inline-block; min-width:260px; margin-top:15px} .vl-info {border-left: thick solid #1fa3ec;background: #cee6ff; text-align:left; display:inline-block; min-width:260px; margin-top:15px}
.red { background-color: #ff0000;} .red { background-color: #ff0000;}
.magenta { background-color: #ff00ff;} .magenta { background-color: #ff00ff;}
@ -142,11 +152,8 @@ R"=====(
</form><br/><form action="/u" method="get"> </form><br/><form action="/u" method="get">
<button class="magenta">Aktualizace firmware</button></form><br>)=====" <button class="magenta">Aktualizace firmware</button></form><br>)====="
#endif #endif
; R"=====(
<div class=\"vl-info\">Pokus o připojení: %STATUS%</div>
// druha cast (mezi 1. a 2. cast muzeme doplnit dodatecne informace)
static const char PAGE_CAPTIVEPORTALCATCH2[] PROGMEM = R"=====(
</div>
)=====" )====="
#if defined APP_NAME && defined APP_VERSION #if defined APP_NAME && defined APP_VERSION
R"=====(<div style='text-align:right;font-size:11px;'><hr/>)=====" R"=====(<div style='text-align:right;font-size:11px;'><hr/>)====="
@ -161,8 +168,8 @@ R"=====(<div style='text-align:right;font-size:11px;'><hr/><a href='http://xpabl
R"=====(</body></html> R"=====(</body></html>
)====="; )=====";
static const char PAGE_RESTART[] PROGMEM = R"=====( static const char PAGE_RESTART[] PROGMEM = R"=====(<!DOCTYPE html>
<!DOCTYPE html><html lang="cs"><head><meta http-equiv="refresh" content="5;url=/"/> <html lang="cs"><head><meta http-equiv="refresh" content="5;url=/"/>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"/> <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"/>
<title>xPablo Setup - Restart</title> <title>xPablo Setup - Restart</title>
<style>div,input {margin-bottom: 5px;}body{width:200px;display:block;margin-left:auto;margin-right:auto;}</style> <style>div,input {margin-bottom: 5px;}body{width:200px;display:block;margin-left:auto;margin-right:auto;}</style>
@ -225,3 +232,26 @@ domReady(function() {
</body> </body>
</html> </html>
)====="; )=====";
static const char PAGE_INFO[] PROGMEM = R"=====(
<!DOCTYPE html><html lang="en"><head><meta charset= "utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>)=====" WIFICONFIG_WEBTITLE R"=====(</title>
<style>
.c{text-align: center;}
div,input{padding:5px;font-size:1em;}
input{width:95%%;}
body{text-align: center;font-family:verdana;}
button{border:0;border-radius:0.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%%;}
.q{float: right;width: 64px;text-align: right;}
</style>
</head><body><h1>)=====" WIFICONFIG_TITLE R"=====(</h1><div style='text-align:left;display:inline-block;min-width:260px;'>
<form><table><tr><th>System Info<th>
<tr><td>Core Version:<td>%SDK%
<tr><td>Flash Size:<td>%FS% kB
<tr><td>Sketch Size/Free:<td>%SS% kB / %FSS% kB
<tr><td>STA MAC:<td>%STAMAC%
<tr><td>AP MAC:<td>%APMAC%
<tr><td>ESP Chip ID:<td>%ESPID%
<tr><td>Flash Chip ID:<td>%FID%
</table></form>
)=====";