Skip to content

Commit

Permalink
server port and hostname at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
cotestatnt committed Feb 7, 2024
1 parent 29f6bf2 commit 49b0faa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=esp-fs-webserver
version=2.0.2
version=2.0.3
author=Tolentino Cotesta <[email protected]>
maintainer=Tolentino Cotesta <[email protected]>
sentence=From FSBrowser.ino example to library
Expand Down
25 changes: 15 additions & 10 deletions src/esp-fs-webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
26 changes: 22 additions & 4 deletions src/esp-fs-webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand Down Expand Up @@ -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 ////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 49b0faa

Please sign in to comment.