diff --git a/WifiLamp.ino b/WifiLamp.ino new file mode 100644 index 0000000..e744316 --- /dev/null +++ b/WifiLamp.ino @@ -0,0 +1,251 @@ +#include +#include +#include +#include + +const char* host = "debug1"; +const char* ssid = "McDonalds Free WiFi 2.4GHz"; +const char* password = "Passwort_123"; + +const int relaisPin = D1; + +const char* success = "success"; + +const char* indexContent = "" + "" + "" + "" + "" + "" + + "Relais Control" + + "" + "" + "" + "" + "" + + "
" + "

Relais Control

" + + "
Initializing...
" + + "" + "" + "
" + + "" + "" + "" + "" + ""; + +const char* updateContent = "" + "" + "" + "" + "" + "" + + "Update" + + "" + "" + "" + "" + "" + + "
" + "

Update

" + + "
" + "" + "" + "
" + "
" + + "" + "" + "" + ""; + +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() { + Serial.begin(115200); + Serial.println(); + + pinMode(relaisPin, OUTPUT); + digitalWrite(relaisPin, HIGH); + + 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); + + Serial.printf("Ready! Open http://%s.local in your browser\n", host); + } else { + for(int i = 0; i < 2; i++) { + digitalWrite(relaisPin, LOW); + delay(500); + digitalWrite(relaisPin, HIGH); + delay(500); + } + } +} + +void loop() { + server.handleClient(); + delay(1); +}