From cafb6eadaa5ba7f16ba62f41ba52ea5938e44fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 31 Dec 2016 07:13:16 +0100 Subject: [PATCH] Allow static IP --- code/espurna/config/general.h | 2 +- code/espurna/web.ino | 42 +++++++++- code/espurna/wifi.ino | 21 ++++- code/html/custom.css | 22 ++++- code/html/custom.js | 60 ++++++++++++-- code/html/index.html | 146 +++++++++++++++++++--------------- 6 files changed, 213 insertions(+), 80 deletions(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 3260496f..c153bbdf 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -41,7 +41,7 @@ // ----------------------------------------------------------------------------- #define WIFI_RECONNECT_INTERVAL 300000 -#define WIFI_MAX_NETWORKS 3 +#define WIFI_MAX_NETWORKS 5 #define ADMIN_PASS "fibonacci" #define HTTP_USERNAME "admin" #define WS_BUFFER_SIZE 5 diff --git a/code/espurna/web.ino b/code/espurna/web.ino index f7f85836..327dcbe7 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -161,6 +161,18 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { } if (key == "pass") { key = key + String(network); + } + if (key == "ip") { + key = key + String(network); + } + if (key == "gw") { + key = key + String(network); + } + if (key == "mask") { + key = key + String(network); + } + if (key == "dns") { + key = key + String(network); ++network; } @@ -184,6 +196,26 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { } #endif + // Clean wifi networks + for (int i = 0; i < network; i++) { + if (getSetting("pass" + String(i)).length() == 0) delSetting("pass" + String(i)); + if (getSetting("ip" + String(i)).length() == 0) delSetting("ip" + String(i)); + if (getSetting("gw" + String(i)).length() == 0) delSetting("gw" + String(i)); + if (getSetting("mask" + String(i)).length() == 0) delSetting("mask" + String(i)); + if (getSetting("dns" + String(i)).length() == 0) delSetting("dns" + String(i)); + } + for (int i = network; i 0) { + dirty = true; + } + delSetting("ssid" + String(i)); + delSetting("pass" + String(i)); + delSetting("ip" + String(i)); + delSetting("gw" + String(i)); + delSetting("mask" + String(i)); + delSetting("dns" + String(i)); + } + // Save settings if (dirty) { @@ -239,7 +271,7 @@ void _wsStart(uint32_t client_id) { root["device"] = String(DEVICE); root["hostname"] = getSetting("hostname", HOSTNAME); root["network"] = getNetwork(); - root["ip"] = getIP(); + root["deviceip"] = getIP(); root["mqttStatus"] = mqttConnected(); root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER); @@ -308,11 +340,17 @@ void _wsStart(uint32_t client_id) { root["powActivePower"] = getActivePower(); #endif + root["maxNetworks"] = WIFI_MAX_NETWORKS; JsonArray& wifi = root.createNestedArray("wifi"); - for (byte i=0; i<3; i++) { + for (byte i=0; i 0) jw.addNetwork(getSetting("ssid0").c_str(), getSetting("pass0").c_str()); - if (getSetting("ssid1").length() > 0) jw.addNetwork(getSetting("ssid1").c_str(), getSetting("pass1").c_str()); - if (getSetting("ssid2").length() > 0) jw.addNetwork(getSetting("ssid2").c_str(), getSetting("pass2").c_str()); + for (int i = 0; i< WIFI_MAX_NETWORKS; i++) { + if (getSetting("ssid" + String(i)).length() == 0) break; + if (getSetting("ip" + String(i)).length() == 0) { + jw.addNetwork( + getSetting("ssid" + String(i)).c_str(), + getSetting("pass" + String(i)).c_str() + ); + } else { + jw.addNetwork( + getSetting("ssid" + String(i)).c_str(), + getSetting("pass" + String(i)).c_str(), + getSetting("ip" + String(i)).c_str(), + getSetting("gw" + String(i)).c_str(), + getSetting("mask" + String(i)).c_str(), + getSetting("dns" + String(i)).c_str() + ); + } + } } void wifiSetup() { diff --git a/code/html/custom.css b/code/html/custom.css index f0d3d89a..8a1aac33 100644 --- a/code/html/custom.css +++ b/code/html/custom.css @@ -41,6 +41,15 @@ background: rgb(0, 202, 0); margin-left: 5px; } +.button-add-network { + background: rgb(28, 184, 65); +} +.button-del-network { + background: rgb(202, 60, 60); +} +.button-more-network { + background: rgb(223, 117, 20); +} .pure-g { margin-bottom: 20px; } @@ -57,14 +66,23 @@ div.hint { font-size: 80%; color: #ccc; } - +.break { + margin-top: 5px; +} +#networks .pure-g { + padding-bottom: 10px; + margin-bottom: 5px; + border-bottom: 2px dashed #e5e5e5; +} +#networks div.more { + display: none; +} .module { display: none; } .template { display: none; } - .pure-form .center { margin: .5em 0 .2em; } diff --git a/code/html/custom.js b/code/html/custom.js index d721084b..c6a2791a 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -1,5 +1,6 @@ var websock; var password = false; +var maxNetworks; // http://www.the-art-of-web.com/javascript/validate-password/ function checkPassword(str) { @@ -132,6 +133,38 @@ function createIdxs(count) { } +function delNetwork() { + var parent = $(this).parents(".pure-g"); + $(parent).remove(); +} + +function moreNetwork() { + var parent = $(this).parents(".pure-g"); + $("div.more", parent).toggle(); +} + +function addNetwork() { + + var numNetworks = $("#networks > div").length; + if (numNetworks >= maxNetworks) { + alert("Max number of networks reached"); + return; + } + + var tabindex = 200 + numNetworks * 10; + var template = $("#networkTemplate").children(); + var line = $(template).clone(); + $(line).find("input").each(function() { + $(this).attr("tabindex", tabindex++); + }); + $(line).find(".button-del-network").on('click', delNetwork); + $(line).find(".button-more-network").on('click', moreNetwork); + line.appendTo("#networks"); + + return line; + +} + function processData(data) { // title @@ -178,17 +211,32 @@ function processData(data) { } + if (key == "maxNetworks") { + maxNetworks = parseInt(data.maxNetworks); + return; + } + // Wifi if (key == "wifi") { - var groups = $("#panel-wifi .pure-g"); - for (var i in data.wifi) { + + var networks = data.wifi; + + for (var i in networks) { + + // add a new row + var line = addNetwork(); + + // fill in the blanks var wifi = data.wifi[i]; Object.keys(wifi).forEach(function(key) { - var id = "input[name=" + key + "]"; - if ($(id, groups[i]).length) $(id, groups[i]).val(wifi[key]); + var element = $("input[name=" + key + "]", line); + if (element.length) element.val(wifi[key]); }); - }; + + } + return; + } // Relay status @@ -233,7 +281,6 @@ function processData(data) { // Enable options if (key.endsWith("Visible")) { var module = key.slice(0,-7); - console.log(module); $(".module-" + module).show(); return; } @@ -305,6 +352,7 @@ function init() { $(".button-reconnect").on('click', doReconnect); $(".button-apikey").on('click', doGenerateAPIKey); $(".pure-menu-link").on('click', showPanel); + $(".button-add-network").on('click', addNetwork); $.ajax({ 'method': 'GET', diff --git a/code/html/index.html b/code/html/index.html index 2045e6c0..3e9718ae 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -111,8 +111,8 @@
- - + +
@@ -143,13 +143,6 @@
-
-
- -
-
-
- @@ -192,7 +185,7 @@
-