11 Commits

6 changed files with 116 additions and 25 deletions

View File

@ -4,6 +4,7 @@
#include <EEPROM.h>
#include <ESP8266NetBIOS.h>
#include <WiFiConfig.h>
#include <encipheredEEPROMStrings.h>
// Plati pro desticku Witty s modulem ESP-12E
#define PIN_FORCE_CONFIG 4
@ -14,6 +15,19 @@
//#define DEBUG_OUT(a) {}
#define DEBUG_OUT(a) Serial.print(a)
// Definice obsazeni EEPROM
#define elementSize(type, element) sizeof(((type *)0)->element)
#define countof(a) (sizeof(a) / sizeof(a[0]))
typedef struct
{
char devname[32 + 2]; // jmeno zarizeni (pro NBNS plati jen 16 znaku)
} eepromconfig_t;
#define EEPROM_SIZE (sizeof(wificonfigarea_t) + sizeof(eepromconfig_t) + 10) // velikost EEPROM oblasti (POZOR!!! zbytecnym zvetsovanim se zaroven zmensuje velikost RAM kvuli zrcadlu!!!)
#define EEPROM_WIFICONFIG_ORIGIN (EEPROM_SIZE - sizeof(wificonfigarea_t)) // pocatek oblasti, pouzivane WiFiConfig (alokovano odzadu)
#define EEPROM_CONFIG_ORIGIN (0)
char WiFiDeviceName[elementSize(eepromconfig_t, devname)]; // misto pro jmeno zarizeni (dodane do DNS, DHCP NBNS apod...)
ESP8266WebServer wwwserver(80); // webovy server
String content;
@ -52,23 +66,27 @@ void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state)
}
}
void saveServer(const char *param)
void ICACHE_FLASH_ATTR saveDevname(const char *param)
{
String p = param;
DEBUG_OUT(param);
EES_storeString(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, devname), elementSize(eepromconfig_t, devname), p);
}
void ICACHE_FLASH_ATTR setup()
{
WiFiConfig wifi; // konfigurace ESP modulu
WiFiConfigUsrParameter testOnly("server", "adresa serveru", "Default server", 32, saveServer);
WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname);
EEPROM.begin(512); // zahajujeme praci s EEPROM
EEPROM.begin(EEPROM_SIZE); // zahajujeme praci s EEPROM
pinMode(PIN_FORCE_CONFIG, INPUT_PULLUP); // pin, co slouzi jako vstup tlacitka
int fc = digitalRead(PIN_FORCE_CONFIG); // pozadavek na vynucene vyvolani konfigurace
Serial.begin(9600);
wifi.addParameter(&testOnly);
if (WCR_OK != wifi.begin(0, fc, wcb)) // startujeme pripojeni
String dn = EES_readString(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, devname), elementSize(eepromconfig_t, devname));
strcpy(WiFiDeviceName, dn.c_str());
wifi.addParameter(&devname);
if (WCR_OK != wifi.begin(EEPROM_WIFICONFIG_ORIGIN, fc, wcb)) // startujeme pripojeni
ESP.restart();
wwwserver.on("/", handleRoot);

View File

@ -12,7 +12,7 @@
"type": "git",
"url": "http://git.xpablo.cz/pablo2048/WiFiConfig.git"
},
"version": "6.0",
"version": "6.3.1",
"license": "MIT",
"frameworks": "arduino",
"platforms": ["espressif8266", "espressif32"],

View File

@ -1,5 +1,5 @@
name=WiFiConfig
version=6.0
version=6.3.1
author=Pavel Brychta
maintainer=Pavel Brychta <Pablo@xpablo.cz>
sentence=Enables seamless module configuration.

View File

