From 6ade3bf806b70be310b472d32b894dde002abd8e Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE <0xfeedc0de64@gmail.com> Date: Fri, 20 Apr 2018 00:48:17 +0200 Subject: [PATCH] Removed dns and moved script.js --- WifiLamp.ino | 222 ++++++++++++++++---------------------------------- cdn/relais.js | 48 +++++++++++ 2 files changed, 117 insertions(+), 153 deletions(-) create mode 100644 cdn/relais.js diff --git a/WifiLamp.ino b/WifiLamp.ino index e744316..4a2e50b 100644 --- a/WifiLamp.ino +++ b/WifiLamp.ino @@ -1,9 +1,7 @@ #include #include #include -#include -const char* host = "debug1"; const char* ssid = "McDonalds Free WiFi 2.4GHz"; const char* password = "Passwort_123"; @@ -39,7 +37,7 @@ const char* indexContent = "" "" "" - "" + "" "" ""; @@ -74,78 +72,6 @@ const char* updateContent = "" "" ""; -const char* scriptContent = "jQuery(document).ready(function() {" - "var refreshStatus = function() {" - "$('#status')" - ".text('Loading status...')" - ".show();" - - "$.ajax({" - "url: 'status'" - "}).done(function(data) {" - "console.log(data);" - - "if(data=='on') {" - "$('#linkOn').removeClass('hidden');" - "$('#linkOff').addClass('hidden');" - "} else if(data=='off') {" - "$('#linkOn').addClass('hidden');" - "$('#linkOff').removeClass('hidden');" - "}" - - "$('#status')" - ".hide();" - "});" - "};" - - "$('#linkOn').click(function(e) {" - "e.preventDefault();" - - "$('#status')" - ".text('Sending On-command...')" - ".show();" - - "$('#linkOn').addClass('hidden');" - - "$.ajax({" - "url: $('#linkOn').attr('href')" - "}).done(function(data) {" - "console.log(data);" - - "if(data != 'success') {" - "alert('error');" - "}" - - "refreshStatus();" - "});" - "});" - - "$('#linkOff').click(function(e) {" - "e.preventDefault();" - - "$('#status')" - ".text('Sending Off-command...')" - ".show();" - - "$('#linkOff').addClass('hidden');" - - "$.ajax({" - "url: $('#linkOff').attr('href')" - "}).done(function(data) {" - "console.log(data);" - - "if(data != 'success') {" - "alert('error');" - "}" - - "refreshStatus();" - "});" - "});" - - "setInterval(refreshStatus, 15000);" - "refreshStatus();" - "});"; - ESP8266WebServer server(80); void setup() { @@ -157,85 +83,75 @@ void setup() { WiFi.begin(ssid, password); - if (WiFi.waitForConnectResult() == WL_CONNECTED) { - MDNS.begin(host); - - server.on("/", HTTP_GET, []() { - server.sendHeader("Connection", "close"); - server.send(200, "text/html", indexContent); - }); - - server.on("/update", HTTP_GET, []() { - server.sendHeader("Connection", "close"); - server.send(200, "text/html", updateContent); - }); - - server.on("/script.js", HTTP_GET, []() { - server.sendHeader("Connection", "close"); - server.send(200, "text/javascript", scriptContent); - }); - - server.on("/on", HTTP_GET, []() { - digitalWrite(relaisPin, HIGH); - - server.sendHeader("Connection", "close"); - server.send(200, "text/plain", success); - }); - - server.on("/off", HTTP_GET, []() { - digitalWrite(relaisPin, LOW); - - server.sendHeader("Connection", "close"); - server.send(200, "text/plain", success); - }); - - server.on("/toggle", HTTP_GET, []() { - digitalWrite(relaisPin, digitalRead(relaisPin) ? LOW : HIGH); - - server.sendHeader("Connection", "close"); - server.send(200, "text/plain", success); - }); - - server.on("/status", HTTP_GET, []() { - server.sendHeader("Connection", "close"); - server.send(200, "text/plain", digitalRead(relaisPin) ? "on" : "off"); - }); - - server.on("/update", HTTP_POST, []() { - server.sendHeader("Connection", "close"); - server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); - ESP.restart(); - }, []() { - HTTPUpload& upload = server.upload(); - if (upload.status == UPLOAD_FILE_START) { - Serial.setDebugOutput(true); - Serial.printf("Update: %s\n", upload.filename.c_str()); - - uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; - if (!Update.begin(maxSketchSpace)) { //start with max available size - Update.printError(Serial); - } - } else if (upload.status == UPLOAD_FILE_WRITE) { - if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { - Update.printError(Serial); - } - } else if (upload.status == UPLOAD_FILE_END) { - if (Update.end(true)) { //true to set the size to the current progress - Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); - } else { - Update.printError(Serial); - } - - Serial.setDebugOutput(false); - } - yield(); - }); - - server.begin(); - MDNS.addService("http", "tcp", 80); + server.on("/", HTTP_GET, []() { + server.sendHeader("Connection", "close"); + server.send(200, "text/html", indexContent); + }); - Serial.printf("Ready! Open http://%s.local in your browser\n", host); - } else { + server.on("/update", HTTP_GET, []() { + server.sendHeader("Connection", "close"); + server.send(200, "text/html", updateContent); + }); + + server.on("/on", HTTP_GET, []() { + digitalWrite(relaisPin, HIGH); + + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", success); + }); + + server.on("/off", HTTP_GET, []() { + digitalWrite(relaisPin, LOW); + + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", success); + }); + + server.on("/toggle", HTTP_GET, []() { + digitalWrite(relaisPin, digitalRead(relaisPin) ? LOW : HIGH); + + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", success); + }); + + server.on("/status", HTTP_GET, []() { + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", digitalRead(relaisPin) ? "on" : "off"); + }); + + server.on("/update", HTTP_POST, []() { + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); + ESP.restart(); + }, []() { + HTTPUpload& upload = server.upload(); + if (upload.status == UPLOAD_FILE_START) { + Serial.setDebugOutput(true); + Serial.printf("Update: %s\n", upload.filename.c_str()); + + uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000; + if (!Update.begin(maxSketchSpace)) { //start with max available size + Update.printError(Serial); + } + } else if (upload.status == UPLOAD_FILE_WRITE) { + if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { + Update.printError(Serial); + } + } else if (upload.status == UPLOAD_FILE_END) { + if (Update.end(true)) { //true to set the size to the current progress + Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); + } else { + Update.printError(Serial); + } + + Serial.setDebugOutput(false); + } + yield(); + }); + + server.begin(); + + if (WiFi.waitForConnectResult() != WL_CONNECTED) { for(int i = 0; i < 2; i++) { digitalWrite(relaisPin, LOW); delay(500); diff --git a/cdn/relais.js b/cdn/relais.js new file mode 100644 index 0000000..d70f33b --- /dev/null +++ b/cdn/relais.js @@ -0,0 +1,48 @@ +jQuery(document).ready(function() { + var refreshStatus = function() { + $('#status').text('Loading status...').show(); + $.ajax({ + url: 'status' + }).done(function(data) { + console.log(data); + if (data == 'on') { + $('#linkOn').removeClass('hidden'); + $('#linkOff').addClass('hidden'); + } else if (data == 'off') { + $('#linkOn').addClass('hidden'); + $('#linkOff').removeClass('hidden'); + } + $('#status').hide(); + }); + }; + $('#linkOn').click(function(e) { + e.preventDefault(); + $('#status').text('Sending On-command...').show(); + $('#linkOn').addClass('hidden'); + $.ajax({ + url: $('#linkOn').attr('href') + }).done(function(data) { + console.log(data); + if (data != 'success') { + alert('error'); + } + refreshStatus(); + }); + }); + $('#linkOff').click(function(e) { + e.preventDefault(); + $('#status').text('Sending Off-command...').show(); + $('#linkOff').addClass('hidden'); + $.ajax({ + url: $('#linkOff').attr('href') + }).done(function(data) { + console.log(data); + if (data != 'success') { + alert('error'); + } + refreshStatus(); + }); + }); + setInterval(refreshStatus, 15000); + refreshStatus(); +});