Zprovozneni prikladu

This commit is contained in:
Pavel Brychta 2021-03-05 07:41:14 +01:00
parent b608d36096
commit 1106aa060a
5 changed files with 42 additions and 73 deletions

View File

@ -8,11 +8,8 @@
# include <NetBIOS.h>
#endif
#include <ArduinoOTA.h>
#include <EEPROM.h>
#include <WiFiConfig.h>
#include <encipheredEEPROMStrings.h>
#define EEPROM_MAGIC 0xdeadbeef
#include <sysvars.hpp>
// Plati pro desticku Witty s modulem ESP-12E
#define PIN_FORCE_CONFIG 4
@ -20,34 +17,24 @@
//#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 {
uint32_t magic; // kontrola spravneho obsahu EEPROM
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...)
char WiFiDeviceName[32]; // misto pro jmeno zarizeni (dodane do DNS, DHCP NBNS apod...)
#if defined(ARDUINO_ARCH_ESP8266)
ESP8266WebServer wwwserver(80); // webovy server
#else
WebServer wwwserver(80); // webovy server
#endif
String content;
static char MAINPAGE[] PROGMEM = R"=====(<!DOCTYPE HTML>
<html>
Hello world from ESP8266
<p>
</html>
)=====";
static void handleRoot(void)
{
content = F("<!DOCTYPE HTML>\n<html>Hello world from ESP8266");
content += F("<p>");
content += F("</html>");
wwwserver.send(200, "text/html", content);
wwwserver.send_P(200, "text/html", MAINPAGE);
}
void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state)
@ -82,7 +69,7 @@ void ICACHE_FLASH_ATTR saveDevname(const char *param)
{
String p = param;
EES_storeString(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, devname), elementSize(eepromconfig_t, devname), p);
svSetV(F("devname"), p);
}
void ICACHE_FLASH_ATTR setup()
@ -90,28 +77,16 @@ void ICACHE_FLASH_ATTR setup()
WiFiConfig wifi; // konfigurace ESP modulu
WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname);
EEPROM.begin(EEPROM_SIZE); // zahajujeme praci s EEPROM
uint32_t magic;
EEPROM.get(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, magic), magic);
if (EEPROM_MAGIC != magic) {
// EEPROM ma spatny obsah
for (unsigned int i = 0; i < EEPROM_SIZE; i++)
EEPROM.write(i, 0);
magic = EEPROM_MAGIC;
EEPROM.put(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, magic), magic);
EEPROM.commit();
}
LittleFS.begin();
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(115200);
String dn = EES_readString(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, devname), elementSize(eepromconfig_t, devname));
String dn = svGetV<String>(F("devname"));
strcpy(WiFiDeviceName, dn.c_str());
wifi.addParameter(&devname);
if (WCR_OK != wifi.begin(EEPROM_WIFICONFIG_ORIGIN, fc, wcb)) // startujeme pripojeni
if (WCR_OK != wifi.begin(fc, wcb)) // startujeme pripojeni
ESP.restart();
wwwserver.on("/", handleRoot);

View File

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

View File

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

View File