@ -1,4 +1,12 @@
/*
* v6.3.2- 4.10.2019 - Informace o aplikaci a jeji verzi do footeru, zrychlen restart z 10 na 5 sekund, vylepsena stranka pro reset
* verze pro ESP32 zobrazuje neco malo informaci o stavu pripojeni a duvodu vstupu do WifiConfig
*
* V6.3.1- 1.9.2019 - Odstraneni HTML5 validatoru - delaly akorat problemy a nedala se ulozit konfigurace i kdyz byly polozky skryty
*
* V6.3 - 12.8.2019 - IP adresa Captive portalu je zmenena na 172.217.28.1 kvuli funkcnosti na Android zarizenich (viz. zmena zde https://github.com/esp8266/Arduino/blob/master/libraries/DNSServer/examples/CaptivePortal/CaptivePortal.ino)
* ladici vypis pri zachyceni presmerovani v Captive portalu.
*
* V6.2 - 12.12.2018- Nastaveni jmena zarizeni nadale NENI soucasti WiFiConfig a je treba ho pridat jako uzivatelsky parametr. Je to proto, protoze
* se ukazalo, ze neni dobre z externiho kodu sahat do vnitrni struktury WiFiConfig (napr. pri ukladani noveho jmena).
*
@ -21,7 +29,7 @@
*
* V5.3 - 8.12.2017 - prepracovano presmerovani v Captive portalu, DEBUG_MSG makro upraveno na posledni pouzivanou verzi, scitani retezcu prepracovano na .concat. Pripsany Informace o modulu, reakce na Reset
*
* V5.2 - 20.4.2017 - Pokud je pouzite v timeoutu EC_DONT_RUN_CONFIGAP tak se vracime okamzite a necekame na pripojeni k WiFi (take nevolame zadnou callback metodu).
* V5.2 - 20.4.2017 - Pokud je pouzite v timeoutu WC_DONT_RUN_CONFIGAP tak se vracime okamzite a necekame na pripojeni k WiFi (take nevolame zadnou callback metodu).
* Navratovy kod je pak WCR_CONFIGAP_NOT_STARTED .
*
* V5.1 - 3.4.2017 - Webove rozhrani upravene pro snadnejsi ovladani na telefonu/tabletu, sila signalu AP zobrazena jako barevne odliseny indikator.
@ -79,6 +87,7 @@
#include <ESP8266WebServer.h>
#else
#include <WebServer.h>
#include <rom/rtc.h> // duvod resetu (https://github.com/espressif/arduino-esp32/issues/449)
#endif
#include "WiFiConfig.h"
#include <EEPROM.h>
@ -313,7 +322,13 @@ void WiFiConfig::_handleNotFound(void)
}
else
{
server->sendHeader(F("Location"), String(F("http://")) + server->client().localIP().toString() + String(F("/index.htm")), true);
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"));
}
}
@ -447,7 +462,24 @@ void WiFiConfig::_handleRoot(void)
break;
}
#else
content.concat(F("Prozatím nedostupné"));
switch (_status)
{
case WL_CONNECT_FAILED:
content.concat(F("Připojení selhalo"));
break;
case 0xfe:
content.concat(F("Špatná EEPROM"));
break;
case 0xff:
content.concat(F("Vynucená konfigurace"));
break;
default:
content.concat(F("Neznámý"));
break;
}
#endif
content.concat(F("</div>"));
content.concat(FPSTR(PAGE_CAPTIVEPORTALCATCH2)); // 2. cast stranky (ukonceni)
@ -697,7 +729,7 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
#endif
/* Soft AP network parameters */
IPAddress apIP(192, 168, 4, 1);
IPAddress apIP(172, 217, 28, 1);
IPAddress netMsk(255, 255, 255, 0);
WiFi.disconnect(); // pro jistotu se odpojime
@ -759,7 +791,7 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
// nakonfigurujeme ESP dle nove nastavenych parametru
WiFi.disconnect(); // vsechno odpojime
s = EES_readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
String s = EES_readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
String pass = EES_readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)))
@ -893,6 +925,14 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificon
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;
#endif
}
}
}
@ -904,6 +944,8 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int tim
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

View File

@ -2,7 +2,7 @@
* @file WiFiConfig.h
* @author Pavel Brychta, http://www.xpablo.cz
*
* Copyright (c) 2015-18 Pavel Brychta. All rights reserved.
* Copyright (c) 2015-19 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

View File

