From 49b0faa8e76364b3fbce6b51035acc1796c5313f Mon Sep 17 00:00:00 2001 From: cotestatnt Date: Wed, 7 Feb 2024 09:03:30 +0100 Subject: [PATCH] server port and hostname at runtime --- library.properties | 2 +- src/esp-fs-webserver.cpp | 25 +++++++++++++++---------- src/esp-fs-webserver.h | 26 ++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/library.properties b/library.properties index e6aef95..04afc9d 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=esp-fs-webserver -version=2.0.2 +version=2.0.3 author=Tolentino Cotesta maintainer=Tolentino Cotesta sentence=From FSBrowser.ino example to library diff --git a/src/esp-fs-webserver.cpp b/src/esp-fs-webserver.cpp index a26c2dc..d5b627b 100644 --- a/src/esp-fs-webserver.cpp +++ b/src/esp-fs-webserver.cpp @@ -62,10 +62,15 @@ void FSWebServer::handleClient() #endif +void FSWebServer::setHostname(const char* host) { + m_host = host; +} + + // Override default begin() method to set library built-in handlers -void FSWebServer::begin() +void FSWebServer::begin(uint16_t port) { - + m_port = port; #if ESP_FS_WS_SETUP m_fsOK = setup->checkConfigFile(); if (setup->isOpened()) { @@ -102,15 +107,15 @@ void FSWebServer::begin() this->enableCrossOrigin(true); _server.setNoDelay(true); #endif - close(); - _server.begin(); + _server.begin(port); #ifdef ESP8266 - WiFi.hostname(m_host); + WiFi.hostname(m_host.c_str()); #elif defined(ESP32) - WiFi.setHostname(m_host); + WiFi.setHostname(m_host.c_str()); #endif + log_info("Server started on %s:%d\n", WiFi.localIP().toString().c_str(), port); } @@ -166,8 +171,8 @@ IPAddress FSWebServer::startAP() } WiFi.mode(WIFI_AP); - // Set AP IP 8.8.8.8 and subnet 255.255.255.0 - if (! WiFi.softAPConfig(0x08080808, 0x08080808, 0x00FFFFFF)) { + // Set AP IP and subnet 255.255.255.0 + if (! WiFi.softAPConfig(m_captiveIp, m_captiveIp, 0x00FFFFFF)) { log_error("Captive portal failed to start: WiFi.softAPConfig failed!"); WiFi.enableAP(false); return IPAddress((uint32_t) 0); @@ -190,7 +195,7 @@ IPAddress FSWebServer::startAP() } ip = WiFi.softAPIP(); - log_info("AP mode.\nServer IP address: %s", ip.toString().c_str()); + log_info("\nAP mode.\nIP address: %s", ip.toString().c_str()); m_apmode = true; return ip; } @@ -265,7 +270,7 @@ IPAddress FSWebServer::startWiFi(uint32_t timeout, bool apFlag, CallbackF fn) log_error("MDNS responder not started"); } MDNS.addService("http", "tcp", m_port); - MDNS.setInstanceName("esp-fs-webserver"); + MDNS.setInstanceName(m_host); if ((WiFi.status() == WL_CONNECTED) || apFlag) return WiFi.localIP(); diff --git a/src/esp-fs-webserver.h b/src/esp-fs-webserver.h index 3196728..ca2409d 100644 --- a/src/esp-fs-webserver.h +++ b/src/esp-fs-webserver.h @@ -91,15 +91,20 @@ class FSWebServer : public WebServerClass public: - FSWebServer(fs::FS& fs, uint16_t port, const char* host = "esphost"): + FSWebServer(fs::FS& fs, uint16_t port = 80, const char* host = "esphost"): WebServerClass(port), m_filesystem(&fs), - m_host((char*)host) + m_host(host) { setup = new SetupConfigurator(m_filesystem); m_port = port; } + /* + * Set web server hostname + */ + void setHostname(const char* host); + /* * setup web page "configurator" class reference */ @@ -108,7 +113,7 @@ class FSWebServer : public WebServerClass /* * Override default begin() method to set library built-in handlers */ - virtual void begin(); + virtual void begin(uint16_t port = 80); /* * Call this method in your loop @@ -194,6 +199,18 @@ class FSWebServer : public WebServerClass */ bool clearOptions(); + /* + * Set IP address in AP mode + */ + void setIPaddressAP(IPAddress ip) { + m_captiveIp = ip; + } + + /* + * Get current web server port + */ + uint16_t getPort() { return m_port;} + ///////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////// SETUP PAGE CONFIGURATION //////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////// @@ -233,12 +250,13 @@ class FSWebServer : public WebServerClass String m_apWebpage = "/setup"; String m_apSsid = ""; String m_apPsk = ""; + String m_host; uint32_t m_timeout = 10000; bool m_fsOK = false; bool m_apmode = false; uint16_t m_port = 80; char m_version[16] = {__TIME__}; - char* m_host; + IPAddress m_captiveIp = IPAddress(192, 168, 4, 1); #if defined(ESP32) // Override default handleClient() method to increase connection speed