LittleFS #10

Merged
pablo2048 merged 3 commits from LittleFS into master 2021-06-21 17:19:58 +02:00
5 changed files with 42 additions and 73 deletions
Showing only changes of commit 1106aa060a - Show all commits

View File

@ -8,11 +8,8 @@
# include <NetBIOS.h> # include <NetBIOS.h>
#endif #endif
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <EEPROM.h>
#include <WiFiConfig.h> #include <WiFiConfig.h>
#include <encipheredEEPROMStrings.h> #include <sysvars.hpp>
#define EEPROM_MAGIC 0xdeadbeef
// 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
@ -20,34 +17,24 @@
//#define DEBUG_OUT(a) {} //#define DEBUG_OUT(a) {}
#define DEBUG_OUT(a) Serial.print(a) #define DEBUG_OUT(a) Serial.print(a)
// Definice obsazeni EEPROM char WiFiDeviceName[32]; // misto pro jmeno zarizeni (dodane do DNS, DHCP NBNS apod...)
#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...)
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
ESP8266WebServer wwwserver(80); // webovy server ESP8266WebServer wwwserver(80); // webovy server
#else #else
WebServer wwwserver(80); // webovy server WebServer wwwserver(80); // webovy server
#endif #endif
String content;
static char MAINPAGE[] PROGMEM = R"=====(<!DOCTYPE HTML>
<html>
Hello world from ESP8266
<p>
</html>
)=====";
static void handleRoot(void) static void handleRoot(void)
{ {
content = F("<!DOCTYPE HTML>\n<html>Hello world from ESP8266"); wwwserver.send_P(200, "text/html", MAINPAGE);
content += F("<p>");
content += F("</html>");
wwwserver.send(200, "text/html", content);
} }
void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state) void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state)
@ -82,7 +69,7 @@ void ICACHE_FLASH_ATTR saveDevname(const char *param)
{ {
String p = 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() void ICACHE_FLASH_ATTR setup()
@ -90,28 +77,16 @@ void ICACHE_FLASH_ATTR setup()
WiFiConfig wifi; // konfigurace ESP modulu WiFiConfig wifi; // konfigurace ESP modulu
WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname); WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname);
EEPROM.begin(EEPROM_SIZE); // zahajujeme praci s EEPROM LittleFS.begin();
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();
}
pinMode(PIN_FORCE_CONFIG, INPUT_PULLUP); // pin, co slouzi jako vstup tlacitka pinMode(PIN_FORCE_CONFIG, INPUT_PULLUP); // pin, co slouzi jako vstup tlacitka
int fc = digitalRead(PIN_FORCE_CONFIG); // pozadavek na vynucene vyvolani konfigurace int fc = digitalRead(PIN_FORCE_CONFIG); // pozadavek na vynucene vyvolani konfigurace
Serial.begin(115200); 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()); strcpy(WiFiDeviceName, dn.c_str());
wifi.addParameter(&devname); 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(); ESP.restart();
wwwserver.on("/", handleRoot); wwwserver.on("/", handleRoot);

View File

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

View File

@ -1,5 +1,5 @@
name=WiFiConfig name=WiFiConfig
version=6.4.0 version=7.0.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.

View File

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

View File

@ -130,10 +130,10 @@ class WiFiConfig
{ {
public: public:
WiFiConfig(): _params(NULL), _timeout(0) {} WiFiConfig(): _params(NULL), _timeout(0) {}
wificonfigresult_t begin(int configarea, int forceConfigure, wificonfig_cb cb); wificonfigresult_t begin(int forceConfigure, wificonfig_cb cb);
wificonfigresult_t begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb); wificonfigresult_t begin(int forceConfigure, int timeout, wificonfig_cb cb);
void addParameter(WiFiConfigUsrParameter *p); 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: private:
void _prepareWifi(void); void _prepareWifi(void);
@ -145,8 +145,6 @@ 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);
WiFiConfigUsrParameter *_params; // ukazatel na posledni zadany uzivatelsky parametr WiFiConfigUsrParameter *_params; // ukazatel na posledni zadany uzivatelsky parametr