@ -171,7 +171,7 @@ IPAddress getOurIP(void)
uint8_t * getOurMAC(uint8_t *mac)
{
if (WIFIMODE_STA == EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)))
if (WIFIMODE_STA == svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE)))
return WiFi.macAddress(mac);
else
return WiFi.softAPmacAddress(mac);
@ -427,7 +427,7 @@ void WiFiConfig::_handleRoot(void)
void WiFiConfig::_handleDisplayAP(void)
{
String s;
encString v;
String v;
String content;
_time = millis() + (360 * 1000); // spocitame si novy cas, kdy budeme modul restartovat (6 minut)
@ -459,13 +459,13 @@ void WiFiConfig::_handleDisplayAP(void)
}
}
s = FPSTR(PAGE_INDEX2);
v = svGetV<encString>(F("ssid"), nullptr, F(WIFICFG_FILE));
v.decode();
v = svGetV<String>(F("ssid"), nullptr, F(WIFICFG_FILE));
strDecode(v);
if (v.length())
s.replace(F("{s}"), String(FPSTR(VALUE)) + v + F("'"));
else
s.replace(F("{s}"), F(""));
v = svGetV<encString>(F("pass"), nullptr, F(WIFICFG_FILE));
v = svGetV<String>(F("pass"), nullptr, F(WIFICFG_FILE));
if (v.length())
s.replace(F("{p}"), String(FPSTR(VALUE)) + v + F("'"));
else
@ -519,16 +519,12 @@ void WiFiConfig::_handleSetAP(void)
str.reserve(128);
str = server->arg(F("_s"));
if (str.length() > 0) {
encString dummy;
dummy = str;
dummy.encode();
svSetV(F("ssid"), dummy, F(WIFICFG_FILE));
strEncode(str);
svSetV(F("ssid"), str, F(WIFICFG_FILE));
str = server->arg(F("_p"));
dummy = str;
dummy.encode();
svSetV(F("pass"), dummy, F(WIFICFG_FILE));
strEncode(str);
svSetV(F("pass"), str, F(WIFICFG_FILE));
str = server->arg(F("_a"));
if (str.length() > 0) {
@ -695,9 +691,9 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
// nakonfigurujeme ESP dle nove nastavenych parametru
WiFi.disconnect(); // vsechno odpojime
encString s = svGetV<encString>(F("ssid"), nullptr, F(WIFICFG_FILE));
encString s = svGetV<String>(F("ssid"), nullptr, F(WIFICFG_FILE));
s.decode();
encString pass = svGetV<encString>(F("pass"), nullptr, F(WIFICFG_FILE));
encString pass = svGetV<String>(F("pass"), nullptr, F(WIFICFG_FILE));
pass.decode();
switch (svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE))) {
@ -773,13 +769,12 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
return false; // pripojeni se nezdarilo
}
wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificonfig_cb cb)
wificonfigresult_t WiFiConfig::begin(int forceConfigure, wificonfig_cb cb)
{
wificonfigresult_t result = WCR_OK; // predpokladame, ze se pripojeni podari
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;
@ -843,7 +838,7 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificon
return result; // mame vyreseno
}
wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb = NULL)
wificonfigresult_t WiFiConfig::begin(int forceConfigure, int timeout, wificonfig_cb cb = NULL)
{
if ((timeout > 0) && (40 > timeout))
@ -853,19 +848,19 @@ wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int tim
_timeout = timeout;
if (_timeout > 0)
_time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
return begin(configarea, forceConfigure, cb); // spustime WiFi
return begin(forceConfigure, cb); // spustime WiFi
}
void WiFiConfig::initConfig(const String &ssid, const String &pass, const int mode, const int ipcfg, const IPAddress &ip, const IPAddress &mask, const IPAddress &gw, const IPAddress &dns)
{
encString es;
String es;
DEBUG_MSG("initConfig %d\r\n", mode);
es = ssid;
es.encrypt();
strEncode(es);
svSetV<String>(F("ssid"), es, F(WIFICFG_FILE));
es = pass;
es.encrypt();
strEncode(es);
svSetV<String>(F("pass"), es, F(WIFICFG_FILE));
svSetV(F("mode"), mode, F(WIFICFG_FILE));
svSetV(F("ipmode"), ipcfg, F(WIFICFG_FILE));
@ -884,10 +879,10 @@ void WiFiConfig::_prepareWifi(void)
WiFi.disconnect(); // vsechno odpojime
delay(100);
encString s = svGetV<encString>(F("ssid"), nullptr, F(WIFICFG_FILE));
s.decode();
encString pass = svGetV<encString>(F("pass"), nullptr, F(WIFICFG_FILE));
pass.decode();
String s = svGetV<String>(F("ssid"), nullptr, F(WIFICFG_FILE));
strDecode(s);
String pass = svGetV<String>(F("pass"), nullptr, F(WIFICFG_FILE));
strDecode(pass);
int mode = svGetV<int>(F("mode"), nullptr, F(WIFICFG_FILE));
switch (mode) {
@ -925,7 +920,7 @@ void WiFiConfig::_prepareWifi(void)
}
break;
case WIFIMODE_AP:
case WIFIMODE_AP:{
DEBUG_MSG("AP mode.\r\n");
if (s.endsWith(F("?"))) {
@ -949,6 +944,7 @@ void WiFiConfig::_prepareWifi(void)
IPAddress(svGetV<uint32_t>(F("mask"), nullptr, F(WIFICFG_FILE))));
}
break;
}
default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???)

View File

@ -130,10 +130,10 @@ 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);
wificonfigresult_t begin(int forceConfigure, wificonfig_cb cb);
wificonfigresult_t begin(int forceConfigure, int timeout, wificonfig_cb cb);
void addParameter(WiFiConfigUsrParameter *p);
void initConfig(int cfgBase, const String &ssid, const String &pass, const int mode = WIFIMODE_STA, const int ipcfg = IPCONFIG_DHCP, const IPAddress &ip = 0, const IPAddress &mask = 0, const IPAddress &gw = 0, const IPAddress &dns = 0);
void initConfig(const String &ssid, const String &pass, const int mode = WIFIMODE_STA, const int ipcfg = IPCONFIG_DHCP, const IPAddress &ip = 0, const IPAddress &mask = 0, const IPAddress &gw = 0, const IPAddress &dns = 0);
private:
void _prepareWifi(void);
@ -145,8 +145,6 @@ 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