diff --git a/src/hasp_config.cpp b/src/hasp_config.cpp index b1ccf938..6c35abc4 100644 --- a/src/hasp_config.cpp +++ b/src/hasp_config.cpp @@ -174,13 +174,44 @@ void configWriteConfig() } } -void configSetup(JsonDocument & settings) +void configSetup(JsonObject dummy) { if(!SPIFFS.begin()) { - Log.error(F("FILE: SPI flash init failed. Unable to mount FS.")); + Log.error(F("FILE: SPI flash init failed. Unable to mount FS: Using default settings...")); } else { + DynamicJsonDocument settings(1024 + 128); + configGetConfig(settings, true); + Log.verbose(F("Loading debug settings")); + debugSetConfig(settings[F("debug")]); + Log.verbose(F("Loading GUI settings")); + guiSetConfig(settings[F("gui")]); + Log.verbose(F("Loading HASP settings")); + haspSetConfig(settings[F("hasp")]); + // otaGetConfig(settings[F("ota")]); + +#if HASP_USE_WIFI + Log.verbose(F("Loading WiFi settings")); + wifiSetConfig(settings[F("wifi")]); +#if HASP_USE_MQTT + Log.verbose(F("Loading MQTT settings")); + mqttSetConfig(settings[F("mqtt")]); +#endif +#if HASP_USE_TELNET + Log.verbose(F("Loading Telnet settings")); + telnetSetConfig(settings[F("telnet")]); +#endif +#if HASP_USE_MDNS + Log.verbose(F("Loading MDNS settings")); + mdnsSetConfig(settings[F("mdns")]); +#endif +#if HASP_USE_HTTP + Log.verbose(F("Loading HTTP settings")); + httpSetConfig(settings[F("http")]); +#endif +#endif } + Log.notice(F("User configuration loaded")); } void configOutput(const JsonObject & settings) diff --git a/src/hasp_config.h b/src/hasp_config.h index 7555ec9c..615b9a73 100644 --- a/src/hasp_config.h +++ b/src/hasp_config.h @@ -32,7 +32,7 @@ const char F_DEBUG_TELEPERIOD[] PROGMEM = "teleperiod"; const char HASP_CONFIG_FILE[] PROGMEM = "/config.json"; -void configSetup(JsonDocument & settings); +void configSetup(JsonObject settings); void configStop(void); void configSetConfig(JsonObject & settings); diff --git a/src/hasp_gui.h b/src/hasp_gui.h index c0289f3f..56d7f864 100644 --- a/src/hasp_gui.h +++ b/src/hasp_gui.h @@ -16,7 +16,7 @@ void guiTakeScreenshot(ESP8266WebServer & client); void guiTakeScreenshot(WebServer & client); #endif // ESP32 -void guiSetup(JsonObject settings); +void guiSetup(const JsonObject & settings); void guiLoop(void); void guiStop(void); diff --git a/src/hasp_http.cpp b/src/hasp_http.cpp index edac871f..6eb8a95e 100644 --- a/src/hasp_http.cpp +++ b/src/hasp_http.cpp @@ -541,7 +541,7 @@ static inline String httpGetNodename() } //////////////////////////////////////////////////////////////////////////////////////////////////// -bool httpIsAuthenticated(const String & page) +bool httpIsAuthenticated(const __FlashStringHelper * page) { if(httpPassword[0] != '\0') { // Request HTTP auth if httpPassword is set if(!webServer.authenticate(httpUser, httpPassword)) { @@ -551,7 +551,7 @@ bool httpIsAuthenticated(const String & page) } { - Log.verbose(F("HTTP: Sending %s page to client connected from: %s"), page.c_str(), + Log.verbose(F("HTTP: Sending %s page to client connected from: %s"), page, webServer.client().remoteIP().toString().c_str()); } return true; @@ -982,10 +982,7 @@ bool handleFileRead(String path) { if(!httpIsAuthenticated(F("fileread"))) return false; - // path = urldecode(path).substring(0, 31); - path = webServer.urlDecode(path); - if(!httpIsAuthenticated(path)) return false; - + path = webServer.urlDecode(path).substring(0, 31); if(path.endsWith("/")) { path += F("index.htm"); } @@ -1829,10 +1826,25 @@ void httpHandleResetConfig() } } +void webStart() +{ + webServer.begin(); + webServerStarted = true; + Log.notice(F("HTTP: Server started @ http://%s"), + (WiFi.getMode() == WIFI_AP ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); +} + +void webStop() +{ + webServer.stop(); + webServerStarted = false; + Log.warning(F("HTTP: Server stoped")); +} + //////////////////////////////////////////////////////////////////////////////////////////////////// void httpSetup(const JsonObject & settings) { - httpSetConfig(settings); + // httpSetConfig(settings); if(WiFi.getMode() == WIFI_AP) { Log.notice(F("HTTP: Wifi access point")); @@ -1905,8 +1917,8 @@ void httpSetup(const JsonObject & settings) webServer.on(F("/config"), webHandleConfig); webServer.onNotFound(httpHandleNotFound); - httpReconnect(); Log.verbose(F("HTTP: Setup Complete")); + webStart(); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1915,15 +1927,9 @@ void httpReconnect() if(!httpEnable) return; if(webServerStarted) { - webServer.stop(); - webServerStarted = false; - Log.warning(F("HTTP: Server stoped")); + webStop(); } else if(WiFi.status() == WL_CONNECTED || WiFi.getMode() == WIFI_AP) { - webServer.begin(); - webServerStarted = true; - Log.notice( - F("HTTP: Server started @ http://%s"), - (WiFi.getMode() == WIFI_AP ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); + webStart(); } } diff --git a/src/hasp_mdns.cpp b/src/hasp_mdns.cpp index 92894cef..b39896fd 100644 --- a/src/hasp_mdns.cpp +++ b/src/hasp_mdns.cpp @@ -22,7 +22,7 @@ uint8_t mdnsEnabled = true; void mdnsSetup(const JsonObject & settings) { - mdnsSetConfig(settings); + // mdnsSetConfig(settings); Log.verbose(F("MDNS: Setup Complete")); } @@ -43,7 +43,6 @@ void mdnsStart() HASP_VERSION_REVISION); MDNS.addServiceTxt(hasp2Node, "tcp", "app_version", buffer); */ if(MDNS.begin(hasp2Node.c_str())) { - Log.notice(F("MDNS: Responder started")); MDNS.addService(F("http"), F("tcp"), 80); MDNS.addService(F("telnet"), F("tcp"), 23); MDNS.addServiceTxt(hasp2Node, F("tcp"), F("app_name"), F("HASP-lvgl")); @@ -53,6 +52,7 @@ void mdnsStart() addServiceTxt("arduino", "tcp", "ssh_upload", "no"); addServiceTxt("arduino", "tcp", "board", ARDUINO_BOARD); addServiceTxt("arduino", "tcp", "auth_upload", (auth) ? "yes" : "no");*/ + Log.notice(F("MDNS: Responder started")); } else { Log.error(F("MDNS: Responder failed to start %s"), hasp2Node.c_str()); }; diff --git a/src/hasp_spiffs.cpp b/src/hasp_spiffs.cpp index 9bc44bd1..3ca100a1 100644 --- a/src/hasp_spiffs.cpp +++ b/src/hasp_spiffs.cpp @@ -44,7 +44,7 @@ void spiffsSetup() #endif Log.error(F("FILE: SPI flash init failed. Unable to mount FS.")); } else { - Log.notice(F("FILE: SPI Flash FS mounted")); + Log.verbose(F("FILE: SPI Flash FS mounted")); } #endif } diff --git a/src/hasp_wifi.h b/src/hasp_wifi.h index 73628d8c..d09f9a59 100644 --- a/src/hasp_wifi.h +++ b/src/hasp_wifi.h @@ -4,7 +4,7 @@ #include "ArduinoJson.h" void wifiSetup(JsonObject settings); -bool wifiLoop(void); +bool wifiEvery5Seconds(void); void wifiStop(void); bool wifiGetConfig(const JsonObject & settings); diff --git a/src/main.cpp b/src/main.cpp index ad8ee2f2..de247e01 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,6 +54,10 @@ unsigned long mainLastLoopTime = 0; void setup() { + + /**************************** + * Constant initialzations + ***************************/ #if defined(ARDUINO_ARCH_ESP8266) pinMode(D1, OUTPUT); pinMode(D2, INPUT_PULLUP); @@ -68,20 +72,29 @@ void setup() spiffsSetup(); #endif - /* Read Config File */ - DynamicJsonDocument settings(1024 + 512); - configSetup(settings); - #if HASP_USE_SDCARD sdcardSetup(); #endif + /**************************** + * Read & Apply User Configuration + ***************************/ + DynamicJsonDocument settings(128); + configSetup(settings[F("debug")]); + + /**************************** + * Apply User Configuration + ***************************/ debugSetup(settings[F("debug")]); /* Init Graphics */ // TFT_eSPI screen = TFT_eSPI(); guiSetup(settings[F("gui")]); +#if HASP_USE_WIFI + wifiSetup(settings[F("wifi")]); +#endif + /* Init GUI Application */ haspSetup(settings[F("hasp")]); @@ -89,6 +102,10 @@ void setup() #if HASP_USE_WIFI wifiSetup(settings[F("wifi")]); +#if HASP_USE_HTTP + httpSetup(settings[F("http")]); +#endif + #if HASP_USE_MQTT mqttSetup(settings[F("mqtt")]); #endif @@ -101,19 +118,15 @@ void setup() mdnsSetup(settings[F("mdns")]); #endif -#if HASP_USE_HTTP - httpSetup(settings[F("http")]); -#endif - -#if HASP_USE_BUTTON - buttonSetup(); -#endif - #if HASP_USE_OTA otaSetup(settings[F("ota")]); #endif #endif // WIFI + +#if HASP_USE_BUTTON + buttonSetup(); +#endif } void loop() @@ -183,7 +196,7 @@ void loop() /* Run Every 5 Seconds */ if(mainLoopCounter == 0 || mainLoopCounter == 5) { httpEvery5Seconds(); - isConnected = wifiLoop(); + isConnected = wifiEvery5Seconds(); mqttEvery5Seconds(isConnected); } }