Compare commits

..

6 Commits

5 changed files with 129 additions and 47 deletions

View File

@ -12,7 +12,7 @@
"type": "git",
"url": "http://git.xpablo.cz/pablo2048/WiFiConfig.git"
},
"version": "5.8",
"version": "5.9",
"license": "MIT",
"frameworks": "arduino",
"platforms": "espressif8266",

View File

@ -1,9 +1,9 @@
name=WiFiConfig
version=5.8
version=5.9
author=Pavel Brychta
maintainer=Pavel Brychta <Pablo@xpablo.cz>
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 module to be configured over WiFi. Includes Captive Portal for easy configuration.
category=Other
url=http://www.xpablo.cz
architectures=esp8266

View File

@ -1,4 +1,6 @@
/*
* 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
@ -139,11 +141,11 @@ uint8_t * getOurMAC(uint8_t *mac)
return WiFi.softAPmacAddress(mac);
}
uint32_t getEEPROMuint32(int start)
uint32_t getEEPROMuint32(unsigned int start)
{
uint32_t result = 0;
for (int i=0; i<4; ++i)
for (uint32_t i=0; i<4; ++i)
{
result <<= 8;
result += EEPROM.read(start);
@ -152,36 +154,36 @@ uint32_t getEEPROMuint32(int start)
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);
val >>= 8;
}
}
String getEEPROMString(int start, int len)
String getEEPROMString(unsigned int start, size_t len)
{
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);
if ((0xff == b) || (0 == b))
break;
string.concat(char(b));
string += char(b);
}
return string;
}
void setEEPROMString(int start, int len, String string)
void setEEPROMString(unsigned int start, size_t len, String &string)
{
unsigned int si = 0;
for (int i = start; i < start + len; ++i)
for (unsigned int i = start; i < start + len; ++i)
{
char c;
@ -198,7 +200,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;
_cb = cb;
@ -206,7 +208,7 @@ WiFiConfigUsrParameter::WiFiConfigUsrParameter(const char *id, const char *label
_label = label;
_length = length;
_value = new char[length + 1];
for (int i = 0; i < length; i++)
for (unsigned int i = 0; i < length; i++)
{
_value[i] = 0;
}
@ -281,18 +283,16 @@ void WiFiConfig::addParameter(WiFiConfigUsrParameter *p)
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());
if (server->uri().endsWith(String(F("favicon.ico"))))
{
server->send(404, F("text/plain"), F("Err"));
server->send_P(404, TEXTPLAIN, PSTR("Err"));
}
else
{
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 (302, F("text/plain"), F("Redirect"));
server->send_P(302, TEXTPLAIN, PSTR("Redirect"));
}
}
@ -353,7 +353,7 @@ void WiFiConfig::_handleInfo(void)
reply.concat(buff);
reply.concat(F("</table></form>"));
server->send(200, TEXTHTML, reply);
server->send(200, FPSTR(TEXTHTML), reply);
}
void WiFiConfig::_handleRoot(void)
@ -399,12 +399,12 @@ void WiFiConfig::_handleRoot(void)
break;
default:
content.concat(F("Neznámého"));
content.concat(F("Neznámý"));
break;
}
content.concat(F("</div>"));
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
}
@ -443,17 +443,17 @@ void WiFiConfig::_handleDisplayAP(void)
}
}
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())
s.replace(F("{s}"), String(FPSTR(VALUE)) + v + F("'"));
else
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())
s.replace(F("{p}"), String(FPSTR(VALUE)) + v + F("'"));
else
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())
s.replace(F("{n}"), String(FPSTR(VALUE)) + v + F("'"));
else
@ -498,7 +498,7 @@ void WiFiConfig::_handleDisplayAP(void)
}
content.concat(FPSTR(PAGE_END));
server->send(200, TEXTHTML, content);
server->send(200, FPSTR(TEXTHTML), content);
}
void WiFiConfig::_handleSetAP(void)
@ -510,13 +510,13 @@ void WiFiConfig::_handleSetAP(void)
str = server->arg(F("_s"));
if (str.length() > 0)
{
setEEPROMString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), str);
_storeString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), str);
str = server->arg(F("_p"));
setEEPROMString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str);
_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str);
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"));
if (str.length() > 0)
@ -566,16 +566,16 @@ void WiFiConfig::_handleSetAP(void)
}
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
// nakonfigurujeme ESP dle nove nastavenych parametru
WiFi.disconnect(); // vsechno odpojime
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());
s = getEEPROMString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
String pass = getEEPROMString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
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)))
{
@ -696,6 +696,71 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
return WCR_TIMEOUT; // nepripojeno, vyprsel timeout konfiguracniho AP
}
String WiFiConfig::_readString(unsigned int start, size_t maxlen)
{
uint8_t mac[WL_MAC_ADDR_LENGTH];
unsigned int enciphidx = 0;
String result;
unsigned int top = EEPROM.read(start);
unsigned int i;
result.reserve(maxlen);
WiFi.softAPmacAddress(mac);
if ((top > 0) && (top < (maxlen - 2)))
{
uint8_t b;
for (i = start + 1; i < + start + top + 1; ++i)
{
b = EEPROM.read(i);
b ^= mac[enciphidx];
++enciphidx;
enciphidx %= WL_MAC_ADDR_LENGTH;
result.concat(char(b));
}
b = EEPROM.read(i) ^ mac[enciphidx];
if (b != 0)
result = F(""); // spatna ukoncovaci nula - neplatny retezec
}
return result;
}
bool WiFiConfig::_storeString(unsigned int start, size_t maxlen, String &string)
{
uint8_t mac[WL_MAC_ADDR_LENGTH];
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();
}
WiFi.softAPmacAddress(mac);
EEPROM.write(start, (uint8_t)top); // ulozime delku retezce (pouzite pr kontrolu pri vycitani)
for (i = start + 1; i < start + maxlen + 1; ++i)
{
c = string[si];
c ^= mac[enciphidx];
++enciphidx;
enciphidx %= WL_MAC_ADDR_LENGTH;
EEPROM.write(i, c);
++si;
}
c = 0 ^ mac[enciphidx];
EEPROM.write(i, c); // ukoncovaci a kontrolni nula
return result;
}
// Testovani, zda se modul pripojil k AP
bool WiFiConfig::_testWifi(wificonfig_cb cb)
{
@ -743,7 +808,7 @@ wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wif
}
else
{
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());
switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)))

View File

@ -2,7 +2,7 @@
* @file WiFiConfig.h
* @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
* modify it under the terms of the GNU Lesser General Public
@ -24,6 +24,7 @@
* 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)
* 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__
@ -41,9 +42,9 @@ typedef struct
{
uint8_t mode; // rezim prace AP/STA
uint8_t ip; // konfigurace ip (staticka/DHCP)
char ssid[32]; // SSID site
char pass[64]; // heslo
char devname[32]; // jmeno zarizeni (pro NBNS plati jen 16 znaku)
char ssid[32 + 2]; // SSID site
char pass[64 + 2]; // heslo
char devname[32 + 2]; // jmeno zarizeni (pro NBNS plati jen 16 znaku)
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
@ -93,7 +94,7 @@ typedef void (*storeparam_cb)(const char *newvalue); // callback pro ulozeni uzi
* \param [in] len Delka retezce
* \return Vycteny retezec
*/
String getEEPROMString(int start, int len);
String getEEPROMString(unsigned int start, size_t len);
/**
* \brief Ulozeni retezce do EEPROM
@ -102,7 +103,7 @@ String getEEPROMString(int start, int len);
* \param [in] len Maximalni delka ulozeneho retezce
* \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
@ -121,8 +122,21 @@ IPAddress getOurIP(void);
*/
uint8_t * getOurMAC(uint8_t *mac);
/**
* @brief Ziskani 32 bitoveho cisla z EEPROM
*
* @param[in] start Pocatecni adresa, ze ktere se ziskava cislo.
*
* @return Ziskane cislo.
*/
uint32_t getEEPROMuint32(int start);
/**
* @brief Ulozeni 32 bitoveho cisla do EEPROM
*
* @param[in] start Pocatecni adresa, kam cislo ukladame.
* @param[in] val Ukladana hodnota
*/
void setEEPROMuint32(int start, uint32_t val);
extern char WiFiDeviceName[]; // jmeno zarizeni, pouzivane i pro DHCP
@ -130,23 +144,23 @@ extern char WiFiDeviceName[]; // jmeno zarizeni, pouzivane i pro DHCP
class WiFiConfigUsrParameter
{
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 *getValue();
const char *getLabel();
int getValueLength();
void setNext(WiFiConfigUsrParameter *n);
int getValueLength();
void setNext(WiFiConfigUsrParameter *n);
WiFiConfigUsrParameter *getNext();
void setNewValue(const char *newval);
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;
};
@ -157,7 +171,7 @@ public:
WiFiConfig(): _params(NULL), _timeout(0) {}
wificonfigresult_t begin(int configarea, uint8_t forceConfigure, wificonfig_cb cb);
wificonfigresult_t begin(int configarea, uint8_t forceConfigure, int timeout, wificonfig_cb cb);
void addParameter(WiFiConfigUsrParameter *p);
void addParameter(WiFiConfigUsrParameter *p);
private:
wificonfigresult_t _setupAP(wificonfig_cb cb);
void _handleDisplayAP(void);
@ -167,6 +181,8 @@ private:
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

View File

@ -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 TEXTPLAIN[] PROGMEM = "text/plain";
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">