Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
81ceb9aca9 | |||
33f8f6a8b8 | |||
f5fce8dee3 | |||
8c23c4eddb | |||
51ae416dd0 | |||
a742f624fb | |||
242238a1f0 | |||
f4f8915197 | |||
68083ee040 | |||
7fdca6fa92 | |||
febbcefcb6 | |||
d2134e6397 | |||
d99c4dc11a | |||
36b213efde | |||
ef4be58c76 | |||
7f540c3c7a | |||
4c4f27667c |
@ -1,3 +1,5 @@
|
|||||||
# WiFiConfig
|
# WiFiConfig
|
||||||
|
|
||||||
Knihovna pro konfiguraci WiFi rozhraní modulu ESP8266
|

|
||||||
|
|
||||||
|
Knihovna pro konfiguraci WiFi rozhraní modulu ESP8266 a ESP32, obsahující Captive portal.
|
BIN
doc/images/Screenshot_20180507-100906.png
Normal file
BIN
doc/images/Screenshot_20180507-100906.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 56 KiB |
@ -2,8 +2,8 @@
|
|||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
#include "src/espnbns/espnbns.h"
|
#include <ESP8266NetBIOS.h>
|
||||||
#include "src/WiFiConfig/WiFiConfig.h"
|
#include <WiFiConfig.h>
|
||||||
|
|
||||||
// Plati pro desticku Witty s modulem ESP-12E
|
// Plati pro desticku Witty s modulem ESP-12E
|
||||||
#define PIN_FORCE_CONFIG 4
|
#define PIN_FORCE_CONFIG 4
|
||||||
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
ESP8266WebServer wwwserver(80); // webovy server
|
ESP8266WebServer wwwserver(80); // webovy server
|
||||||
String content;
|
String content;
|
||||||
NBNS nbns; // Netbios (xPablo.cz)
|
|
||||||
|
|
||||||
static void handleRoot(void)
|
static void handleRoot(void)
|
||||||
{
|
{
|
||||||
@ -77,7 +76,7 @@ void ICACHE_FLASH_ATTR setup()
|
|||||||
|
|
||||||
if (strlen(WiFiDeviceName) > 0)
|
if (strlen(WiFiDeviceName) > 0)
|
||||||
{
|
{
|
||||||
nbns.begin(WiFiDeviceName);
|
NBNS.begin(WiFiDeviceName);
|
||||||
ArduinoOTA.setHostname(WiFiDeviceName);
|
ArduinoOTA.setHostname(WiFiDeviceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,5 +124,4 @@ void loop()
|
|||||||
{
|
{
|
||||||
wwwserver.handleClient(); // osetrujeme praci serveru
|
wwwserver.handleClient(); // osetrujeme praci serveru
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
nbns.poll();
|
|
||||||
}
|
}
|
||||||
|
22
library.json
Normal file
22
library.json
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name":"WiFiConfig",
|
||||||
|
"description":"Web based WiFi part configuration for the ESP8266 and ESP32 SoC",
|
||||||
|
"keywords":"wifi, configuration",
|
||||||
|
"authors":
|
||||||
|
{
|
||||||
|
"name": "Pavel Brychta",
|
||||||
|
"maintainer": true
|
||||||
|
},
|
||||||
|
"repository":
|
||||||
|
{
|
||||||
|
"type": "git",
|
||||||
|
"url": "http://git.xpablo.cz/pablo2048/WiFiConfig.git"
|
||||||
|
},
|
||||||
|
"version": "6.0",
|
||||||
|
"license": "MIT",
|
||||||
|
"frameworks": "arduino",
|
||||||
|
"platforms": ["espressif8266", "espressif32"],
|
||||||
|
"build": {
|
||||||
|
"libCompatMode": 2
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
name=WiFiConfig
|
name=WiFiConfig
|
||||||
version=5.4.0
|
version=6.0
|
||||||
author=Pavel Brychta
|
author=Pavel Brychta
|
||||||
maintainer=Pavel Brychta <Pablo@xpablo.cz>
|
maintainer=Pavel Brychta <Pablo@xpablo.cz>
|
||||||
sentence=Enables seamless module configuration.
|
sentence=Enables seamless module configuration.
|
||||||
paragraph=With this library you can enable your ESP8266 module to be configured over network. Includes Captive Portal for easy configuration.
|
paragraph=With this library you can enable your ESP8266 or ESP32 module to be configured over WiFi. Includes Captive Portal for easy configuration.
|
||||||
category=Other
|
category=Other
|
||||||
url=http://www.xpablo.cz
|
url=http://www.xpablo.cz
|
||||||
architectures=esp8266
|
architectures=esp8266,esp32
|
||||||
|
@ -1,4 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
|
* 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.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.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
|
||||||
@ -56,9 +69,13 @@
|
|||||||
* Prejit na pripojovani pomoci vnitrnich mechanizmu SDK ESP - zvysi se tim rychlost pripojeni kvuli bateriovym zarizenim
|
* Prejit na pripojovani pomoci vnitrnich mechanizmu SDK ESP - zvysi se tim rychlost pripojeni kvuli bateriovym zarizenim
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "WiFiConfig.h"
|
|
||||||
#include <ESP8266WebServer.h>
|
|
||||||
#include <DNSServer.h>
|
#include <DNSServer.h>
|
||||||
|
#if defined(ESP8266)
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
#else
|
||||||
|
#include <WebServer.h>
|
||||||
|
#endif
|
||||||
|
#include "WiFiConfig.h"
|
||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
#include <Ticker.h>
|
#include <Ticker.h>
|
||||||
#include "embHTML.h"
|
#include "embHTML.h"
|
||||||
@ -69,9 +86,15 @@
|
|||||||
#define DEBUG_MSG(...)
|
#define DEBUG_MSG(...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern "C" {
|
#if defined(ESP8266)
|
||||||
|
extern "C" {
|
||||||
#include "user_interface.h"
|
#include "user_interface.h"
|
||||||
}
|
}
|
||||||
|
#define ESP_getChipId() (ESP.getChipId())
|
||||||
|
#else
|
||||||
|
#include <esp_wifi.h>
|
||||||
|
#define ESP_getChipId() ((uint32_t)ESP.getEfuseMac())
|
||||||
|
#endif
|
||||||
|
|
||||||
#define DNS_PORT 53
|
#define DNS_PORT 53
|
||||||
|
|
||||||
@ -93,7 +116,11 @@ enum
|
|||||||
static int configBase; // musi byt trvale ulozene, aby fungovaly metody pro ziskani retezcu z EEPROM
|
static int configBase; // musi byt trvale ulozene, aby fungovaly metody pro ziskani retezcu z EEPROM
|
||||||
|
|
||||||
static std::unique_ptr<DNSServer> dnsServer;
|
static std::unique_ptr<DNSServer> dnsServer;
|
||||||
static std::unique_ptr<ESP8266WebServer> server;
|
#if defined(ESP8266)
|
||||||
|
static std::unique_ptr<ESP8266WebServer> server;
|
||||||
|
#else
|
||||||
|
static std::unique_ptr<WebServer> server;
|
||||||
|
#endif
|
||||||
static std::unique_ptr<Ticker> rsttick;
|
static std::unique_ptr<Ticker> rsttick;
|
||||||
|
|
||||||
const char VALUE[] PROGMEM = "value='";
|
const char VALUE[] PROGMEM = "value='";
|
||||||
@ -130,11 +157,11 @@ uint8_t * getOurMAC(uint8_t *mac)
|
|||||||
return WiFi.softAPmacAddress(mac);
|
return WiFi.softAPmacAddress(mac);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getEEPROMuint32(int start)
|
uint32_t getEEPROMuint32(unsigned int start)
|
||||||
{
|
{
|
||||||
uint32_t result = 0;
|
uint32_t result = 0;
|
||||||
|
|
||||||
for (int 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);
|
||||||
@ -143,36 +170,36 @@ uint32_t getEEPROMuint32(int start)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setEEPROMuint32(int start, uint32_t val)
|
void setEEPROMuint32(unsigned int start, uint32_t val)
|
||||||
{
|
{
|
||||||
|
|
||||||
for (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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String getEEPROMString(int start, int len)
|
String getEEPROMString(unsigned int start, size_t len)
|
||||||
{
|
{
|
||||||
String string = "";
|
String string = "";
|
||||||
|
|
||||||
for (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))
|
||||||
break;
|
break;
|
||||||
string.concat(char(b));
|
string += char(b);
|
||||||
}
|
}
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setEEPROMString(int start, int len, String string)
|
void setEEPROMString(unsigned int start, size_t len, String &string)
|
||||||
{
|
{
|
||||||
unsigned int si = 0;
|
unsigned int si = 0;
|
||||||
|
|
||||||
for (int i = start; i < start + len; ++i)
|
for (unsigned int i = start; i < start + len; ++i)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
|
|
||||||
@ -189,7 +216,7 @@ void setEEPROMString(int start, int len, String string)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WiFiConfigUsrParameter::WiFiConfigUsrParameter(const char *id, const char *label, const char *defaultValue, int length, storeparam_cb cb)
|
WiFiConfigUsrParameter::WiFiConfigUsrParameter(const char *id, const char *label, const char *defaultValue, unsigned int length, storeparam_cb cb)
|
||||||
{
|
{
|
||||||
_next = NULL;
|
_next = NULL;
|
||||||
_cb = cb;
|
_cb = cb;
|
||||||
@ -197,7 +224,7 @@ 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 (int i = 0; i < length; i++)
|
for (unsigned int i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
_value[i] = 0;
|
_value[i] = 0;
|
||||||
}
|
}
|
||||||
@ -272,18 +299,16 @@ void WiFiConfig::addParameter(WiFiConfigUsrParameter *p)
|
|||||||
void WiFiConfig::_handleNotFound(void)
|
void WiFiConfig::_handleNotFound(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
// _time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
|
|
||||||
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(404, F("text/plain"), F("Err"));
|
server->send_P(404, TEXTPLAIN, PSTR("Err"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
server->sendHeader(F("Location"), String(F("http://")) + server->client().localIP().toString() + String(F("/index.htm")), true);
|
server->sendHeader(F("Location"), String(F("http://")) + server->client().localIP().toString() + String(F("/index.htm")), true);
|
||||||
// server->sendHeader(F("Location"), String(F("http://")) + WiFi.softAPIP().toString() + String(F("/index.htm")), true);
|
server->send_P(302, TEXTPLAIN, PSTR("Redirect"));
|
||||||
server->send (302, F("text/plain"), F("Redirect"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -303,6 +328,7 @@ void WiFiConfig::_handleReset(void)
|
|||||||
void WiFiConfig::_handleInfo(void)
|
void WiFiConfig::_handleInfo(void)
|
||||||
{
|
{
|
||||||
String reply;
|
String reply;
|
||||||
|
char buff[32];
|
||||||
|
|
||||||
reply.reserve(3000);
|
reply.reserve(3000);
|
||||||
|
|
||||||
@ -310,20 +336,32 @@ void WiFiConfig::_handleInfo(void)
|
|||||||
reply.concat(F("<form><table><TR><TH>System Info<TH>"));
|
reply.concat(F("<form><table><TR><TH>System Info<TH>"));
|
||||||
|
|
||||||
reply.concat(F("<TR><TD>Core Version:<TD>"));
|
reply.concat(F("<TR><TD>Core Version:<TD>"));
|
||||||
reply.concat(ESP.getCoreVersion());
|
#if defined(ESP8266)
|
||||||
|
reply.concat(ESP.getFullVersion());
|
||||||
|
#else
|
||||||
|
// TODO
|
||||||
|
reply.concat(ESP.getSdkVersion());
|
||||||
|
#endif
|
||||||
|
|
||||||
reply.concat(F("<TR><TD>Flash Size:<TD>"));
|
reply.concat(F("<TR><TD>Flash Size:<TD>"));
|
||||||
|
#if defined(ESP8266)
|
||||||
reply.concat(ESP.getFlashChipRealSize() / 1024);
|
reply.concat(ESP.getFlashChipRealSize() / 1024);
|
||||||
|
#else
|
||||||
|
// TODO
|
||||||
|
reply.concat(ESP.getFlashChipSize());
|
||||||
|
#endif
|
||||||
reply.concat(F(" kB"));
|
reply.concat(F(" kB"));
|
||||||
|
|
||||||
|
#if defined(ESP8266)
|
||||||
reply.concat(F("<TR><TD>Sketch Size/Free:<TD>"));
|
reply.concat(F("<TR><TD>Sketch Size/Free:<TD>"));
|
||||||
reply.concat(ESP.getSketchSize() / 1024);
|
reply.concat(ESP.getSketchSize() / 1024);
|
||||||
reply.concat(F(" kB / "));
|
reply.concat(F(" kB / "));
|
||||||
reply.concat(ESP.getFreeSketchSpace() / 1024);
|
reply.concat(ESP.getFreeSketchSpace() / 1024);
|
||||||
reply.concat(F(" kB"));
|
reply.concat(F(" kB"));
|
||||||
|
#endif
|
||||||
|
|
||||||
reply.concat(F("<TR><TD>STA MAC:<TD>"));
|
reply.concat(F("<TR><TD>STA MAC:<TD>"));
|
||||||
uint8_t mac[] = {0, 0, 0, 0, 0, 0};
|
uint8_t mac[6];
|
||||||
uint8_t* macread = WiFi.macAddress(mac);
|
uint8_t* macread = WiFi.macAddress(mac);
|
||||||
char macaddress[20];
|
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]);
|
sprintf_P(macaddress, PSTR("%02x:%02x:%02x:%02x:%02x:%02x"), macread[0], macread[1], macread[2], macread[3], macread[4], macread[5]);
|
||||||
@ -335,13 +373,29 @@ void WiFiConfig::_handleInfo(void)
|
|||||||
reply.concat(macaddress);
|
reply.concat(macaddress);
|
||||||
|
|
||||||
reply.concat(F("<TR><TD>ESP Chip ID:<TD>"));
|
reply.concat(F("<TR><TD>ESP Chip ID:<TD>"));
|
||||||
reply.concat(ESP.getChipId());
|
sprintf_P(buff, PSTR("%06X"), ESP_getChipId());
|
||||||
|
reply.concat(buff);
|
||||||
|
|
||||||
|
#if defined(ESP8266)
|
||||||
reply.concat(F("<TR><TD>Flash Chip ID:<TD>"));
|
reply.concat(F("<TR><TD>Flash Chip ID:<TD>"));
|
||||||
reply.concat(ESP.getFlashChipId());
|
sprintf_P(buff, PSTR("%08X"), ESP.getFlashChipId());
|
||||||
|
reply.concat(buff);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
reply.concat(F("<TR><TD>Timeout:<TD>"));
|
||||||
|
reply.concat(_timeout);
|
||||||
|
reply.concat(F("/"));
|
||||||
|
reply.concat(_time);
|
||||||
|
|
||||||
|
#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>"));
|
reply.concat(F("</table></form>"));
|
||||||
server->send(200, TEXTHTML, reply);
|
server->send(200, FPSTR(TEXTHTML), reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiConfig::_handleRoot(void)
|
void WiFiConfig::_handleRoot(void)
|
||||||
@ -352,6 +406,7 @@ void WiFiConfig::_handleRoot(void)
|
|||||||
content = FPSTR(PAGE_CAPTIVEPORTALCATCH); // 1. cast stranky
|
content = FPSTR(PAGE_CAPTIVEPORTALCATCH); // 1. cast stranky
|
||||||
// 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)
|
||||||
switch (_status)
|
switch (_status)
|
||||||
{
|
{
|
||||||
case STATION_IDLE:
|
case STATION_IDLE:
|
||||||
@ -387,12 +442,15 @@ void WiFiConfig::_handleRoot(void)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
content.concat(F("Neznámého"));
|
content.concat(F("Neznámý"));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
content.concat(F("Prozatím nedostupné"));
|
||||||
|
#endif
|
||||||
content.concat(F("</div>"));
|
content.concat(F("</div>"));
|
||||||
content.concat(FPSTR(PAGE_CAPTIVEPORTALCATCH2)); // 2. cast stranky (ukonceni)
|
content.concat(FPSTR(PAGE_CAPTIVEPORTALCATCH2)); // 2. cast stranky (ukonceni)
|
||||||
server->send(200, TEXTHTML, content);
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -426,22 +484,26 @@ void WiFiConfig::_handleDisplayAP(void)
|
|||||||
s = FPSTR(SSID_ITEM);
|
s = FPSTR(SSID_ITEM);
|
||||||
s.replace(F("{v}"), WiFi.SSID(i));
|
s.replace(F("{v}"), WiFi.SSID(i));
|
||||||
s.replace(F("{a}"), String(quality));
|
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"));
|
||||||
|
#endif
|
||||||
content.concat(s);
|
content.concat(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s = FPSTR(PAGE_INDEX2);
|
s = FPSTR(PAGE_INDEX2);
|
||||||
v = getEEPROMString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
v = _readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
||||||
if (v.length())
|
if (v.length())
|
||||||
s.replace(F("{s}"), String(FPSTR(VALUE)) + v + F("'"));
|
s.replace(F("{s}"), String(FPSTR(VALUE)) + v + F("'"));
|
||||||
else
|
else
|
||||||
s.replace(F("{s}"), F("placeholder='SSID'"));
|
s.replace(F("{s}"), F("placeholder='SSID'"));
|
||||||
v = getEEPROMString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
|
v = _readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
|
||||||
if (v.length())
|
if (v.length())
|
||||||
s.replace(F("{p}"), String(FPSTR(VALUE)) + v + F("'"));
|
s.replace(F("{p}"), String(FPSTR(VALUE)) + v + F("'"));
|
||||||
else
|
else
|
||||||
s.replace(F("{p}"), F("placeholder='password'"));
|
s.replace(F("{p}"), F("placeholder='password'"));
|
||||||
v = getEEPROMString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
v = _readString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
||||||
if (v.length())
|
if (v.length())
|
||||||
s.replace(F("{n}"), String(FPSTR(VALUE)) + v + F("'"));
|
s.replace(F("{n}"), String(FPSTR(VALUE)) + v + F("'"));
|
||||||
else
|
else
|
||||||
@ -486,15 +548,7 @@ void WiFiConfig::_handleDisplayAP(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
content.concat(FPSTR(PAGE_END));
|
content.concat(FPSTR(PAGE_END));
|
||||||
server->send(200, TEXTHTML, content);
|
server->send(200, FPSTR(TEXTHTML), content);
|
||||||
}
|
|
||||||
|
|
||||||
// Deal with (potentially) plus-encoded ssid/pass
|
|
||||||
void WiFiConfig::_plusDecode(String &s)
|
|
||||||
{
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < s.length(); i++)
|
|
||||||
s[i] = (s[i] == '+' ? ' ' : s[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiConfig::_handleSetAP(void)
|
void WiFiConfig::_handleSetAP(void)
|
||||||
@ -506,15 +560,13 @@ void WiFiConfig::_handleSetAP(void)
|
|||||||
str = server->arg(F("_s"));
|
str = server->arg(F("_s"));
|
||||||
if (str.length() > 0)
|
if (str.length() > 0)
|
||||||
{
|
{
|
||||||
_plusDecode(str);
|
_storeString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), str);
|
||||||
setEEPROMString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), str);
|
|
||||||
|
|
||||||
str = server->arg(F("_p"));
|
str = server->arg(F("_p"));
|
||||||
_plusDecode(str);
|
_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str);
|
||||||
setEEPROMString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str);
|
|
||||||
|
|
||||||
str = server->arg(F("_n"));
|
str = server->arg(F("_n"));
|
||||||
setEEPROMString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname), str);
|
_storeString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname), str);
|
||||||
|
|
||||||
str = server->arg(F("_a"));
|
str = server->arg(F("_a"));
|
||||||
if (str.length() > 0)
|
if (str.length() > 0)
|
||||||
@ -564,33 +616,50 @@ void WiFiConfig::_handleSetAP(void)
|
|||||||
}
|
}
|
||||||
EEPROM.commit(); // skutecne ulozime data
|
EEPROM.commit(); // skutecne ulozime data
|
||||||
}
|
}
|
||||||
server->send(200, F("text/html"), FPSTR(PAGE_SAVED));
|
server->send_P(200, TEXTHTML, PAGE_SAVED);
|
||||||
delay(2000); // cekame na odeslani dat
|
delay(2000); // cekame na odeslani dat
|
||||||
// nakonfigurujeme ESP dle nove nastavenych parametru
|
// nakonfigurujeme ESP dle nove nastavenych parametru
|
||||||
WiFi.disconnect(); // vsechno odpojime
|
WiFi.disconnect(); // vsechno odpojime
|
||||||
WiFi.persistent(true); // chceme, aby si modul zapamatoval konfiguraci
|
WiFi.persistent(true); // chceme, aby si modul zapamatoval konfiguraci
|
||||||
|
|
||||||
String s = getEEPROMString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
String s = _readString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
||||||
strcpy(WiFiDeviceName, s.c_str());
|
strcpy(WiFiDeviceName, s.c_str());
|
||||||
s = getEEPROMString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
s = _readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
||||||
String pass = getEEPROMString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
|
String pass = _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");
|
||||||
WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta
|
#if defined(ESP8266)
|
||||||
if (strlen(WiFiDeviceName))
|
if (strlen(WiFiDeviceName))
|
||||||
|
{
|
||||||
WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni
|
WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni
|
||||||
WiFi.begin(s.c_str(), pass.c_str());
|
}
|
||||||
|
#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))));
|
||||||
|
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
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if defined(ESP8266)
|
||||||
wifi_station_set_auto_connect(true);
|
wifi_station_set_auto_connect(true);
|
||||||
|
#else
|
||||||
|
//esp_wifi_set_auto_connect(true);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
delay(1000);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -598,14 +667,20 @@ void WiFiConfig::_handleSetAP(void)
|
|||||||
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))));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???)
|
default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???)
|
||||||
@ -624,7 +699,11 @@ 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.reset(new ESP8266WebServer(80));
|
server.reset(new ESP8266WebServer(80));
|
||||||
|
#else
|
||||||
|
server.reset(new WebServer(80));
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Soft AP network parameters */
|
/* Soft AP network parameters */
|
||||||
IPAddress apIP(192, 168, 4, 1);
|
IPAddress apIP(192, 168, 4, 1);
|
||||||
@ -642,7 +721,7 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
|
|||||||
//WiFi.softAPmacAddress(amac);
|
//WiFi.softAPmacAddress(amac);
|
||||||
//sprintf_P(lmac, PSTR("%02X%02X%02X"), amac[3], amac[4], amac[5]);
|
//sprintf_P(lmac, PSTR("%02X%02X%02X"), amac[3], amac[4], amac[5]);
|
||||||
|
|
||||||
sprintf_P(lmac, PSTR("%06X"), ESP.getChipId());
|
sprintf_P(lmac, PSTR("%06X"), ESP_getChipId());
|
||||||
ssid.replace(F("?"), String(lmac));
|
ssid.replace(F("?"), String(lmac));
|
||||||
}
|
}
|
||||||
WiFi.softAP(ssid.c_str(), NULL, SETUP_CHANNEL);
|
WiFi.softAP(ssid.c_str(), NULL, SETUP_CHANNEL);
|
||||||
@ -684,11 +763,142 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
|
|||||||
return WCR_TIMEOUT; // nepripojeno, vyprsel timeout konfiguracniho AP
|
return WCR_TIMEOUT; // nepripojeno, vyprsel timeout konfiguracniho AP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String WiFiConfig::_readString(unsigned int start, size_t maxlen)
|
||||||
|
{
|
||||||
|
uint8_t key[4];
|
||||||
|
unsigned int enciphidx = 0;
|
||||||
|
String result;
|
||||||
|
unsigned int top = EEPROM.read(start);
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
result.reserve(maxlen);
|
||||||
|
uint32_t cid = ESP_getChipId();
|
||||||
|
uint32_t *pkey = (uint32_t *)&key[0];
|
||||||
|
*pkey = cid;
|
||||||
|
DEBUG_MSG("Key = %02X:%02X:%02X:%02X\r\n", key[0], key[1], key[2], key[3]);
|
||||||
|
if ((top > 0) && (top < (maxlen - 2)))
|
||||||
|
{
|
||||||
|
uint8_t b;
|
||||||
|
for (i = start + 1; i < + start + top + 1; ++i)
|
||||||
|
{
|
||||||
|
b = EEPROM.read(i);
|
||||||
|
|
||||||
|
b ^= key[enciphidx];
|
||||||
|
++enciphidx;
|
||||||
|
enciphidx %= sizeof(key);
|
||||||
|
|
||||||
|
result.concat(char(b));
|
||||||
|
}
|
||||||
|
b = EEPROM.read(i) ^ key[enciphidx];
|
||||||
|
if (b != 0)
|
||||||
|
{
|
||||||
|
DEBUG_MSG("_readString error! (%s)\r\n", result.c_str());
|
||||||
|
result = F(""); // spatna ukoncovaci nula - neplatny retezec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WiFiConfig::_storeString(unsigned int start, size_t maxlen, String &string)
|
||||||
|
{
|
||||||
|
uint8_t key[4];
|
||||||
|
unsigned int enciphidx = 0;
|
||||||
|
unsigned int si = 0;
|
||||||
|
unsigned int top;
|
||||||
|
char c;
|
||||||
|
bool result = false; // retezec nebyl ulozeny cely (nevesel s do bufferu)
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
if (string.length() > maxlen - 2)
|
||||||
|
top = maxlen - 2;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = true; // retezec se do urceneho mista vejde
|
||||||
|
top = string.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t cid = ESP_getChipId();
|
||||||
|
uint32_t *pkey = (uint32_t *)&key[0];
|
||||||
|
*pkey = cid;
|
||||||
|
DEBUG_MSG("Key = %02X:%02X:%02X:%02X\r\n", key[0], key[1], key[2], key[3]);
|
||||||
|
EEPROM.write(start, (uint8_t)top); // ulozime delku retezce (pouzite pr kontrolu pri vycitani)
|
||||||
|
for (i = start + 1; i < start + top + 1; ++i)
|
||||||
|
{
|
||||||
|
c = string[si];
|
||||||
|
c ^= key[enciphidx];
|
||||||
|
++enciphidx;
|
||||||
|
enciphidx %= sizeof(key);
|
||||||
|
EEPROM.write(i, c);
|
||||||
|
++si;
|
||||||
|
}
|
||||||
|
c = 0 ^ key[enciphidx];
|
||||||
|
EEPROM.write(i, c); // ukoncovaci a kontrolni nula
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
// Testovani, zda se modul pripojil k AP
|
// Testovani, zda se modul pripojil k AP
|
||||||
bool WiFiConfig::_testWifi(wificonfig_cb cb)
|
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
|
||||||
|
|
||||||
|
String s = _readString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
||||||
|
strcpy(WiFiDeviceName, s.c_str());
|
||||||
|
s = _readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
||||||
|
String pass = _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))));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???)
|
||||||
|
DEBUG_MSG("Mode Error!!\r\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#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)
|
||||||
{
|
{
|
||||||
@ -704,11 +914,15 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
|
|||||||
cb(WCS_CONNECTING); // signalizujeme pokracujici pokus o spojeni
|
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();
|
||||||
|
#endif
|
||||||
return false; // pripojeni se nezdarilo
|
return false; // pripojeni se nezdarilo
|
||||||
}
|
}
|
||||||
|
|
||||||
wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wificonfig_cb cb)
|
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
|
||||||
|
|
||||||
@ -723,22 +937,34 @@ wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wif
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
#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\r\n");
|
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
|
||||||
{
|
{
|
||||||
String s = getEEPROMString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
String s = _readString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
||||||
strcpy(WiFiDeviceName, s.c_str());
|
strcpy(WiFiDeviceName, s.c_str());
|
||||||
|
|
||||||
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)
|
||||||
WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni
|
WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni
|
||||||
|
#else
|
||||||
|
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");
|
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
|
||||||
@ -765,18 +991,14 @@ wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wif
|
|||||||
return result; // mame vyreseno
|
return result; // mame vyreseno
|
||||||
}
|
}
|
||||||
|
|
||||||
wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, int timeout, wificonfig_cb cb = NULL)
|
wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb = NULL)
|
||||||
{
|
{
|
||||||
if (forceConfigure)
|
|
||||||
{ // pouze pokud nemame vynucenou konfiguraci merime cas - to nam umozni dostat se ze spatne zadaneho timeoutu, ktery se neda zvladnout
|
|
||||||
if ((timeout > 0) && (40 > timeout))
|
if ((timeout > 0) && (40 > timeout))
|
||||||
timeout = 40; // timeout musi byt minimalne 40 sekund
|
timeout = 40; // timeout musi byt minimalne 40 sekund
|
||||||
_timeout = timeout;
|
_timeout = timeout;
|
||||||
if (_timeout > 0)
|
if (_timeout > 0)
|
||||||
_time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
|
_time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
|
||||||
}
|
|
||||||
else
|
|
||||||
_timeout = 0; // pri vynucene konfiguraci se parametr timeout neuplatni
|
|
||||||
return begin(configarea, forceConfigure, cb); // spustime WiFi
|
return begin(configarea, forceConfigure, cb); // spustime WiFi
|
||||||
}
|
}
|
||||||
// EOF
|
// EOF
|
@ -2,7 +2,7 @@
|
|||||||
* @file WiFiConfig.h
|
* @file WiFiConfig.h
|
||||||
* @author Pavel Brychta, http://www.xpablo.cz
|
* @author Pavel Brychta, http://www.xpablo.cz
|
||||||
*
|
*
|
||||||
* Copyright (c) 2015,16,17 Pavel Brychta. All rights reserved.
|
* Copyright (c) 2015-18 Pavel Brychta. All rights reserved.
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
@ -24,12 +24,17 @@
|
|||||||
* 1. Pokud je forceConfigure ==0, tak se aktivuje WiFi v rezimu AP a cele ESP je mozne nastavit webovym rozhranim (pocitam s prenosem hodnoty nouzoveho tlacitka pri startu).
|
* 1. Pokud je forceConfigure ==0, tak se aktivuje WiFi v rezimu AP a cele ESP je mozne nastavit webovym rozhranim (pocitam s prenosem hodnoty nouzoveho tlacitka pri startu).
|
||||||
* 2. Neni-li tlacitko stisknute, tak se vezme rezim prace a AP se nastavi dle nej (WIFI_STA a WIFI_AP)
|
* 2. Neni-li tlacitko stisknute, tak se vezme rezim prace a AP se nastavi dle nej (WIFI_STA a WIFI_AP)
|
||||||
* 3. Pokud byl rezim prace WIFI_STA a ESP se nepripoji k zadne siti do casu WIFI_STA_CONNECT_TIMEOUT, tak se pokracuje jako kdyby bylo stisknute rekonfiguracni tlacitko
|
* 3. Pokud byl rezim prace WIFI_STA a ESP se nepripoji k zadne siti do casu WIFI_STA_CONNECT_TIMEOUT, tak se pokracuje jako kdyby bylo stisknute rekonfiguracni tlacitko
|
||||||
|
* Podrobnejsi informace o upravach chovani v jednotlivych verzich jsou v zahlavi .cpp souboru.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __WiFiConfig_h__
|
#ifndef __WiFiConfig_h__
|
||||||
#define __WiFiConfig_h__
|
#define __WiFiConfig_h__
|
||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#if defined(ESP8266)
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#else
|
||||||
|
#include <WiFi.h>
|
||||||
|
#endif
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#define elementSize(type, element) sizeof(((type *)0)->element)
|
#define elementSize(type, element) sizeof(((type *)0)->element)
|
||||||
@ -41,9 +46,9 @@ 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]; // SSID site
|
char ssid[32 + 2]; // SSID site
|
||||||
char pass[64]; // heslo
|
char pass[64 + 2]; // heslo
|
||||||
char devname[32]; // jmeno zarizeni (pro NBNS plati jen 16 znaku)
|
char devname[32 + 2]; // jmeno zarizeni (pro NBNS plati jen 16 znaku)
|
||||||
uint32_t ipaddr; // ip adresa v pripade staticke konfigurace
|
uint32_t ipaddr; // ip adresa v pripade staticke konfigurace
|
||||||
uint32_t netmask; // sitova maska v pripade staticke konfigurace
|
uint32_t netmask; // sitova maska v pripade staticke konfigurace
|
||||||
uint32_t gateway; // sitova brana v pripade staticke konfigurace
|
uint32_t gateway; // sitova brana v pripade staticke konfigurace
|
||||||
@ -93,7 +98,7 @@ typedef void (*storeparam_cb)(const char *newvalue); // callback pro ulozeni uzi
|
|||||||
* \param [in] len Delka retezce
|
* \param [in] len Delka retezce
|
||||||
* \return Vycteny retezec
|
* \return Vycteny retezec
|
||||||
*/
|
*/
|
||||||
String getEEPROMString(int start, int len);
|
String getEEPROMString(unsigned int start, size_t len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Ulozeni retezce do EEPROM
|
* \brief Ulozeni retezce do EEPROM
|
||||||
@ -102,7 +107,7 @@ String getEEPROMString(int start, int len);
|
|||||||
* \param [in] len Maximalni delka ulozeneho retezce
|
* \param [in] len Maximalni delka ulozeneho retezce
|
||||||
* \param [in] string Ukladany retezec
|
* \param [in] string Ukladany retezec
|
||||||
*/
|
*/
|
||||||
void setEEPROMString(int start, int len, String string);
|
void setEEPROMString(unsigned int start, size_t len, String &string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Ziskani nasi ip adresy
|
* \brief Ziskani nasi ip adresy
|
||||||
@ -121,8 +126,21 @@ IPAddress getOurIP(void);
|
|||||||
*/
|
*/
|
||||||
uint8_t * getOurMAC(uint8_t *mac);
|
uint8_t * getOurMAC(uint8_t *mac);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Ziskani 32 bitoveho cisla z EEPROM
|
||||||
|
*
|
||||||
|
* @param[in] start Pocatecni adresa, ze ktere se ziskava cislo.
|
||||||
|
*
|
||||||
|
* @return Ziskane cislo.
|
||||||
|
*/
|
||||||
uint32_t getEEPROMuint32(int start);
|
uint32_t getEEPROMuint32(int start);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Ulozeni 32 bitoveho cisla do EEPROM
|
||||||
|
*
|
||||||
|
* @param[in] start Pocatecni adresa, kam cislo ukladame.
|
||||||
|
* @param[in] val Ukladana hodnota
|
||||||
|
*/
|
||||||
void setEEPROMuint32(int start, uint32_t val);
|
void setEEPROMuint32(int start, uint32_t val);
|
||||||
|
|
||||||
extern char WiFiDeviceName[]; // jmeno zarizeni, pouzivane i pro DHCP
|
extern char WiFiDeviceName[]; // jmeno zarizeni, pouzivane i pro DHCP
|
||||||
@ -130,7 +148,7 @@ extern char WiFiDeviceName[]; // jmeno zarizeni, pouzivane i pro DHCP
|
|||||||
class WiFiConfigUsrParameter
|
class WiFiConfigUsrParameter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WiFiConfigUsrParameter(const char *id, const char *label, const char *defaultValue, int length, storeparam_cb cb);
|
WiFiConfigUsrParameter(const char *id, const char *label, const char *defaultValue, unsigned int length, storeparam_cb cb);
|
||||||
|
|
||||||
const char *getID();
|
const char *getID();
|
||||||
const char *getValue();
|
const char *getValue();
|
||||||
@ -155,8 +173,8 @@ class WiFiConfig
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
WiFiConfig(): _params(NULL), _timeout(0) {}
|
WiFiConfig(): _params(NULL), _timeout(0) {}
|
||||||
wificonfigresult_t begin(int configarea, uint8_t forceConfigure, wificonfig_cb cb);
|
wificonfigresult_t begin(int configarea, int forceConfigure, wificonfig_cb cb);
|
||||||
wificonfigresult_t begin(int configarea, uint8_t forceConfigure, int timeout, wificonfig_cb cb);
|
wificonfigresult_t begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb);
|
||||||
void addParameter(WiFiConfigUsrParameter *p);
|
void addParameter(WiFiConfigUsrParameter *p);
|
||||||
private:
|
private:
|
||||||
wificonfigresult_t _setupAP(wificonfig_cb cb);
|
wificonfigresult_t _setupAP(wificonfig_cb cb);
|
||||||
@ -167,8 +185,9 @@ private:
|
|||||||
void _handleNotFound(); // CaptivePortal redirector
|
void _handleNotFound(); // CaptivePortal redirector
|
||||||
void _handleRoot(); // jen jednoducha stranka kvuli CaptivePortalu umoznuje prejit na spravnou stranku (ale nedela to...)
|
void _handleRoot(); // jen jednoducha stranka kvuli CaptivePortalu umoznuje prejit na spravnou stranku (ale nedela to...)
|
||||||
bool _testWifi(wificonfig_cb cb);
|
bool _testWifi(wificonfig_cb cb);
|
||||||
|
bool _storeString(unsigned int start, size_t maxlen, String &string); // sifrovany retezec, max 255 znaku
|
||||||
|
String _readString(unsigned int start, size_t maxlen); // sifrovany retezec, max. 255 znaku
|
||||||
WiFiConfigUsrParameter *_searchUsrParameter(const char *name);
|
WiFiConfigUsrParameter *_searchUsrParameter(const char *name);
|
||||||
void _plusDecode(String &s);
|
|
||||||
|
|
||||||
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
|
@ -1,5 +1,6 @@
|
|||||||
// file:///I:/MyProjects/PB_ESP8266_WiFiConfig_Vyvoj/doc/_testy/s?_s=SSID1&_p=heslo&_n=jmeno_zarizeni&_a=on&_st=on&_i=192.168.1.1&_m=255.255.255.0&_g=192.168.1.3&_d=192.168.1.3&{n}={v}
|
// HTML data
|
||||||
static const char TEXTHTML[] PROGMEM = "text/html";
|
static const char TEXTHTML[] PROGMEM = "text/html";
|
||||||
|
static const char TEXTPLAIN[] PROGMEM = "text/plain";
|
||||||
|
|
||||||
static const char PAGE_INDEX1[] PROGMEM = R"=====(
|
static const char PAGE_INDEX1[] 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">
|
<!DOCTYPE html><html lang="en"><head><meta charset= "utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
|
||||||
@ -25,11 +26,12 @@ function hSC(){if (document.getElementById('_st').checked){document.getElementBy
|
|||||||
|
|
||||||
/* Polozky nalezenych SSID
|
/* Polozky nalezenych SSID
|
||||||
{v} - SSID
|
{v} - SSID
|
||||||
{a} - RSSI (primo v dBm)
|
{a} - RSSI (%)
|
||||||
{s} - bud nic, nebo 'l'>
|
{s} - bud nic, nebo 'l'>
|
||||||
|
Rozsahy otestovany zde https://css-tricks.com/html5-meter-element/
|
||||||
*/
|
*/
|
||||||
static const char SSID_ITEM[] PROGMEM = R"=====(
|
static const char SSID_ITEM[] PROGMEM = R"=====(
|
||||||
<div><a href='#p' onclick='c(this)'>{v}</a> <span class='q {s}'><meter value="{a}" min="0" max="100" low="30" high="80" optimum="50" style="width: 70%;"></meter></span></div>
|
<div><a href='#p' onclick='c(this)'>{v}</a> <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
|
/* Zacatek formulare pro vyplneni
|
||||||
@ -56,7 +58,7 @@ static const char PAGE_INDEX2[] PROGMEM = R"=====(
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label><input id='_st' name='_st' type='checkbox' style="width: 10%; float: left; margin-top: 5px;" onclick='hSC();' {a}>Staticka IP konfigurace</label>
|
<label><input id='_st' name='_st' type='checkbox' style="width: 10%; float: left; margin-top: 5px;" onclick='hSC();' {c}>Staticka IP konfigurace</label>
|
||||||
<div id="staticip">
|
<div id="staticip">
|
||||||
<label>IP adresa<br><input type="text" name="_i" pattern='((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$' title='ip adresa ve formatu <cislo>.<cislo>.<cislo>.<cislo>' value='{i}'></label><br>
|
<label>IP adresa<br><input type="text" name="_i" pattern='((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$' title='ip adresa ve formatu <cislo>.<cislo>.<cislo>.<cislo>' value='{i}'></label><br>
|
||||||
<label>Síťová maska<br><input type="text" name="_m" pattern='((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$' title='ip adresa ve formatu <cislo>.<cislo>.<cislo>.<cislo>' value='{m}'></label><br>
|
<label>Síťová maska<br><input type="text" name="_m" pattern='((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$' title='ip adresa ve formatu <cislo>.<cislo>.<cislo>.<cislo>' value='{m}'></label><br>
|
Reference in New Issue
Block a user