diff --git a/multi_lamp/multi_lamp.ino b/multi_lamp/multi_lamp.ino new file mode 100644 index 0000000..f3d7eca --- /dev/null +++ b/multi_lamp/multi_lamp.ino @@ -0,0 +1,271 @@ +#include +#include +#include + +const char* ssid = "McDonalds Free WiFi 2.4GHz"; +const char* password = "Passwort_123"; +const char* host = "192.168.0.2"; + +const char* success = "success"; + +const char* indexContent = "" + "" + "" + "" + "" + "" + + "Relais Control" + + "" + "" + "" + "" + "" + + "
" + "

Relais Control

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

Update

" + + "
" + "" + "" + "
" + "
" + + "" + "" + "" + ""; + +class ControlClient { +public: + explicit ControlClient(const char *name, const int pin) : + m_name(name), + m_pin(pin) + { + } + + void begin() { + Serial.println(m_pin); + Serial.println("begin()"); + pinMode(m_pin, OUTPUT); + digitalWrite(m_pin, HIGH); + } + + void handleClient() { + if(!m_client.connected()) { + Serial.println("Connecting to server..."); + + if (m_client.connect(host, 1234)) { + Serial.println("Connected to server!"); + + m_client.println(m_name); + sendStatus(); + } else { + Serial.println("Could not connect to server!"); + } + } + + if(m_client.connected()) { + while(m_client.available()) { + char c(m_client.read()); + Serial.println(c); + switch(c) { + case '1': on(); break; + case '0': off(); break; + case 't': toggle(); break; + case 's': sendStatus(); break; + default: Serial.print("Unknown command: "); Serial.println(c); + } + } + } + } + + void on() { + Serial.println("on()"); + digitalWrite(m_pin, HIGH); + sendStatus(); + } + + void off() { + Serial.println("off()"); + digitalWrite(m_pin, LOW); + sendStatus(); + } + + void toggle() { + Serial.println("toggle()"); + if(status()) { + off(); + } else { + on(); + } + } + + bool status() { + return digitalRead(m_pin) == HIGH; + } + + void sendStatus() { + Serial.println("sendStatus()"); + m_client.println(status() ? "on" : "off"); + } + +private: + WiFiClient m_client; + const char *m_name; + const int m_pin; +}; + +ESP8266WebServer server(80); +ControlClient relais0Client("wohnzimmer_decke", D1); +ControlClient relais1Client("vorzimmer_decke", D2); + +void setup() { + Serial.begin(115200); + Serial.println(); + + WiFi.begin(ssid, password); + + relais0Client.begin(); + relais1Client.begin(); + + 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("/on0", HTTP_GET, []() { + relais0Client.on(); + + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", success); + }); + + server.on("/off0", HTTP_GET, []() { + relais0Client.off(); + + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", success); + }); + + server.on("/on1", HTTP_GET, []() { + relais1Client.on(); + + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", success); + }); + + server.on("/off1", HTTP_GET, []() { + relais1Client.off(); + + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", success); + }); + + server.on("/toggle0", HTTP_GET, []() { + relais0Client.toggle(); + + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", success); + }); + + server.on("/toggle1", HTTP_GET, []() { + relais1Client.toggle(); + + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", success); + }); + + server.on("/status0", HTTP_GET, []() { + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", relais0Client.status() ? "on" : "off"); + }); + + server.on("/status1", HTTP_GET, []() { + server.sendHeader("Connection", "close"); + server.send(200, "text/plain", relais1Client.status() ? "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(); +} + +void loop() { + if(WiFi.status() == WL_CONNECTED) { + relais0Client.handleClient(); + relais1Client.handleClient(); + server.handleClient(); + } else { + Serial.println("No wifi"); + delay(500); + } + + delay(1); +} diff --git a/single_lamp/single_lamp.ino b/single_lamp/single_lamp.ino index 1e97355..898265b 100644 --- a/single_lamp/single_lamp.ino +++ b/single_lamp/single_lamp.ino @@ -14,9 +14,9 @@ const char* indexContent = "" "" "" "" - + "Relais Control" - + "" "" "" @@ -24,16 +24,13 @@ const char* indexContent = "" "" - + "
" "

Relais Control

" - - "
Initializing...
" - - "" - "" + + "" "
" - + "" "" "" @@ -46,9 +43,9 @@ const char* updateContent = "" "" "" "" - + "Update" - + "" "" "" @@ -56,16 +53,16 @@ const char* updateContent = "" "" - + "
" "

Update

" - + "
" "" "" "
" "
" - + "" "" "" @@ -80,7 +77,6 @@ public: } void begin() { - Serial.println(m_pin); Serial.println("begin()"); pinMode(m_pin, OUTPUT); digitalWrite(m_pin, HIGH); @@ -152,7 +148,7 @@ private: }; ESP8266WebServer server(80); -ControlClient relaisClient("daniel_decke", D1); +ControlClient relaisClient("funksteckdose1", D1); void setup() { Serial.begin(115200);