Removed dns and moved script.js

This commit is contained in:
0xFEEDC0DE
2018-04-20 00:48:17 +02:00
parent 9aedb4b66c
commit 6ade3bf806
2 changed files with 117 additions and 153 deletions

View File

@@ -1,9 +1,7 @@
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* host = "debug1";
const char* ssid = "McDonalds Free WiFi 2.4GHz";
const char* password = "Passwort_123";
@@ -39,7 +37,7 @@ const char* indexContent = "<!doctype html>"
"<script src=\"https://code.jquery.com/jquery-3.3.1.min.js\" integrity=\"sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT\" crossorigin=\"anonymous\"></script>"
"<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js\" integrity=\"sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl\" crossorigin=\"anonymous\"></script>"
"<script src=\"script.js\"></script>"
"<script src=\"https://brunner.ninja/relais.js\"></script>"
"</body>"
"</html>";
@@ -74,78 +72,6 @@ const char* updateContent = "<!doctype html>"
"</body>"
"</html>";
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);

48
cdn/relais.js Normal file
View File

@@ -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();
});