Browse Source

Option to change the port the embedded webserver is listening to, defaults to 80

fastled
Xose Pérez 7 years ago
parent
commit
bab4c9d433
5 changed files with 43 additions and 19 deletions
  1. +1
    -0
      code/espurna/config/general.h
  2. +1
    -1
      code/espurna/espurna.ino
  3. +25
    -15
      code/espurna/web.ino
  4. +5
    -2
      code/html/custom.js
  5. +11
    -1
      code/html/index.html

+ 1
- 0
code/espurna/config/general.h View File

@ -82,6 +82,7 @@
#define HTTP_USERNAME "admin" #define HTTP_USERNAME "admin"
#define WS_BUFFER_SIZE 5 #define WS_BUFFER_SIZE 5
#define WS_TIMEOUT 1800000 #define WS_TIMEOUT 1800000
#define WEBSERVER_PORT 80
#define DNS_PORT 53 #define DNS_PORT 53
#define AP_MODE AP_MODE_ALONE #define AP_MODE AP_MODE_ALONE


+ 1
- 1
code/espurna/espurna.ino View File

@ -98,13 +98,13 @@ void setup() {
saveSettings(); saveSettings();
} }
webSetup();
relaySetup(); relaySetup();
buttonSetup(); buttonSetup();
ledSetup(); ledSetup();
wifiSetup(); wifiSetup();
otaSetup(); otaSetup();
mqttSetup(); mqttSetup();
webSetup();
ntpSetup(); ntpSetup();
#if ENABLE_I2C #if ENABLE_I2C


+ 25
- 15
code/espurna/web.ino View File

@ -16,7 +16,7 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#include <Ticker.h> #include <Ticker.h>
#include <vector> #include <vector>
AsyncWebServer server(80);
AsyncWebServer * _server;
AsyncWebSocket ws("/ws"); AsyncWebSocket ws("/ws");
Ticker deferred; Ticker deferred;
@ -157,6 +157,15 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
#endif #endif
// Web portions
if (key == "webPort") {
if ((value.toInt() == 0) || (value.toInt() == 80)) {
save = changed = true;
delSetting(key);
continue;
}
}
// Check password // Check password
if (key == "adminPass1") { if (key == "adminPass1") {
adminPass = value; adminPass = value;
@ -325,6 +334,8 @@ void _wsStart(uint32_t client_id) {
root["relaySync"] = getSetting("relaySync", RELAY_SYNC); root["relaySync"] = getSetting("relaySync", RELAY_SYNC);
} }
root["webPort"] = getSetting("webPort", WEBSERVER_PORT).toInt();
root["apiEnabled"] = getSetting("apiEnabled").toInt() == 1; root["apiEnabled"] = getSetting("apiEnabled").toInt() == 1;
root["apiKey"] = getSetting("apiKey"); root["apiKey"] = getSetting("apiKey");
@ -584,7 +595,7 @@ void apiRegister(const char * url, const char * key, apiGetCallbackFunction getF
// Bind call // Bind call
unsigned int methods = HTTP_GET; unsigned int methods = HTTP_GET;
if (putFn != NULL) methods += HTTP_PUT; 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() { void webSetup() {
// Create server
_server = new AsyncWebServer(getSetting("webPort", WEBSERVER_PORT).toInt());
// Setup websocket // Setup websocket
ws.onEvent(_wsEvent); ws.onEvent(_wsEvent);
mqttRegister(wsMQTTCallback); mqttRegister(wsMQTTCallback);
// Setup webserver // Setup webserver
server.addHandler(&ws);
_server->addHandler(&ws);
// Serve home (basic authentication protection) // 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 // Serve static files
char lastModified[50]; char lastModified[50];
sprintf(lastModified, "%s %s GMT", __DATE__, __TIME__); sprintf(lastModified, "%s %s GMT", __DATE__, __TIME__);
server.serveStatic("/", SPIFFS, "/").setLastModified(lastModified);
_server->serveStatic("/", SPIFFS, "/").setLastModified(lastModified);
// 404 // 404
server.onNotFound([](AsyncWebServerRequest *request){
_server->onNotFound([](AsyncWebServerRequest *request){
request->send(404); request->send(404);
}); });
// Run server // Run server
server.begin();
_server->begin();
} }

+ 5
- 2
code/html/custom.js View File

@ -335,11 +335,14 @@ function getJson(str) {
} }
} }
function connect(host) {
function connect(host, port) {
if (typeof host === 'undefined') { if (typeof host === 'undefined') {
host = window.location.hostname; 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) { websock.onopen = function(evt) {
console.log("Connected"); console.log("Connected");
}; };


+ 11
- 1
code/html/index.html View File

@ -276,6 +276,16 @@
<input name="adminPass2" class="pure-u-1 pure-u-md-3-4" type="password" tabindex="12" /> <input name="adminPass2" class="pure-u-1 pure-u-md-3-4" type="password" tabindex="12" />
</div> </div>
<div class="pure-g">
<label class="pure-u-1 pure-u-md-1-4" for="webPort">HTTP port</label>
<input name="webPort" class="pure-u-1 pure-u-md-3-4" type="text" tabindex="13" />
<div class="pure-u-0 pure-u-md-1-4">&nbsp;</div>
<div class="pure-u-1 pure-u-md-3-4 hint">
This is the port for the web interface and API requests.<br />
If different than 80 (standard HTTP port) you will have to add it explicitly to your requests: http://myip:myport/
</div>
</div>
<div class="pure-g"> <div class="pure-g">
<div class="pure-u-1 pure-u-sm-1-4"><label for="apiEnabled">Enable HTTP API</label></div> <div class="pure-u-1 pure-u-sm-1-4"><label for="apiEnabled">Enable HTTP API</label></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="apiEnabled" /></div> <div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="apiEnabled" /></div>
@ -283,7 +293,7 @@
<div class="pure-g"> <div class="pure-g">
<label class="pure-u-1 pure-u-md-1-4" for="apiKey">HTTP API Key</label> <label class="pure-u-1 pure-u-md-1-4" for="apiKey">HTTP API Key</label>
<input name="apiKey" class="pure-u-3-4 pure-u-md-1-2" type="text" tabindex="13" />
<input name="apiKey" class="pure-u-3-4 pure-u-md-1-2" type="text" tabindex="14" />
<div class=" pure-u-1-4 pure-u-md-1-4"><button class="pure-button button-apikey pure-u-23-24">Generate</button></div> <div class=" pure-u-1-4 pure-u-md-1-4"><button class="pure-button button-apikey pure-u-23-24">Generate</button></div>
<div class="pure-u-0 pure-u-md-1-4">&nbsp;</div> <div class="pure-u-0 pure-u-md-1-4">&nbsp;</div>
<div class="pure-u-1 pure-u-md-3-4 hint"> <div class="pure-u-1 pure-u-md-3-4 hint">


Loading…
Cancel
Save