diff --git a/library.json b/library.json index 05b3e43..f11e2d0 100644 --- a/library.json +++ b/library.json @@ -12,7 +12,7 @@ "type": "git", "url": "http://git.xpablo.cz/pablo2048/WiFiConfig.git" }, - "version": "6.3.1", + "version": "6.3.2", "license": "MIT", "frameworks": "arduino", "platforms": ["espressif8266", "espressif32"], diff --git a/library.properties b/library.properties index 43dc69e..5f051e6 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=WiFiConfig -version=6.3.1 +version=6.3.2 author=Pavel Brychta maintainer=Pavel Brychta sentence=Enables seamless module configuration. diff --git a/src/WiFiConfig.cpp b/src/WiFiConfig.cpp index 8f4ec88..b2111b3 100644 --- a/src/WiFiConfig.cpp +++ b/src/WiFiConfig.cpp @@ -84,10 +84,10 @@ #include #if defined(ESP8266) - #include +#include #else - #include - #include // duvod resetu (https://github.com/espressif/arduino-esp32/issues/449) +#include +#include // duvod resetu (https://github.com/espressif/arduino-esp32/issues/449) #endif #include "WiFiConfig.h" #include @@ -96,36 +96,34 @@ #include "embHTML.h" #ifdef DEBUG_ESP_PORT - #define DEBUG_MSG(_1, ...) DEBUG_ESP_PORT.printf_P(PSTR(_1), ##__VA_ARGS__) +#define DEBUG_MSG(_1, ...) DEBUG_ESP_PORT.printf_P(PSTR(_1), ##__VA_ARGS__) #else - #define DEBUG_MSG(...) +#define DEBUG_MSG(...) #endif #if defined(ESP8266) - extern "C" { - #include "user_interface.h" - } - #define ESP_getChipId() (ESP.getChipId()) +extern "C" { +#include "user_interface.h" +} +#define ESP_getChipId() (ESP.getChipId()) #else - #include - #define ESP_getChipId() ((uint32_t)ESP.getEfuseMac()) +#include +#define ESP_getChipId() ((uint32_t)ESP.getEfuseMac()) #endif #define DNS_PORT 53 extern char WiFiDeviceName[]; -enum -{ - WIFIMODE_AP = WIFI_AP, // rezim prace jako pristupovy bod (AP) - WIFIMODE_STA = WIFI_STA, // rezim prace jako klient - WIFIMODE_AP_STA = WIFI_AP_STA // rezim prace jako klient i pristupovy bod +enum { + WIFIMODE_AP = WIFI_AP, // rezim prace jako pristupovy bod (AP) + WIFIMODE_STA = WIFI_STA, // rezim prace jako klient + WIFIMODE_AP_STA = WIFI_AP_STA // rezim prace jako klient i pristupovy bod }; -enum -{ - IPCONFIG_DHCP = 0x55, // DHCP konfigurace ip adres (default) - IPCONFIG_STATIC = 0xaa // staticka konfigurace ip adres +enum { + IPCONFIG_DHCP = 0x55, // DHCP konfigurace ip adres (default) + IPCONFIG_STATIC = 0xaa // staticka konfigurace ip adres }; static int configBase; // musi byt trvale ulozene, aby fungovaly metody pro ziskani retezcu z EEPROM @@ -143,812 +141,782 @@ const char CHECKED[] PROGMEM = "checked='checked'"; IPAddress getOurIP(void) { - IPAddress ipa; - WiFiMode_t wm = WiFi.getMode(); + IPAddress ipa; + WiFiMode_t wm = WiFi.getMode(); - switch (wm) - { - case WIFI_STA: - ipa = WiFi.localIP(); - break; + switch (wm) { + case WIFI_STA: + ipa = WiFi.localIP(); + break; - case WIFI_AP: - ipa = WiFi.softAPIP(); - break; + case WIFI_AP: + ipa = WiFi.softAPIP(); + break; - default: - ipa = IPAddress(0, 0, 0, 0); // nelze urcit ip adresu (bud je AP + STA aktivni, nebo je vypnute WiFi) - break; - } - return ipa; + default: + ipa = IPAddress(0, 0, 0, 0); // nelze urcit ip adresu (bud je AP + STA aktivni, nebo je vypnute WiFi) + break; + } + return ipa; } uint8_t * getOurMAC(uint8_t *mac) { - if (WIFIMODE_STA == EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) - return WiFi.macAddress(mac); - else - return WiFi.softAPmacAddress(mac); + if (WIFIMODE_STA == EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) + return WiFi.macAddress(mac); + else + return WiFi.softAPmacAddress(mac); } uint32_t getEEPROMuint32(unsigned int start) { - uint32_t result = 0; + uint32_t result = 0; - for (uint32_t i = 0; i < 4; ++i) - { - result <<= 8; - result += EEPROM.read(start); - ++start; - } - return result; + 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; - } + 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 = ""; + String string = ""; - for (unsigned int i = start; i < + start + len; ++i) - { - uint8_t b = EEPROM.read(i); + 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; + 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; + unsigned int si = 0; - for (unsigned int i = start; i < start + len; ++i) - { - char c; + for (unsigned int i = start; i < start + len; ++i) { + char c; - if (si < string.length()) - { - c = string[si]; + if (si < string.length()) { + c = string[si]; + } else { + c = 0; + } + EEPROM.write(i, c); + ++si; } - else - { - c = 0; - } - EEPROM.write(i, c); - ++si; - } } WiFiConfigUsrParameter::WiFiConfigUsrParameter(const char *id, const char *label, const char *defaultValue, unsigned int length, storeparam_cb cb) { - _next = NULL; - _cb = cb; - _id = id; - _label = label; - _length = length; - _value = new char[length + 1]; - for (unsigned int i = 0; i < length; i++) - { - _value[i] = 0; - } - if (defaultValue != NULL) - { - strncpy(_value, defaultValue, length); - } + _next = NULL; + _cb = cb; + _id = id; + _label = label; + _length = length; + _value = new char[length + 1]; + for (unsigned int i = 0; i < length; i++) { + _value[i] = 0; + } + if (defaultValue != NULL) { + strncpy(_value, defaultValue, length); + } } const char* WiFiConfigUsrParameter::getValue() { - return _value; + return _value; } const char* WiFiConfigUsrParameter::getID() { - return _id; + return _id; } const char* WiFiConfigUsrParameter::getLabel() { - return _label; + return _label; } int WiFiConfigUsrParameter::getValueLength() { - return _length; + return _length; } void WiFiConfigUsrParameter::setNext(WiFiConfigUsrParameter *n) { - _next = n; + _next = n; } WiFiConfigUsrParameter *WiFiConfigUsrParameter::getNext() { - return _next; + return _next; } void WiFiConfigUsrParameter::setNewValue(const char *newval) { - if (0 != strcmp(_value, newval)) - _cb(newval); + if (0 != strcmp(_value, newval)) + _cb(newval); } WiFiConfigUsrParameter *WiFiConfig::_searchUsrParameter(const char *name) { - WiFiConfigUsrParameter *ptr = _params; + WiFiConfigUsrParameter *ptr = _params; - while (NULL != ptr) - { - if (0 == strcmp(name, ptr->getID())) - break; - ptr = ptr->getNext(); - } - return ptr; + while (NULL != ptr) { + if (0 == strcmp(name, ptr->getID())) + break; + ptr = ptr->getNext(); + } + return ptr; } void WiFiConfig::addParameter(WiFiConfigUsrParameter *p) { - p->setNext(_params); - _params = p; + p->setNext(_params); + _params = p; } 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")))) - { - server->send_P(404, TEXTPLAIN, PSTR("Err")); - } - else - { - String redirect; - redirect.reserve(256); - redirect = F("http://"); - redirect.concat(server->client().localIP().toString()); - redirect.concat(F("/index.htm")); - DEBUG_MSG("Redirection to: %s\r\n", redirect.c_str()); - server->sendHeader(F("Location"), redirect, true); - server->send_P(302, TEXTPLAIN, PSTR("Redirect")); - } + if (server->uri().endsWith(String(F("favicon.ico")))) { + server->send_P(404, TEXTPLAIN, PSTR("Err")); + } else { + String redirect; + redirect.reserve(256); + redirect = F("http://"); + redirect.concat(server->client().localIP().toString()); + redirect.concat(F("/index.htm")); + DEBUG_MSG("Redirection to: %s\r\n", redirect.c_str()); + server->sendHeader(F("Location"), redirect, true); + server->send_P(302, TEXTPLAIN, PSTR("Redirect")); + } } void WiFiConfig::_handleReset(void) { - rsttick.reset(new(Ticker)); + rsttick.reset(new(Ticker)); - rsttick->once_ms(700, []() - { - ESP.restart(); - }); + rsttick->once_ms(700, []() { + ESP.restart(); + }); - server->send_P(200, TEXTHTML, PAGE_RESTART); + server->send_P(200, TEXTHTML, PAGE_RESTART); } void WiFiConfig::_handleInfo(void) { - String reply; - char buff[32]; + String reply; + char buff[32]; - reply.reserve(3000); + reply.reserve(3000); - reply = FPSTR(PAGE_INDEX1); - reply.concat(F("
System Info")); + reply = FPSTR(PAGE_INDEX1); + reply.concat(F("
System Info")); - reply.concat(F("
Core Version:")); + reply.concat(F("
Core Version:")); #if defined(ESP8266) - reply.concat(ESP.getFullVersion()); + reply.concat(ESP.getFullVersion()); #else - // TODO - reply.concat(ESP.getSdkVersion()); + // TODO + reply.concat(ESP.getSdkVersion()); #endif - reply.concat(F("
Flash Size:")); + reply.concat(F("
Flash Size:")); #if defined(ESP8266) - reply.concat(ESP.getFlashChipRealSize() / 1024); + reply.concat(ESP.getFlashChipRealSize() / 1024); #else - // TODO - reply.concat(ESP.getFlashChipSize()); + // TODO + reply.concat(ESP.getFlashChipSize()); #endif - reply.concat(F(" kB")); + reply.concat(F(" kB")); #if defined(ESP8266) - reply.concat(F("
Sketch Size/Free:")); - reply.concat(ESP.getSketchSize() / 1024); - reply.concat(F(" kB / ")); - reply.concat(ESP.getFreeSketchSpace() / 1024); - reply.concat(F(" kB")); + reply.concat(F("
Sketch Size/Free:")); + reply.concat(ESP.getSketchSize() / 1024); + reply.concat(F(" kB / ")); + reply.concat(ESP.getFreeSketchSpace() / 1024); + reply.concat(F(" kB")); #endif - reply.concat(F("
STA MAC:")); - 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("
STA MAC:")); + 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("
AP MAC:")); - 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("
AP MAC:")); + 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("
ESP Chip ID:")); - sprintf_P(buff, PSTR("%06X"), ESP_getChipId()); - reply.concat(buff); + reply.concat(F("
ESP Chip ID:")); + sprintf_P(buff, PSTR("%06X"), ESP_getChipId()); + reply.concat(buff); #if defined(ESP8266) - reply.concat(F("
Flash Chip ID:")); - sprintf_P(buff, PSTR("%08X"), ESP.getFlashChipId()); - reply.concat(buff); + reply.concat(F("
Flash Chip ID:")); + sprintf_P(buff, PSTR("%08X"), ESP.getFlashChipId()); + reply.concat(buff); #endif #if defined(ESP8266) - uint8_t stations = wifi_softap_get_station_num(); + uint8_t stations = wifi_softap_get_station_num(); - reply.concat(F("
Clients:")); - reply.concat(stations); + reply.concat(F("
Clients:")); + reply.concat(stations); #endif - reply.concat(F("
")); - server->send(200, FPSTR(TEXTHTML), reply); + reply.concat(F("
")); + server->send(200, FPSTR(TEXTHTML), reply); } void WiFiConfig::_handleRoot(void) { - String content; + String content; - content.reserve(3000); - content = FPSTR(PAGE_CAPTIVEPORTALCATCH); // 1. cast stranky + content.reserve(3000); + content = FPSTR(PAGE_CAPTIVEPORTALCATCH); // 1. cast stranky // pridame informaci o stavu pokusu o pripojeni - content.concat(F("
Pokus o připojení: ")); + content.concat(F("
Pokus o připojení: ")); #if defined(ESP8266) - switch (_status) - { - case STATION_IDLE: - content.concat(F("Klid")); - break; + switch (_status) { + case STATION_IDLE: + content.concat(F("Klid")); + break; - case STATION_CONNECTING: - content.concat(F("Připojování")); - break; + case STATION_CONNECTING: + content.concat(F("Připojování")); + break; - case STATION_WRONG_PASSWORD: - content.concat(F("Špatné heslo")); - break; + case STATION_WRONG_PASSWORD: + content.concat(F("Špatné heslo")); + break; - case STATION_NO_AP_FOUND: - content.concat(F("AP nenalezen")); - 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_CONNECT_FAIL: + content.concat(F("Připojení selhalo")); + break; - case STATION_GOT_IP: - content.concat(F("Získaná ip")); - break; + case STATION_GOT_IP: + content.concat(F("Získaná ip")); + break; - case 0xfe: - content.concat(F("Špatná EEPROM")); - break; + case 0xfe: + content.concat(F("Špatná EEPROM")); + break; - case 0xff: - content.concat(F("Vynucená konfigurace")); - break; + case 0xff: + content.concat(F("Vynucená konfigurace")); + break; - default: - content.concat(F("Neznámý")); - break; - } + default: + content.concat(F("Neznámý")); + break; + } #else - switch (_status) - { - case WL_CONNECT_FAILED: - content.concat(F("Připojení selhalo")); - break; + switch (_status) { + case WL_CONNECT_FAILED: + content.concat(F("Připojení selhalo")); + break; - case 0xfe: - content.concat(F("Špatná EEPROM")); - break; + case 0xfe: + content.concat(F("Špatná EEPROM")); + break; - case 0xff: - content.concat(F("Vynucená konfigurace")); - break; + case 0xff: + content.concat(F("Vynucená konfigurace")); + break; - default: - content.concat(F("Neznámý")); - break; - } + default: + content.concat(F("Neznámý")); + break; + } #endif - content.concat(F("
")); - 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 + content.concat(F("
")); + 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 } void WiFiConfig::_handleDisplayAP(void) { - String s; - String v; - String content; + String s; + String v; + String content; - _time = millis() + (360 * 1000); // spocitame si novy cas, kdy budeme modul restartovat (6 minut) - content.reserve(3000); - content = FPSTR(PAGE_INDEX1); - int n = WiFi.scanNetworks(); - if (0 == n) - { - content.concat(FPSTR(PAGE_NO_SSID)); - } - else - { - for (int i = 0; i < n; ++i) - { - int quality; + _time = millis() + (360 * 1000); // spocitame si novy cas, kdy budeme modul restartovat (6 minut) + content.reserve(3000); + content = FPSTR(PAGE_INDEX1); + int n = WiFi.scanNetworks(); + 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); + 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)); + 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")); + 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")); + s.replace(F("{s}"), (WIFI_AUTH_OPEN == WiFi.encryptionType(i)) ? F("") : F("l")); #endif - content.concat(s); + content.concat(s); + } } - } - s = FPSTR(PAGE_INDEX2); - v = EES_readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid)); - if (v.length()) - s.replace(F("{s}"), String(FPSTR(VALUE)) + v + F("'")); - else - s.replace(F("{s}"), F("")); - v = EES_readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass)); - if (v.length()) - s.replace(F("{p}"), String(FPSTR(VALUE)) + v + F("'")); - else - s.replace(F("{p}"), F("")); - if (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)) == WIFIMODE_AP) - s.replace(F("{a}"), FPSTR(CHECKED)); - else - s.replace(F("{a}"), F("")); - if (EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)) == IPCONFIG_STATIC) - s.replace(F("{c}"), FPSTR(CHECKED)); - else - s.replace(F("{c}"), F("")); - s.replace(F("{i}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))).toString()); - s.replace(F("{m}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))).toString()); - s.replace(F("{g}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))).toString()); - s.replace(F("{d}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns))).toString()); + s = FPSTR(PAGE_INDEX2); + v = EES_readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid)); + if (v.length()) + s.replace(F("{s}"), String(FPSTR(VALUE)) + v + F("'")); + else + s.replace(F("{s}"), F("")); + v = EES_readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass)); + if (v.length()) + s.replace(F("{p}"), String(FPSTR(VALUE)) + v + F("'")); + else + s.replace(F("{p}"), F("")); + if (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)) == WIFIMODE_AP) + s.replace(F("{a}"), FPSTR(CHECKED)); + else + s.replace(F("{a}"), F("")); + if (EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)) == IPCONFIG_STATIC) + s.replace(F("{c}"), FPSTR(CHECKED)); + else + s.replace(F("{c}"), F("")); + s.replace(F("{i}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))).toString()); + s.replace(F("{m}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))).toString()); + s.replace(F("{g}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))).toString()); + s.replace(F("{d}"), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns))).toString()); - uint8_t chan = EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)); - if ((chan < 1) || (chan > 13)) - chan = 1; // neplatne cislo kanalu nahradime nejnizsim - s.replace(F("{ch}"), String(chan)); + uint8_t chan = EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)); + if ((chan < 1) || (chan > 13)) + chan = 1; // neplatne cislo kanalu nahradime nejnizsim + s.replace(F("{ch}"), String(chan)); - content.concat(s); + content.concat(s); // Uzivatelske parametry - if (_params) - { - content.concat(FPSTR(PAGE_PARAM_HDR)); - } + if (_params) { + content.concat(FPSTR(PAGE_PARAM_HDR)); + } - WiFiConfigUsrParameter *up = _params; + 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(); - } + 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); + content.concat(FPSTR(PAGE_END)); + server->send(200, FPSTR(TEXTHTML), content); } void WiFiConfig::_handleSetAP(void) { - uint8_t mode; - String str; + uint8_t mode; + String str; - str.reserve(128); - str = server->arg(F("_s")); - if (str.length() > 0) - { - EES_storeString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), str); + str.reserve(128); + str = server->arg(F("_s")); + if (str.length() > 0) { + EES_storeString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), str); - str = server->arg(F("_p")); - EES_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str); + str = server->arg(F("_p")); + EES_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str); - str = server->arg(F("_a")); - if (str.length() > 0) - { - mode = WIFIMODE_AP; // rezim AP - str = server->arg(F("_ch")); // kanal AP - EEPROM.write(configBase + offsetof(wificonfigarea_t, apchannel), (uint8_t)str.toInt()); - } - else - mode = WIFIMODE_STA; // rezim STA - EEPROM.write(configBase + offsetof(wificonfigarea_t, mode), mode); + str = server->arg(F("_a")); + if (str.length() > 0) { + mode = WIFIMODE_AP; // rezim AP + str = server->arg(F("_ch")); // kanal AP + EEPROM.write(configBase + offsetof(wificonfigarea_t, apchannel), (uint8_t)str.toInt()); + } else + mode = WIFIMODE_STA; // rezim STA + EEPROM.write(configBase + offsetof(wificonfigarea_t, mode), mode); - str = server->arg(F("_st")); - if (0 == str.length()) - EEPROM.write(configBase + offsetof(wificonfigarea_t, ip), IPCONFIG_DHCP); // mame DHCP dynamickou konfiguraci - else - { // staticka ip konfigurace - IPAddress ipa; + str = server->arg(F("_st")); + if (0 == str.length()) + EEPROM.write(configBase + offsetof(wificonfigarea_t, ip), IPCONFIG_DHCP); // mame DHCP dynamickou konfiguraci + else { + // staticka ip konfigurace + IPAddress ipa; - EEPROM.write(configBase + offsetof(wificonfigarea_t, ip), IPCONFIG_STATIC); - str = server->arg(F("_i")); - ipa.fromString(str); - setEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr), (uint32_t) ipa); + EEPROM.write(configBase + offsetof(wificonfigarea_t, ip), IPCONFIG_STATIC); + str = server->arg(F("_i")); + ipa.fromString(str); + setEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr), (uint32_t) ipa); - str = server->arg(F("_m")); - ipa.fromString(str); - setEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask), (uint32_t) ipa); + str = server->arg(F("_m")); + ipa.fromString(str); + setEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask), (uint32_t) ipa); - str = server->arg(F("_g")); - ipa.fromString(str); - setEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway), (uint32_t) ipa); + str = server->arg(F("_g")); + ipa.fromString(str); + setEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway), (uint32_t) ipa); - str = server->arg(F("_d")); - ipa.fromString(str); - setEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns), (uint32_t) ipa); - } + str = server->arg(F("_d")); + ipa.fromString(str); + setEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns), (uint32_t) ipa); + } // Uzivatelske parametry - for (int i = 0; i < server->args(); i++) - { - if (!server->argName(i).startsWith(F("_"))) // vnitrni parametry WiFiConfig modulu zacinaji _, takze ty muzeme ignorovat - { - WiFiConfigUsrParameter *up = _searchUsrParameter(server->argName(i).c_str()); - if (NULL != up) - up->setNewValue(server->arg(i).c_str()); - } + for (int i = 0; i < server->args(); i++) { + if (!server->argName(i).startsWith(F("_"))) { // vnitrni parametry WiFiConfig modulu zacinaji _, takze ty muzeme ignorovat + WiFiConfigUsrParameter *up = _searchUsrParameter(server->argName(i).c_str()); + if (NULL != up) + up->setNewValue(server->arg(i).c_str()); + } + } + EEPROM.commit(); // skutecne ulozime data } - EEPROM.commit(); // skutecne ulozime data - } - server->send_P(200, TEXTHTML, PAGE_SAVED); - delay(2000); // cekame na odeslani dat + server->send_P(200, TEXTHTML, PAGE_SAVED); + delay(2000); // cekame na odeslani dat // nakonfigurujeme ESP dle nove nastavenych parametru - WiFi.disconnect(); // vsechno odpojime - WiFi.persistent(true); // chceme, aby si modul zapamatoval konfiguraci + WiFi.disconnect(); // vsechno odpojime + WiFi.persistent(true); // chceme, aby si modul zapamatoval konfiguraci - 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 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)); - switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) - { - case WIFIMODE_STA: - { - DEBUG_MSG("STA mode.\r\n"); + switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) { + case WIFIMODE_STA: { + DEBUG_MSG("STA mode.\r\n"); #if defined(ESP8266) - if (strlen(WiFiDeviceName)) - { - WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni - } + if (strlen(WiFiDeviceName)) { + WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni + } #endif - if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) - { - DEBUG_MSG("Static configuration.\r\n"); - WiFi.config(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), - IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns)))); - delay(100); - } - WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta - WiFi.begin(s.c_str(), pass.c_str()); + if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) { + DEBUG_MSG("Static configuration.\r\n"); + WiFi.config(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), + IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns)))); + delay(100); + } + WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta + WiFi.begin(s.c_str(), pass.c_str()); #if !defined(ESP8266) - if (strlen(WiFiDeviceName)) - { - WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni - } + if (strlen(WiFiDeviceName)) { + WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni + } #endif #if defined(ESP8266) - wifi_station_set_auto_connect(true); + wifi_station_set_auto_connect(true); #else - //esp_wifi_set_auto_connect(true); + //esp_wifi_set_auto_connect(true); #endif - delay(1000); - } - break; - - case WIFIMODE_AP: - DEBUG_MSG("AP mode.\r\n"); - WiFi.mode(WIFI_AP); // startujeme AP - if (pass.length()) - { - // je zadane heslo do AP - WiFi.softAP(s.c_str(), pass.c_str(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel))); - } - else - { - // otevreny AP - WiFi.softAP(s.c_str(), NULL, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel))); - } - if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) - { - WiFi.softAPConfig(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), - IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask)))); + delay(1000); } break; - default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???) - DEBUG_MSG("Mode Error!!\r\n"); - break; - } + case WIFIMODE_AP: + DEBUG_MSG("AP mode.\r\n"); + WiFi.mode(WIFI_AP); // startujeme AP + if (pass.length()) { + // je zadane heslo do AP + WiFi.softAP(s.c_str(), pass.c_str(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel))); + } else { + // otevreny AP + WiFi.softAP(s.c_str(), NULL, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel))); + } + if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) { + WiFi.softAPConfig(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), + IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask)))); + } + break; - delay(1000); // cekame XX sekund na ulozeni atd... - ESP.restart(); + default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???) + DEBUG_MSG("Mode Error!!\r\n"); + break; + } + + delay(1000); // cekame XX sekund na ulozeni atd... + ESP.restart(); } // Start WiFi v rezimu AP pro nastaveni modulu wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb) { - String ssid = SETUP_SSID; + String ssid = SETUP_SSID; - WiFi.persistent(false); // chranime flash pred zbytecnymi prepisy - dnsServer.reset(new DNSServer()); + WiFi.persistent(false); // chranime flash pred zbytecnymi prepisy + dnsServer.reset(new DNSServer()); #if defined(ESP8266) - server.reset(new ESP8266WebServer(80)); + server.reset(new ESP8266WebServer(80)); #else - server.reset(new WebServer(80)); + server.reset(new WebServer(80)); #endif - /* Soft AP network parameters */ - IPAddress apIP(172, 217, 28, 1); - IPAddress netMsk(255, 255, 255, 0); + /* Soft AP network parameters */ + IPAddress apIP(172, 217, 28, 1); + IPAddress netMsk(255, 255, 255, 0); - WiFi.disconnect(); // pro jistotu se odpojime - WiFi.mode(WIFI_AP); + WiFi.disconnect(); // pro jistotu se odpojime + WiFi.mode(WIFI_AP); - WiFi.softAPConfig(apIP, apIP, netMsk); - if (ssid.endsWith(F("?"))) - { - char lmac[16]; + WiFi.softAPConfig(apIP, apIP, netMsk); + if (ssid.endsWith(F("?"))) { + char lmac[16]; - sprintf_P(lmac, PSTR("%06X"), ESP_getChipId()); - ssid.replace(F("?"), String(lmac)); - } - WiFi.softAP(ssid.c_str(), NULL, SETUP_CHANNEL); - delay(500); // dulezite - jinak se nevraci spravna IP adresa !!! (udajne od verze 2.3.0 uz neni nutne) - - dnsServer->setErrorReplyCode(DNSReplyCode::NoError); - dnsServer->start(DNS_PORT, "*", WiFi.softAPIP()); // spustime tzv. Captive portal - vsechny DNS dotazy jsou smerovany na nasi ip adresu - if (cb) - cb(WCS_CONFIGSTART); // signalizujeme start konfiguracniho serveru -// Nastavime handlery weboveho serveru pro konfiguraci - server->onNotFound(std::bind(&WiFiConfig::_handleNotFound, this)); - server->on(F("/config"), std::bind(&WiFiConfig::_handleDisplayAP, this)); - server->on(F("/s"), std::bind(&WiFiConfig::_handleSetAP, this)); - server->on(F("/r"), std::bind(&WiFiConfig::_handleReset, this)); - server->on(F("/i"), std::bind(&WiFiConfig::_handleInfo, this)); - server->on(F("/index.htm"), std::bind(&WiFiConfig::_handleRoot, this)); - server->begin(); // startujeme webovy server - while (1) - { - server->handleClient(); // osetrujeme praci serveru - if (cb) - cb(WCS_CONFIGWAIT); // volame uzivatelsky callback (napr. signalizace) - dnsServer->processNextRequest(); - yield(); // procesy uvnitr systemu ESP potrebuji take svuj cas - if (_timeout) - { - if (millis() > _time) - { - DEBUG_MSG("AP timeout\r\n"); - if (cb) - cb(WCS_CONFIGTIMEOUT); // signalizujeme timeout - break; // ukoncime cekani a vracime se - } + sprintf_P(lmac, PSTR("%06X"), ESP_getChipId()); + ssid.replace(F("?"), String(lmac)); + } + WiFi.softAP(ssid.c_str(), NULL, SETUP_CHANNEL); + delay(500); // dulezite - jinak se nevraci spravna IP adresa !!! (udajne od verze 2.3.0 uz neni nutne) + + dnsServer->setErrorReplyCode(DNSReplyCode::NoError); + dnsServer->start(DNS_PORT, "*", WiFi.softAPIP()); // spustime tzv. Captive portal - vsechny DNS dotazy jsou smerovany na nasi ip adresu + if (cb) + cb(WCS_CONFIGSTART); // signalizujeme start konfiguracniho serveru +// Nastavime handlery weboveho serveru pro konfiguraci + server->onNotFound(std::bind(&WiFiConfig::_handleNotFound, this)); + server->on(F("/config"), std::bind(&WiFiConfig::_handleDisplayAP, this)); + server->on(F("/s"), std::bind(&WiFiConfig::_handleSetAP, this)); + server->on(F("/r"), std::bind(&WiFiConfig::_handleReset, this)); + server->on(F("/i"), std::bind(&WiFiConfig::_handleInfo, 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 + while (1) { + server->handleClient(); // osetrujeme praci serveru + if (cb) + cb(WCS_CONFIGWAIT); // volame uzivatelsky callback (napr. signalizace) + dnsServer->processNextRequest(); + yield(); // procesy uvnitr systemu ESP potrebuji take svuj cas + if (_timeout) { + if (millis() > _time) { + DEBUG_MSG("AP timeout\r\n"); + if (cb) + cb(WCS_CONFIGTIMEOUT); // signalizujeme timeout + break; // ukoncime cekani a vracime se + } + } } - } // tady by asi melo byt zastaveni weboveho a dns serveru... - dnsServer->stop(); - server->stop(); - return WCR_TIMEOUT; // nepripojeno, vyprsel timeout konfiguracniho AP + dnsServer->stop(); + server->stop(); + return WCR_TIMEOUT; // nepripojeno, vyprsel timeout konfiguracniho AP } // Testovani, zda se modul pripojil k AP bool WiFiConfig::_testWifi(wificonfig_cb cb) { - uint32_t startt = millis(); + uint32_t startt = millis(); #if !defined(ESP8266) // 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)); - String pass = EES_readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass)); + 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)); - switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) - { - case WIFIMODE_STA: - { - DEBUG_MSG("STA mode.\r\n"); - if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) - { - DEBUG_MSG("Static configuration.\r\n"); - WiFi.config(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), - IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns)))); - 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"); - WiFi.mode(WIFI_AP); // startujeme AP - if (pass.length()) - { - // je zadane heslo do AP - WiFi.softAP(s.c_str(), pass.c_str(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel))); - } - else - { - // otevreny AP - WiFi.softAP(s.c_str(), NULL, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel))); - } - if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) - { - WiFi.softAPConfig(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), - IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask)))); + switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) { + case WIFIMODE_STA: { + DEBUG_MSG("STA mode.\r\n"); + if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) { + DEBUG_MSG("Static configuration.\r\n"); + WiFi.config(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), + IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns)))); + 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; - default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???) - DEBUG_MSG("Mode Error!!\r\n"); - break; - } + case WIFIMODE_AP: + DEBUG_MSG("AP mode.\r\n"); + WiFi.mode(WIFI_AP); // startujeme AP + if (pass.length()) { + // je zadane heslo do AP + WiFi.softAP(s.c_str(), pass.c_str(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel))); + } else { + // otevreny AP + WiFi.softAP(s.c_str(), NULL, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel))); + } + if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip))) { + WiFi.softAPConfig(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))), + IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask)))); + } + break; + + default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???) + DEBUG_MSG("Mode Error!!\r\n"); + break; + } #endif - DEBUG_MSG("Trying to connect.\r\n"); - while ((millis() - startt) < WIFI_STA_CONNECT_TIMEOUT) - { - if (WL_CONNECTED == WiFi.status()) - { - DEBUG_MSG("Connected...\r\n"); - if (cb) - cb(WCS_CONNECTED); - return true; // jsme pripojeni + DEBUG_MSG("Trying to connect.\r\n"); + while ((millis() - startt) < WIFI_STA_CONNECT_TIMEOUT) { + if (WL_CONNECTED == WiFi.status()) { + DEBUG_MSG("Connected...\r\n"); + if (cb) + cb(WCS_CONNECTED); + return true; // jsme pripojeni + } + yield(); + if (cb) + cb(WCS_CONNECTING); // signalizujeme pokracujici pokus o spojeni } - yield(); - if (cb) - cb(WCS_CONNECTING); // signalizujeme pokracujici pokus o spojeni - } - DEBUG_MSG("Not connected!\r\n"); + DEBUG_MSG("Not connected!\r\n"); #if defined(ESP8266) - _status = wifi_station_get_connect_status(); + _status = wifi_station_get_connect_status(); #else - _status = WiFi.status(); + _status = WiFi.status(); #endif - return false; // pripojeni se nezdarilo + return false; // pripojeni se nezdarilo } wificonfigresult_t WiFiConfig::begin(int configarea, 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) - { - DEBUG_MSG("Force config.\r\n"); - _status = 0xff; - result = _setupAP(cb); - } - else - { + configBase = configarea; // pocatek konfigurace v EEPROM + if (0 == forceConfigure) { + DEBUG_MSG("Force config.\r\n"); + _status = 0xff; + result = _setupAP(cb); + } else { #if defined(ESP8266) - if (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)) != WiFi.getMode()) - { // neshoduje se rezim - musime spustit konfiguracni AP (poskozena konfigurace) - DEBUG_MSG("Wrong config (%d, %d)\r\n", EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)), WiFi.getMode()); - _status = 0xfe; - result = _setupAP(cb); - } - else + if (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)) != WiFi.getMode()) { + // neshoduje se rezim - musime spustit konfiguracni AP (poskozena konfigurace) + DEBUG_MSG("Wrong config (%d, %d)\r\n", EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)), WiFi.getMode()); + _status = 0xfe; + result = _setupAP(cb); + } else #endif - { - switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) - { - case WIFIMODE_STA: - { + { + switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))) { + case WIFIMODE_STA: { #if defined(ESP8266) - WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni + WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni #else - WiFi.setHostname(WiFiDeviceName); + WiFi.setHostname(WiFiDeviceName); #endif - 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"); - if (WC_DONT_RUN_CONFIGAP == _timeout) - result = WCR_CONFIGAP_NOT_STARTED; // nemame spoustet konfiguracni AP - vracime se hned - else - { - if (cb) - cb(WCS_CONNECTSTART); // signalizujeme zacatek pokusu o pripojeni - if (!_testWifi(cb)) - { - result = _setupAP(cb); // modul se nepripojil - startujeme AP rezim - } - } - } - break; + 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"); + if (WC_DONT_RUN_CONFIGAP == _timeout) + result = WCR_CONFIGAP_NOT_STARTED; // nemame spoustet konfiguracni AP - vracime se hned + else { + if (cb) + cb(WCS_CONNECTSTART); // signalizujeme zacatek pokusu o pripojeni + if (!_testWifi(cb)) { + result = _setupAP(cb); // modul se nepripojil - startujeme AP rezim + } + } + } + break; - case WIFIMODE_AP: - DEBUG_MSG("AP mode.\r\n"); - if (cb) - cb(WCS_CONNECTSTART); // signalizujeme zacatek pokusu o pripojeni (zde se nic jineho stejne nestane...) - break; + case WIFIMODE_AP: + DEBUG_MSG("AP mode.\r\n"); + if (cb) + cb(WCS_CONNECTSTART); // signalizujeme zacatek pokusu o pripojeni (zde se nic jineho stejne nestane...) + break; #if !defined(ESP8266) - default: - DEBUG_MSG("Wrong config (%d)\r\n", EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))); - _status = 0xfe; - result = _setupAP(cb); - break; + default: + DEBUG_MSG("Wrong config (%d)\r\n", EEPROM.read(configBase + offsetof(wificonfigarea_t, mode))); + _status = 0xfe; + result = _setupAP(cb); + break; #endif - } + } + } } - } - return result; // mame vyreseno + return result; // mame vyreseno } wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb = NULL) { - if ((timeout > 0) && (40 > timeout)) - timeout = 40; // timeout musi byt minimalne 40 sekund - if ((0 == forceConfigure) && (WC_DONT_RUN_CONFIGAP == timeout)) - timeout = 40; - _timeout = timeout; - if (_timeout > 0) - _time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat - return begin(configarea, forceConfigure, cb); // spustime WiFi + if ((timeout > 0) && (40 > timeout)) + timeout = 40; // timeout musi byt minimalne 40 sekund + if ((0 == forceConfigure) && (WC_DONT_RUN_CONFIGAP == timeout)) + timeout = 40; + _timeout = timeout; + if (_timeout > 0) + _time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat + return begin(configarea, forceConfigure, cb); // spustime WiFi } // EOF diff --git a/src/WiFiConfig.h b/src/WiFiConfig.h index 0dd0872..c5179de 100644 --- a/src/WiFiConfig.h +++ b/src/WiFiConfig.h @@ -31,9 +31,9 @@ #define __WiFiConfig_h__ #if defined(ESP8266) - #include +#include #else - #include +#include #endif #include @@ -42,52 +42,49 @@ #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) +typedef struct { + uint8_t mode; // rezim prace AP/STA + uint8_t ip; // konfigurace ip (staticka/DHCP) + char ssid[32 + 2]; // SSID site + char pass[64 + 2]; // heslo + uint32_t ipaddr; // ip adresa v pripade staticke konfigurace + uint32_t netmask; // sitova maska v pripade staticke konfigurace + uint32_t gateway; // sitova brana v pripade staticke konfigurace + uint32_t dns; // ip adresa DNS serveru v pripade staticke konfigurace + uint8_t apchannel; // kanal, na kterem pracuje AP (pokud je zapnuty rezim AP) } wificonfigarea_t; // Parametr, predany uzivatelske callback funkci, urceny pro aplikacni vizualizaci stavu konfigurace a pripojeni -typedef enum -{ - WCS_CONNECTSTART = 0, // zacatek pokusu o pripojeni k ulozene konfiguraci - WCS_CONNECTING = 1, // probiha pokus o pripojeni - WCS_CONNECTED = 2, // pripojeni bylo uspesne - WCS_CONFIGSTART = 3, // zacatek startu konfiguracniho AP - WCS_CONFIGWAIT = 4, // cekame na nastaveni konfigurace pres web - WCS_CONFIGTIMEOUT = 5, // doslo k vyprseni timeoutu konfigurace, budeme se vracet s False jako vysledek z .begin(...) -}wificonfigstate_t; +typedef enum { + WCS_CONNECTSTART = 0, // zacatek pokusu o pripojeni k ulozene konfiguraci + WCS_CONNECTING = 1, // probiha pokus o pripojeni + WCS_CONNECTED = 2, // pripojeni bylo uspesne + WCS_CONFIGSTART = 3, // zacatek startu konfiguracniho AP + WCS_CONFIGWAIT = 4, // cekame na nastaveni konfigurace pres web + WCS_CONFIGTIMEOUT = 5, // doslo k vyprseni timeoutu konfigurace, budeme se vracet s False jako vysledek z .begin(...) +} wificonfigstate_t; // Navratovy parametr z volani begin() - udava, jak se podarilo WiFiConfig modulu pripojit k AP -typedef enum -{ - WCR_OK = 0, // wifi pripojena/AP nastartovane (dle parametru v EEPROM) - 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) -}wificonfigresult_t; +typedef enum { + WCR_OK = 0, // wifi pripojena/AP nastartovane (dle parametru v EEPROM) + 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) +} wificonfigresult_t; typedef void (*wificonfig_cb)(wificonfigstate_t state); // definice callbacku typedef void (*storeparam_cb)(const char *newvalue); // callback pro ulozeni uzivatelskeho parametru #ifndef WIFI_STA_CONNECT_TIMEOUT - #define WIFI_STA_CONNECT_TIMEOUT 15000UL // delka cekani na pripojeni k AP [ms] +#define WIFI_STA_CONNECT_TIMEOUT 15000UL // delka cekani na pripojeni k AP [ms] #endif #ifndef SETUP_SSID - #define SETUP_SSID "ESPPBSetup_?" +#define SETUP_SSID "ESPPBSetup_?" #endif #ifndef SETUP_CHANNEL - #define SETUP_CHANNEL 3 +#define SETUP_CHANNEL 3 #endif /** @@ -144,24 +141,24 @@ void setEEPROMuint32(int start, uint32_t val); class WiFiConfigUsrParameter { - public: +public: WiFiConfigUsrParameter(const char *id, const char *label, const char *defaultValue, unsigned int length, storeparam_cb cb); const char *getID(); const char *getValue(); const char *getLabel(); int getValueLength(); - void setNext(WiFiConfigUsrParameter *n); - WiFiConfigUsrParameter *getNext(); - void setNewValue(const char *newval); - private: + void setNext(WiFiConfigUsrParameter *n); + WiFiConfigUsrParameter *getNext(); + void setNewValue(const char *newval); +private: const char *_id; const char *_label; char *_value; int _length; - storeparam_cb _cb; - WiFiConfigUsrParameter *_next; + storeparam_cb _cb; + WiFiConfigUsrParameter *_next; friend class WiFiConfig; }; @@ -169,26 +166,26 @@ class WiFiConfigUsrParameter class WiFiConfig { public: - WiFiConfig(): _params(NULL), _timeout(0) {} - wificonfigresult_t begin(int configarea, int forceConfigure, wificonfig_cb cb); - wificonfigresult_t begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb); - void addParameter(WiFiConfigUsrParameter *p); + WiFiConfig(): _params(NULL), _timeout(0) {} + wificonfigresult_t begin(int configarea, int forceConfigure, wificonfig_cb cb); + wificonfigresult_t begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb); + void addParameter(WiFiConfigUsrParameter *p); private: - wificonfigresult_t _setupAP(wificonfig_cb cb); - void _handleDisplayAP(void); - void _handleSetAP(void); - void _handleInfo(void); - void _handleReset(void); - void _handleNotFound(); // CaptivePortal redirector - void _handleRoot(); // jen jednoducha stranka kvuli CaptivePortalu umoznuje prejit na spravnou stranku (ale nedela to...) - bool _testWifi(wificonfig_cb cb); - bool _storeString(unsigned int start, size_t maxlen, String &string); // sifrovany retezec, max 255 znaku - String _readString(unsigned int start, size_t maxlen); // sifrovany retezec, max. 255 znaku - WiFiConfigUsrParameter *_searchUsrParameter(const char *name); + wificonfigresult_t _setupAP(wificonfig_cb cb); + void _handleDisplayAP(void); + void _handleSetAP(void); + void _handleInfo(void); + void _handleReset(void); + void _handleNotFound(); // CaptivePortal redirector + void _handleRoot(); // jen jednoducha stranka kvuli CaptivePortalu umoznuje prejit na spravnou stranku (ale nedela to...) + bool _testWifi(wificonfig_cb cb); + bool _storeString(unsigned int start, size_t maxlen, String &string); // sifrovany retezec, max 255 znaku + String _readString(unsigned int start, size_t maxlen); // sifrovany retezec, max. 255 znaku + WiFiConfigUsrParameter *_searchUsrParameter(const char *name); - WiFiConfigUsrParameter *_params; // ukazatel na posledni zadany uzivatelsky parametr - int _timeout; // timeout pri cekani na konfiguraci - uint32_t _time; // hodnota, po ktere bude ESP restartovano (pokud je _timeout != 0) - uint8_t _status; // stav pripojeni - je pouzity pro signalizaci duvodu, proc se nepripoji k AP + WiFiConfigUsrParameter *_params; // ukazatel na posledni zadany uzivatelsky parametr + int _timeout; // timeout pri cekani na konfiguraci + uint32_t _time; // hodnota, po ktere bude ESP restartovano (pokud je _timeout != 0) + uint8_t _status; // stav pripojeni - je pouzity pro signalizaci duvodu, proc se nepripoji k AP }; #endif diff --git a/src/embHTML.h b/src/embHTML.h index a997198..13e6aff 100644 --- a/src/embHTML.h +++ b/src/embHTML.h @@ -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 hSC(){if (document.getElementById('_st').checked){document.getElementById('staticip').style.display = 'block';}else{document.getElementById('staticip').style.display = 'none';}} -

ESP8266 WiFiConfig

+

ESP WiFiConfig

)====="; /* Polozky nalezenych SSID @@ -119,10 +119,22 @@ static const char PAGE_CAPTIVEPORTALCATCH[] PROGMEM = R"=====(
-



-)====="; +
+ +

+
+ +

+

)=====" +#if defined USE_WIFICONFIG_OTAUPDATE +R"=====( +
+

)=====" +#endif +; // druha cast (mezi 1. a 2. cast muzeme doplnit dodatecne informace) static const char PAGE_CAPTIVEPORTALCATCH2[] PROGMEM = R"=====( @@ -150,3 +162,58 @@ static const char PAGE_RESTART[] PROGMEM = R"=====( Restartuji... )====="; + +static const char PAGE_UPDATE[] PROGMEM = R"=====( + + + + + + +
+ +
+
+ +
+ + + +)=====";