@ -58,10 +58,10 @@ static const char PAGE_INDEX2[] PROGMEM = R"=====(
<div>
<label><input id='_st' name='_st' type='checkbox' style="width: 10%; float: left; margin-top: 5px;" onclick='hSC();' {c}>Statická IP konfigurace</label>
<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>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á brána<br><input type="text" name="_g" 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='{g}'></label><br>
<label>Server DNS<br><input type="text" name="_d" 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='{d}'></label><br>
<label>IP adresa<br><input type="text" name="_i" value='{i}'></label><br>
<label>Síťová maska<br><input type="text" name="_m" value='{m}'></label><br>
<label>Síťová brána<br><input type="text" name="_g" value='{g}'></label><br>
<label>Server DNS<br><input type="text" name="_d" value='{d}'></label><br>
</div>
</div>
<script>hAP();hSC();</script>
@ -81,9 +81,22 @@ static const char PAGE_PARAM[] PROGMEM = R"=====(
static const char PAGE_END[] PROGMEM = R"=====(
<br>
<button type='submit'>Uložit a restartovat</button></form>
</div></body></html>
</div>)====="
#if defined APP_NAME && defined APP_VERSION
R"=====(<div style='text-align:right;font-size:11px;'><hr/>)====="
APP_NAME
" v"
APP_VERSION
R"=====(</div>
)====="
#else
R"=====(<div style='text-align:right;font-size:11px;'><hr/><a href='http://xpablo.cz' target='_blank' style='color:#aaa;'>WiFiConfig by xPablo</a></div>)====="
#endif
R"=====(</body></html>
)=====";
/*
<div style='text-align:right;font-size:11px;'><hr/><a href='http://xpablo.cz' target='_blank' style='color:#aaa;'>WiFiConfig by xPablo</a></div>
*/
// Nenalezeno zadne SSID
static const char PAGE_NO_SSID[] PROGMEM = R"=====(
<div>Nenalezená žádná síť. Občerstvi stránku pro nové hledání.</div>
@ -91,13 +104,13 @@ static const char PAGE_NO_SSID[] PROGMEM = R"=====(
// Konfigurace ulozena - restartuji...
static const char PAGE_SAVED[] PROGMEM = R"=====(
<!DOCTYPE html><html lang="cs"><head><meta http-equiv="refresh" content="10;url=/"/>
<!DOCTYPE html><html lang="cs"><head><meta http-equiv="refresh" content="5;url=/"/>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"/>
<title>xPablo Setup - Konfigurace uložena</title>
<style>div,input {margin-bottom: 5px;}body{width:200px;display:block;margin-left:auto;margin-right:auto;}</style>
</head><body>
Uloženo do EEPROM...<br/>
Restart za 10 sekund.
Restart za 5 sekund.
</body></html>
)=====";
@ -111,11 +124,29 @@ static const char PAGE_CAPTIVEPORTALCATCH[] PROGMEM = R"=====(
<form action="/config" method="get"><button>Konfigurace WiFi</button></form><br/><form action="/i" method="get"><button>Informace o modulu</button></form><br/><form action="/r" method="post"><button class="red">Reset</button></form><br>
)=====";
// druha cast (mezi 1. a druhou cast muzeme doplnit dodatecne informace)
// druha cast (mezi 1. a 2. cast muzeme doplnit dodatecne informace)
static const char PAGE_CAPTIVEPORTALCATCH2[] PROGMEM = R"=====(
</div></body></html>
</div>
)====="
#if defined APP_NAME && defined APP_VERSION
R"=====(<div style='text-align:right;font-size:11px;'><hr/>)====="
APP_NAME
" v"
APP_VERSION
R"=====(</div>
)====="
#else
R"=====(<div style='text-align:right;font-size:11px;'><hr/><a href='http://xpablo.cz' target='_blank' style='color:#aaa;'>WiFiConfig by xPablo</a></div>)====="
#endif
R"=====(</body></html>
)=====";
static const char PAGE_RESTART[] PROGMEM = R"=====(
Restartuji...
<!DOCTYPE html><html lang="cs"><head><meta http-equiv="refresh" content="5;url=/"/>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"/>
<title>xPablo Setup - Restart</title>
<style>div,input {margin-bottom: 5px;}body{width:200px;display:block;margin-left:auto;margin-right:auto;}</style>
</head><body>
Restartuji...
</body></html>
)=====";