diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 58e52335..e502debb 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -82,6 +82,7 @@ #define HTTP_USERNAME "admin" #define WS_BUFFER_SIZE 5 #define WS_TIMEOUT 1800000 +#define WEBSERVER_PORT 80 #define DNS_PORT 53 #define AP_MODE AP_MODE_ALONE diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 3461e6b4..93c80a7c 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -98,13 +98,13 @@ void setup() { saveSettings(); } + webSetup(); relaySetup(); buttonSetup(); ledSetup(); wifiSetup(); otaSetup(); mqttSetup(); - webSetup(); ntpSetup(); #if ENABLE_I2C diff --git a/code/espurna/web.ino b/code/espurna/web.ino index b7283c26..658efcb8 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -16,7 +16,7 @@ Copyright (C) 2016-2017 by Xose PĂ©rez #include #include -AsyncWebServer server(80); +AsyncWebServer * _server; AsyncWebSocket ws("/ws"); Ticker deferred; @@ -157,6 +157,15 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { #endif + // Web portions + if (key == "webPort") { + if ((value.toInt() == 0) || (value.toInt() == 80)) { + save = changed = true; + delSetting(key); + continue; + } + } + // Check password if (key == "adminPass1") { adminPass = value; @@ -325,6 +334,8 @@ void _wsStart(uint32_t client_id) { root["relaySync"] = getSetting("relaySync", RELAY_SYNC); } + root["webPort"] = getSetting("webPort", WEBSERVER_PORT).toInt(); + root["apiEnabled"] = getSetting("apiEnabled").toInt() == 1; root["apiKey"] = getSetting("apiKey"); @@ -584,7 +595,7 @@ void apiRegister(const char * url, const char * key, apiGetCallbackFunction getF // Bind call unsigned int methods = HTTP_GET; if (putFn != NULL) methods += HTTP_PUT; - server.on(url, methods, _bindAPI(_apis.size() - 1)); + _server->on(url, methods, _bindAPI(_apis.size() - 1)); } @@ -683,37 +694,36 @@ void _onAuth(AsyncWebServerRequest *request) { } -AsyncWebServer * getServer() { - return &server; -} - void webSetup() { + // Create server + _server = new AsyncWebServer(getSetting("webPort", WEBSERVER_PORT).toInt()); + // Setup websocket ws.onEvent(_wsEvent); mqttRegister(wsMQTTCallback); // Setup webserver - server.addHandler(&ws); + _server->addHandler(&ws); // Serve home (basic authentication protection) - server.on("/", HTTP_GET, _onHome); - server.on("/index.html", HTTP_GET, _onHome); - server.on("/auth", HTTP_GET, _onAuth); - server.on("/apis", HTTP_GET, _onAPIs); - server.on("/rpc", HTTP_GET, _onRPC); + _server->on("/", HTTP_GET, _onHome); + _server->on("/index.html", HTTP_GET, _onHome); + _server->on("/auth", HTTP_GET, _onAuth); + _server->on("/apis", HTTP_GET, _onAPIs); + _server->on("/rpc", HTTP_GET, _onRPC); // Serve static files char lastModified[50]; sprintf(lastModified, "%s %s GMT", __DATE__, __TIME__); - server.serveStatic("/", SPIFFS, "/").setLastModified(lastModified); + _server->serveStatic("/", SPIFFS, "/").setLastModified(lastModified); // 404 - server.onNotFound([](AsyncWebServerRequest *request){ + _server->onNotFound([](AsyncWebServerRequest *request){ request->send(404); }); // Run server - server.begin(); + _server->begin(); } diff --git a/code/html/custom.js b/code/html/custom.js index d9c35bfc..4724b509 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -335,11 +335,14 @@ function getJson(str) { } } -function connect(host) { +function connect(host, port) { if (typeof host === 'undefined') { host = window.location.hostname; } - websock = new WebSocket('ws://' + host + '/ws'); + if (typeof port === 'undefined') { + port = location.port; + } + websock = new WebSocket('ws://' + host + ':' + port + '/ws'); websock.onopen = function(evt) { console.log("Connected"); }; diff --git a/code/html/index.html b/code/html/index.html index 94111a25..a7901f98 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -276,6 +276,16 @@ +
+ + +
 
+
+ This is the port for the web interface and API requests.
+ If different than 80 (standard HTTP port) you will have to add it explicitly to your requests: http://myip:myport/ +
+
+
@@ -283,7 +293,7 @@
- +