Optimalizace i pro ESP32, moznost uploadu firmware

This commit is contained in:
Pavel Brychta 2019-10-11 16:17:54 +02:00
parent 52648c8915
commit c6f3e3371c
5 changed files with 678 additions and 646 deletions

View File

@ -12,7 +12,7 @@
"type": "git", "type": "git",
"url": "http://git.xpablo.cz/pablo2048/WiFiConfig.git" "url": "http://git.xpablo.cz/pablo2048/WiFiConfig.git"
}, },
"version": "6.3.1", "version": "6.3.2",
"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.3.1 version=6.3.2
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

@ -115,15 +115,13 @@
extern char WiFiDeviceName[]; extern char WiFiDeviceName[];
enum enum {
{
WIFIMODE_AP = WIFI_AP, // rezim prace jako pristupovy bod (AP) WIFIMODE_AP = WIFI_AP, // rezim prace jako pristupovy bod (AP)
WIFIMODE_STA = WIFI_STA, // rezim prace jako klient WIFIMODE_STA = WIFI_STA, // rezim prace jako klient
WIFIMODE_AP_STA = WIFI_AP_STA // rezim prace jako klient i pristupovy bod WIFIMODE_AP_STA = WIFI_AP_STA // rezim prace jako klient i pristupovy bod
}; };
enum enum {
{
IPCONFIG_DHCP = 0x55, // DHCP konfigurace ip adres (default) IPCONFIG_DHCP = 0x55, // DHCP konfigurace ip adres (default)
IPCONFIG_STATIC = 0xaa // staticka konfigurace ip adres IPCONFIG_STATIC = 0xaa // staticka konfigurace ip adres
}; };
@ -146,8 +144,7 @@ IPAddress getOurIP(void)
IPAddress ipa; IPAddress ipa;
WiFiMode_t wm = WiFi.getMode(); WiFiMode_t wm = WiFi.getMode();
switch (wm) switch (wm) {
{
case WIFI_STA: case WIFI_STA:
ipa = WiFi.localIP(); ipa = WiFi.localIP();
break; break;
@ -176,8 +173,7 @@ uint32_t getEEPROMuint32(unsigned int start)
{ {
uint32_t result = 0; uint32_t result = 0;
for (uint32_t i = 0; i < 4; ++i) for (uint32_t i = 0; i < 4; ++i) {
{
result <<= 8; result <<= 8;
result += EEPROM.read(start); result += EEPROM.read(start);
++start; ++start;
@ -188,8 +184,7 @@ uint32_t getEEPROMuint32(unsigned int start)
void setEEPROMuint32(unsigned int start, uint32_t val) void setEEPROMuint32(unsigned int start, uint32_t val)
{ {
for (unsigned int i = 0; i < 4; ++i) for (unsigned int i = 0; i < 4; ++i) {
{
EEPROM.write(start + 3 - i, (uint8_t)val); EEPROM.write(start + 3 - i, (uint8_t)val);
val >>= 8; val >>= 8;
} }
@ -199,8 +194,7 @@ String getEEPROMString(unsigned int start, size_t len)
{ {
String string = ""; String string = "";
for (unsigned int i = start; i < + start + len; ++i) for (unsigned int i = start; i < + start + len; ++i) {
{
uint8_t b = EEPROM.read(i); uint8_t b = EEPROM.read(i);
if ((0xff == b) || (0 == b)) if ((0xff == b) || (0 == b))
@ -214,16 +208,12 @@ void setEEPROMString(unsigned int start, size_t len, String &string)
{ {
unsigned int si = 0; unsigned int si = 0;
for (unsigned int i = start; i < start + len; ++i) for (unsigned int i = start; i < start + len; ++i) {
{
char c; char c;
if (si < string.length()) if (si < string.length()) {
{
c = string[si]; c = string[si];
} } else {
else
{
c = 0; c = 0;
} }
EEPROM.write(i, c); EEPROM.write(i, c);
@ -239,12 +229,10 @@ WiFiConfigUsrParameter::WiFiConfigUsrParameter(const char *id, const char *label
_label = label; _label = label;
_length = length; _length = length;
_value = new char[length + 1]; _value = new char[length + 1];
for (unsigned int i = 0; i < length; i++) for (unsigned int i = 0; i < length; i++) {
{
_value[i] = 0; _value[i] = 0;
} }
if (defaultValue != NULL) if (defaultValue != NULL) {
{
strncpy(_value, defaultValue, length); strncpy(_value, defaultValue, length);
} }
} }
@ -296,8 +284,7 @@ WiFiConfigUsrParameter *WiFiConfig::_searchUsrParameter(const char *name)
{ {
WiFiConfigUsrParameter *ptr = _params; WiFiConfigUsrParameter *ptr = _params;
while (NULL != ptr) while (NULL != ptr) {
{
if (0 == strcmp(name, ptr->getID())) if (0 == strcmp(name, ptr->getID()))
break; break;
ptr = ptr->getNext(); ptr = ptr->getNext();
@ -316,12 +303,9 @@ void WiFiConfig::_handleNotFound(void)
DEBUG_MSG("Requested URI: %s\r\n", server->uri().c_str()); DEBUG_MSG("Requested URI: %s\r\n", server->uri().c_str());
if (server->uri().endsWith(String(F("favicon.ico")))) if (server->uri().endsWith(String(F("favicon.ico")))) {
{
server->send_P(404, TEXTPLAIN, PSTR("Err")); server->send_P(404, TEXTPLAIN, PSTR("Err"));
} } else {
else
{
String redirect; String redirect;
redirect.reserve(256); redirect.reserve(256);
redirect = F("http://"); redirect = F("http://");
@ -338,8 +322,7 @@ void WiFiConfig::_handleReset(void)
rsttick.reset(new(Ticker)); rsttick.reset(new(Ticker));
rsttick->once_ms(700, []() rsttick->once_ms(700, []() {
{
ESP.restart(); ESP.restart();
}); });
@ -423,8 +406,7 @@ void WiFiConfig::_handleRoot(void)
// pridame informaci o stavu pokusu o pripojeni // pridame informaci o stavu pokusu o pripojeni
content.concat(F("<div class=\"vl-info\">Pokus o připojení: ")); content.concat(F("<div class=\"vl-info\">Pokus o připojení: "));
#if defined(ESP8266) #if defined(ESP8266)
switch (_status) switch (_status) {
{
case STATION_IDLE: case STATION_IDLE:
content.concat(F("Klid")); content.concat(F("Klid"));
break; break;
@ -462,8 +444,7 @@ void WiFiConfig::_handleRoot(void)
break; break;
} }
#else #else
switch (_status) switch (_status) {
{
case WL_CONNECT_FAILED: case WL_CONNECT_FAILED:
content.concat(F("Připojení selhalo")); content.concat(F("Připojení selhalo"));
break; break;
@ -497,14 +478,10 @@ void WiFiConfig::_handleDisplayAP(void)
content.reserve(3000); content.reserve(3000);
content = FPSTR(PAGE_INDEX1); content = FPSTR(PAGE_INDEX1);
int n = WiFi.scanNetworks(); int n = WiFi.scanNetworks();
if (0 == n) if (0 == n) {
{
content.concat(FPSTR(PAGE_NO_SSID)); content.concat(FPSTR(PAGE_NO_SSID));
} } else {
else for (int i = 0; i < n; ++i) {
{
for (int i = 0; i < n; ++i)
{
int quality; int quality;
if (WiFi.RSSI(i) <= -100) if (WiFi.RSSI(i) <= -100)
@ -557,15 +534,13 @@ void WiFiConfig::_handleDisplayAP(void)
content.concat(s); content.concat(s);
// Uzivatelske parametry // Uzivatelske parametry
if (_params) if (_params) {
{
content.concat(FPSTR(PAGE_PARAM_HDR)); content.concat(FPSTR(PAGE_PARAM_HDR));
} }
WiFiConfigUsrParameter *up = _params; WiFiConfigUsrParameter *up = _params;
while (NULL != up) while (NULL != up) {
{
s = FPSTR(PAGE_PARAM); s = FPSTR(PAGE_PARAM);
s.replace(F("{t}"), up->getLabel()); s.replace(F("{t}"), up->getLabel());
s.replace(F("{n}"), up->getID()); s.replace(F("{n}"), up->getID());
@ -586,29 +561,26 @@ 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); EES_storeString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), str);
str = server->arg(F("_p")); str = server->arg(F("_p"));
EES_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str); EES_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str);
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()); EEPROM.write(configBase + offsetof(wificonfigarea_t, apchannel), (uint8_t)str.toInt());
} } else
else
mode = WIFIMODE_STA; // rezim STA mode = WIFIMODE_STA; // rezim STA
EEPROM.write(configBase + offsetof(wificonfigarea_t, mode), mode); EEPROM.write(configBase + offsetof(wificonfigarea_t, mode), mode);
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 EEPROM.write(configBase + offsetof(wificonfigarea_t, ip), IPCONFIG_DHCP); // 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); EEPROM.write(configBase + offsetof(wificonfigarea_t, ip), IPCONFIG_STATIC);
@ -630,10 +602,8 @@ void WiFiConfig::_handleSetAP(void)
} }
// Uzivatelske parametry // Uzivatelske parametry
for (int i = 0; i < server->args(); i++) for (int i = 0; i < server->args(); i++) {
{ if (!server->argName(i).startsWith(F("_"))) { // vnitrni parametry WiFiConfig modulu zacinaji _, takze ty muzeme ignorovat
if (!server->argName(i).startsWith(F("_"))) // vnitrni parametry WiFiConfig modulu zacinaji _, takze ty muzeme ignorovat
{
WiFiConfigUsrParameter *up = _searchUsrParameter(server->argName(i).c_str()); WiFiConfigUsrParameter *up = _searchUsrParameter(server->argName(i).c_str());
if (NULL != up) if (NULL != up)
up->setNewValue(server->arg(i).c_str()); up->setNewValue(server->arg(i).c_str());
@ -650,19 +620,15 @@ void WiFiConfig::_handleSetAP(void)
String s = EES_readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid)); 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)); String pass = EES_readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, 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)) {
{
WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni
} }
#endif #endif
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) {
{
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(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)))); IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns))));
@ -671,8 +637,7 @@ void WiFiConfig::_handleSetAP(void)
WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta
WiFi.begin(s.c_str(), pass.c_str()); WiFi.begin(s.c_str(), pass.c_str());
#if !defined(ESP8266) #if !defined(ESP8266)
if (strlen(WiFiDeviceName)) if (strlen(WiFiDeviceName)) {
{
WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni
} }
#endif #endif
@ -689,18 +654,14 @@ void WiFiConfig::_handleSetAP(void)
case WIFIMODE_AP: case WIFIMODE_AP:
DEBUG_MSG("AP mode.\r\n"); DEBUG_MSG("AP mode.\r\n");
WiFi.mode(WIFI_AP); // startujeme AP WiFi.mode(WIFI_AP); // startujeme AP
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(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
} } 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, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
} }
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) 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))), WiFi.softAPConfig(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, netmask))));
} }
@ -736,8 +697,7 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, netMsk); WiFi.softAPConfig(apIP, apIP, netMsk);
if (ssid.endsWith(F("?"))) if (ssid.endsWith(F("?"))) {
{
char lmac[16]; char lmac[16];
sprintf_P(lmac, PSTR("%06X"), ESP_getChipId()); sprintf_P(lmac, PSTR("%06X"), ESP_getChipId());
@ -757,18 +717,45 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
server->on(F("/r"), std::bind(&WiFiConfig::_handleReset, this)); server->on(F("/r"), std::bind(&WiFiConfig::_handleReset, this));
server->on(F("/i"), std::bind(&WiFiConfig::_handleInfo, this)); server->on(F("/i"), std::bind(&WiFiConfig::_handleInfo, this));
server->on(F("/index.htm"), std::bind(&WiFiConfig::_handleRoot, this)); server->on(F("/index.htm"), std::bind(&WiFiConfig::_handleRoot, this));
#ifdef USE_WIFICONFIG_OTAUPDATE
server->on(F("/u"), []() {
server->send_P(200, TEXTHTML, PAGE_UPDATE);
});
server->on(F("/update"), HTTP_POST, []() {
server->sendHeader(F("Connection"), F("close"));
server->send(200, FPSTR(TEXTPLAIN), (Update.hasError()) ? F("FAIL") : F("OK"));
ESP.restart();
}, []() {
HTTPUpload& upload = server->upload();
if (upload.status == UPLOAD_FILE_START) {
WiFiUDP::stopAll();
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
//Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
//Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
//Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
//Update.printError(Serial);
}
}
yield();
});
#endif
server->begin(); // startujeme webovy server server->begin(); // startujeme webovy server
while (1) while (1) {
{
server->handleClient(); // osetrujeme praci serveru server->handleClient(); // osetrujeme praci serveru
if (cb) if (cb)
cb(WCS_CONFIGWAIT); // volame uzivatelsky callback (napr. signalizace) cb(WCS_CONFIGWAIT); // volame uzivatelsky callback (napr. signalizace)
dnsServer->processNextRequest(); dnsServer->processNextRequest();
yield(); // procesy uvnitr systemu ESP potrebuji take svuj cas yield(); // procesy uvnitr systemu ESP potrebuji take svuj cas
if (_timeout) if (_timeout) {
{ if (millis() > _time) {
if (millis() > _time)
{
DEBUG_MSG("AP timeout\r\n"); DEBUG_MSG("AP timeout\r\n");
if (cb) if (cb)
cb(WCS_CONFIGTIMEOUT); // signalizujeme timeout cb(WCS_CONFIGTIMEOUT); // signalizujeme timeout
@ -794,13 +781,10 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
String s = EES_readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid)); 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)); String pass = EES_readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) {
{ 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 == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) {
{
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(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)))); IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns))));
@ -809,8 +793,7 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta
WiFi.begin(s.c_str(), pass.c_str()); WiFi.begin(s.c_str(), pass.c_str());
DEBUG_MSG("STA params: %s, %s\r\n", s.c_str(), pass.c_str()); DEBUG_MSG("STA params: %s, %s\r\n", s.c_str(), pass.c_str());
if (strlen(WiFiDeviceName)) if (strlen(WiFiDeviceName)) {
{
WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni
} }
} }
@ -819,18 +802,14 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
case WIFIMODE_AP: case WIFIMODE_AP:
DEBUG_MSG("AP mode.\r\n"); DEBUG_MSG("AP mode.\r\n");
WiFi.mode(WIFI_AP); // startujeme AP WiFi.mode(WIFI_AP); // startujeme AP
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(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
} } 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, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
} }
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) 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))), WiFi.softAPConfig(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, netmask))));
} }
@ -843,10 +822,8 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
#endif #endif
DEBUG_MSG("Trying to connect.\r\n"); DEBUG_MSG("Trying to connect.\r\n");
while ((millis() - startt) < WIFI_STA_CONNECT_TIMEOUT) while ((millis() - startt) < WIFI_STA_CONNECT_TIMEOUT) {
{ if (WL_CONNECTED == WiFi.status()) {
if (WL_CONNECTED == WiFi.status())
{
DEBUG_MSG("Connected...\r\n"); DEBUG_MSG("Connected...\r\n");
if (cb) if (cb)
cb(WCS_CONNECTED); cb(WCS_CONNECTED);
@ -872,35 +849,28 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificon
DEBUG_MSG("\r\n\r\n"); // oddeleni vypisu DEBUG_MSG("\r\n\r\n"); // oddeleni vypisu
configBase = configarea; // pocatek konfigurace v EEPROM 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 (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)) != 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", EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)), 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 (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) {
{ case WIFIMODE_STA: {
case WIFIMODE_STA:
{
#if defined(ESP8266) #if defined(ESP8266)
WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni
#else #else
WiFi.setHostname(WiFiDeviceName); WiFi.setHostname(WiFiDeviceName);
#endif #endif
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) {
{
DEBUG_MSG("Static configuration SET.\r\n"); DEBUG_MSG("Static configuration SET.\r\n");
WiFi.config(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), 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)))); IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns))));
@ -908,12 +878,10 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificon
DEBUG_MSG("STA mode.\r\n"); DEBUG_MSG("STA mode.\r\n");
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 {
{
if (cb) if (cb)
cb(WCS_CONNECTSTART); // signalizujeme zacatek pokusu o pripojeni cb(WCS_CONNECTSTART); // signalizujeme zacatek pokusu o pripojeni
if (!_testWifi(cb)) if (!_testWifi(cb)) {
{
result = _setupAP(cb); // modul se nepripojil - startujeme AP rezim result = _setupAP(cb); // modul se nepripojil - startujeme AP rezim
} }
} }

View File

@ -42,8 +42,7 @@
#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 // Struktura konfigurace, ulozena v EEPROM
typedef struct typedef struct {
{
uint8_t mode; // rezim prace AP/STA uint8_t mode; // rezim prace AP/STA
uint8_t ip; // konfigurace ip (staticka/DHCP) uint8_t ip; // konfigurace ip (staticka/DHCP)
char ssid[32 + 2]; // SSID site char ssid[32 + 2]; // SSID site
@ -56,8 +55,7 @@ typedef struct
} wificonfigarea_t; } 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
WCS_CONNECTING = 1, // probiha pokus o pripojeni WCS_CONNECTING = 1, // probiha pokus o pripojeni
WCS_CONNECTED = 2, // pripojeni bylo uspesne WCS_CONNECTED = 2, // pripojeni bylo uspesne
@ -67,8 +65,7 @@ typedef enum
} wificonfigstate_t; } wificonfigstate_t;
// Navratovy parametr z volani begin() - udava, jak se podarilo WiFiConfig modulu pripojit k AP // Navratovy parametr z volani begin() - udava, jak se podarilo WiFiConfig modulu pripojit k AP
typedef enum typedef enum {
{
WCR_OK = 0, // wifi pripojena/AP nastartovane (dle parametru v EEPROM) WCR_OK = 0, // wifi pripojena/AP nastartovane (dle parametru v EEPROM)
WCR_TIMEOUT = 1, // wifi neni pripojena a vyprsel zadany timeout WCR_TIMEOUT = 1, // wifi neni pripojena a vyprsel zadany timeout
WCR_CONFIGAP_NOT_STARTED = 2, // wifi neni pripojena a spusteni konfiguracniho AP bylo zakazane parametrem timeout (WC_DONT_RUN_CONFIGAP) WCR_CONFIGAP_NOT_STARTED = 2, // wifi neni pripojena a spusteni konfiguracniho AP bylo zakazane parametrem timeout (WC_DONT_RUN_CONFIGAP)

View File

@ -21,7 +21,7 @@ function c(l){document.getElementById('_s').value=l.innerText||l.textContent;doc
function hAP(){if (document.getElementById('_a').checked){document.getElementById('apconfig').style.display = 'block';}else{document.getElementById('apconfig').style.display = 'none';}} function hAP(){if (document.getElementById('_a').checked){document.getElementById('apconfig').style.display = 'block';}else{document.getElementById('apconfig').style.display = 'none';}}
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>ESP8266 WiFiConfig</h1><div style='text-align:left;display:inline-block;min-width:260px;'> </head><body><h1>ESP WiFiConfig</h1><div style='text-align:left;display:inline-block;min-width:260px;'>
)====="; )=====";
/* Polozky nalezenych SSID /* Polozky nalezenych SSID
@ -119,10 +119,22 @@ static const char PAGE_CAPTIVEPORTALCATCH[] PROGMEM = R"=====(
<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;}
</style> </style>
</head><body><div style='text-align:left;display:inline-block;min-width:260px;'> </head><body><div style='text-align:left;display:inline-block;min-width:260px;'>
<form action="/config" method="get"><button>Konfigurace WiFi</button></form><br/><form action="/i" method="get"><button>Informace o modulu</button></form><br/><form action="/r" method="post"><button class="red">Reset</button></form><br> <form action="/config" method="get">
)====="; <button>Konfigurace WiFi</button>
</form><br/>
<form action="/i" method="get">
<button>Informace o modulu</button>
</form><br/><form action="/r" method="post">
<button class="red">Reset</button></form><br>)====="
#if defined USE_WIFICONFIG_OTAUPDATE
R"=====(
</form><br/><form action="/u" method="get">
<button class="magenta">Aktualizace firmware</button></form><br>)====="
#endif
;
// druha cast (mezi 1. a 2. cast muzeme doplnit dodatecne informace) // druha cast (mezi 1. a 2. cast muzeme doplnit dodatecne informace)
static const char PAGE_CAPTIVEPORTALCATCH2[] PROGMEM = R"=====( static const char PAGE_CAPTIVEPORTALCATCH2[] PROGMEM = R"=====(
@ -150,3 +162,58 @@ static const char PAGE_RESTART[] PROGMEM = R"=====(
Restartuji... Restartuji...
</body></html> </body></html>
)====="; )=====";
static const char PAGE_UPDATE[] PROGMEM = R"=====(
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width,initial-scale=1'/>
</head>
<body>
<form method="POST" action="#" enctype="multipart/form-data" id="upload_form">
<input type="file" name="update" accept='.bin' id="file">
<input type="submit" value="Update"> </form>
<div id="prg_wrap" style="border: 0px solid; width: 100%;">
<div id="prg" style="text-shadow: 2px 2px 3px black; padding: 5px 0; display: none; border: 1px solid #008aff; background: #002180; text-align: center; color: white;"></div>
</div>
<script>
var domReady = function(callback) {
document.readyState === "interactive" || document.readyState === "complete" ? callback() : document.addEventListener("DOMContentLoaded", callback);
};
domReady(function() {
var myform = document.getElementById('upload_form');
var filez = document.getElementById('file');
myform.onsubmit = function(event) {
event.preventDefault();
var formData = new FormData();
var file = filez.files[0];
if(!file) {
return false;
}
formData.append("files", file, file.name);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", function(evt) {
if(evt.lengthComputable) {
var per = Math.round((evt.loaded / evt.total) * 100);
var prg = document.getElementById('prg');
prg.innerHTML = per + "%"
prg.style.width = per + "%"
prg.style.display = "block"
}
}, false);
xhr.open('POST', location.href, true);
// Set up a handler for when the request finishes.
xhr.onload = function() {
if(xhr.status === 200) {
//alert('Success');
} else {
//alert('An error occurred!');
}
};
xhr.send(formData);
}
});
</script>
</body>
</html>
)=====";