diff --git a/multi_lamp/controlclient.cpp b/multi_lamp/controlclient.cpp new file mode 100644 index 0000000..45babdf --- /dev/null +++ b/multi_lamp/controlclient.cpp @@ -0,0 +1,42 @@ +#include "controlclient.h" + +#include "relais.h" + +ControlClient::ControlClient(const char* host, const int port, const char* name, Relais &relais) : + m_host(host), + m_port(port), + m_name(name), + m_relais(relais) +{ +} + +void ControlClient::begin() +{ +} + +void ControlClient::handleClient() +{ + if(!m_client.connected()) { + if (m_client.connect(m_host, m_port)) { + m_client.println(m_name); + sendStatus(); + } + } + + if(m_client.connected()) { + while(m_client.available()) { + switch(m_client.read()) { + case '1': m_relais.on(); break; + case '0': m_relais.off(); break; + case 't': m_relais.toggle(); break; + case 's': sendStatus(); break; + case 'r': m_client.println("rebooting"); ESP.restart(); break; + } + } + } +} + +void ControlClient::sendStatus() +{ + m_client.println(m_relais.status() ? "on" : "off"); +} diff --git a/multi_lamp/controlclient.h b/multi_lamp/controlclient.h new file mode 100644 index 0000000..e027379 --- /dev/null +++ b/multi_lamp/controlclient.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +class Relais; + +class ControlClient { +public: + explicit ControlClient(const char *host, const int port, const char *name, Relais &relais); + + void begin(); + void handleClient(); + + void sendStatus(); + +private: + WiFiClient m_client; + const char *m_host; + const int m_port; + const char *m_name; + Relais &m_relais; +}; diff --git a/multi_lamp/controlwebserver.cpp b/multi_lamp/controlwebserver.cpp new file mode 100644 index 0000000..6121b84 --- /dev/null +++ b/multi_lamp/controlwebserver.cpp @@ -0,0 +1,97 @@ +#include "controlwebserver.h" + +#include "relais.h" + +const char* ControlWebserver::m_content = + "" + "" + "" + "" + "" + "" + + "Relais Control" + + "" + "" + "" + "" + "" + + "
" + "

Relais Control

" + + "" + "" + "
" + + "" + "" + "" + "" + ""; + +ControlWebserver::ControlWebserver(IPAddress addr, int port) : + UpdateWebserver(addr, port) +{ + init(); +} + +ControlWebserver::ControlWebserver(int port) : + UpdateWebserver(port) +{ + init(); +} + +ControlWebserver::~ControlWebserver() +{ +} + +void ControlWebserver::addRelais0(Relais &relais) +{ + addRelais("/on0", "/off0", "/toggle0", "/status0", relais); +} + +void ControlWebserver::addRelais1(Relais &relais) +{ + addRelais("/on1", "/off1", "/toggle1", "/status1", relais); +} + +void ControlWebserver::addRelais(const char *onUrl, const char *offUrl, const char *toggleUrl, const char *statusUrl, Relais &relais) +{ + on(onUrl, HTTP_GET, [this, &relais]() { + relais.on(); + + sendHeader("Connection", "close"); + send(200, "text/plain", "success"); + }); + + on(offUrl, HTTP_GET, [this, &relais]() { + relais.off(); + + sendHeader("Connection", "close"); + send(200, "text/plain", "success"); + }); + + on(toggleUrl, HTTP_GET, [this, &relais]() { + relais.toggle(); + + sendHeader("Connection", "close"); + send(200, "text/plain", "success"); + }); + + on(statusUrl, HTTP_GET, [this, &relais]() { + sendHeader("Connection", "close"); + send(200, "text/plain", relais.status() ? "on" : "off"); + }); +} + +void ControlWebserver::init() +{ + on("/", HTTP_GET, [this]() { + sendHeader("Connection", "close"); + send(200, "text/html", m_content); + }); +} diff --git a/multi_lamp/controlwebserver.h b/multi_lamp/controlwebserver.h new file mode 100644 index 0000000..7f12b22 --- /dev/null +++ b/multi_lamp/controlwebserver.h @@ -0,0 +1,22 @@ +#pragma once + +#include "updatewebserver.h" + +class Relais; + +class ControlWebserver : public UpdateWebserver +{ + static const char* m_content; + +public: + explicit ControlWebserver(IPAddress addr, int port = 80); + explicit ControlWebserver(int port = 80); + virtual ~ControlWebserver(); + + void addRelais0(Relais &relais); + void addRelais1(Relais &relais); + void addRelais(const char *onUrl, const char *offUrl, const char *toggleUrl, const char *statusUrl, Relais &relais); + +private: + void init(); +}; diff --git a/multi_lamp/multi_lamp.ino b/multi_lamp/multi_lamp.ino index b6b067e..cad20ef 100644 --- a/multi_lamp/multi_lamp.ino +++ b/multi_lamp/multi_lamp.ino @@ -1,289 +1,46 @@ #include #include -#include -const char* ssid = "McDonalds Free WiFi 2.4GHz"; -const char* password = "Passwort_123"; -const char* host = "192.168.0.2"; +#include "relais.h" +#include "controlclient.h" +#include "switchwatcher.h" +#include "controlwebserver.h" -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; - case 'r': m_client.println("rebooting"); ESP.restart(); 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("vorzimmer_decke", D1); -ControlClient relais1Client("wohnzimmer_decke", D2); -bool lastState0, lastState1; +Relais relais0(D1); +Relais relais1(D2); +ControlClient controlClient0("192.168.0.2", 1234, "vorzimmer_decke", relais0); +ControlClient controlClient1("192.168.0.2", 1234, "wohnzimmer_decke", relais1); +SwitchWatcher watcher0(D5, relais0); +SwitchWatcher watcher1(D6, relais1); +ControlWebserver webserver; void setup() { - Serial.begin(115200); - Serial.println(); + WiFi.begin("McDonalds Free WiFi 2.4GHz", "Passwort_123"); + + relais0.begin(); + relais1.begin(); - WiFi.begin(ssid, password); - - relais0Client.begin(); - relais1Client.begin(); - - pinMode(D5, INPUT_PULLUP); - pinMode(D6, INPUT_PULLUP); + controlClient0.begin(); + controlClient1.begin(); - lastState0 = digitalRead(D5) == HIGH; - lastState1 = digitalRead(D6) == HIGH; + watcher0.begin(); + watcher1.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()); + webserver.addRelais0(relais0); + webserver.addRelais1(relais1); - 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(); + webserver.begin(); } void loop() { - if(WiFi.status() == WL_CONNECTED) { - relais0Client.handleClient(); - relais1Client.handleClient(); - server.handleClient(); - } else { - Serial.println("No wifi"); - delay(500); - } - - if(digitalRead(D5) != (lastState0 ? HIGH : LOW)) { - lastState0 = !lastState0; - relais0Client.toggle(); - } - - if(digitalRead(D6) != (lastState1 ? HIGH : LOW)) { - lastState1 = !lastState1; - relais1Client.toggle(); - } + if(WiFi.status() == WL_CONNECTED) { + controlClient0.handleClient(); + controlClient1.handleClient(); + webserver.handleClient(); + } - delay(1); + watcher0.handle(); + watcher1.handle(); + + delay(1); } diff --git a/multi_lamp/relais.cpp b/multi_lamp/relais.cpp new file mode 100644 index 0000000..18f93f0 --- /dev/null +++ b/multi_lamp/relais.cpp @@ -0,0 +1,34 @@ +#include "relais.h" + +#include + +Relais::Relais(const int pin) : + m_pin(pin) +{ +} + +void Relais::begin() +{ + pinMode(m_pin, OUTPUT); + on(); +} + +void Relais::on() +{ + digitalWrite(m_pin, HIGH); +} + +void Relais::off() +{ + digitalWrite(m_pin, LOW); +} + +void Relais::toggle() +{ + digitalWrite(m_pin, status() ? LOW : HIGH); +} + +bool Relais::status() const +{ + return digitalRead(m_pin) == HIGH; +} diff --git a/multi_lamp/relais.h b/multi_lamp/relais.h new file mode 100644 index 0000000..41389e2 --- /dev/null +++ b/multi_lamp/relais.h @@ -0,0 +1,17 @@ +#pragma once + +class Relais +{ +public: + explicit Relais(const int pin); + + void begin(); + + void on(); + void off(); + void toggle(); + bool status() const; + +private: + const int m_pin; +}; diff --git a/multi_lamp/switchwatcher.cpp b/multi_lamp/switchwatcher.cpp new file mode 100644 index 0000000..4e1218c --- /dev/null +++ b/multi_lamp/switchwatcher.cpp @@ -0,0 +1,38 @@ +#include "switchwatcher.h" + +#include + +#include "relais.h" + +SwitchWatcher::SwitchWatcher(const int pin, Relais &relais) : + m_pin(pin), + m_relais(relais) +{ +} + +void SwitchWatcher::begin() +{ + pinMode(m_pin, INPUT_PULLUP); + m_lastState = status(); + m_countdown = 0; +} + +void SwitchWatcher::handle() +{ + if(status() != m_lastState) + { + if(++m_countdown >= 10) + { + m_relais.toggle(); + m_lastState = !m_lastState; + m_countdown = 0; + } + } + else + m_countdown = 0; +} + +bool SwitchWatcher::status() const +{ + return digitalRead(m_pin) == HIGH; +} diff --git a/multi_lamp/switchwatcher.h b/multi_lamp/switchwatcher.h new file mode 100644 index 0000000..7e130e0 --- /dev/null +++ b/multi_lamp/switchwatcher.h @@ -0,0 +1,21 @@ +#pragma once + +class Relais; + +class SwitchWatcher +{ +public: + explicit SwitchWatcher(const int pin, Relais &relais); + + void begin(); + void handle(); + + bool status() const; + +private: + const int m_pin; + Relais &m_relais; + + bool m_lastState; + int m_countdown; +}; diff --git a/multi_lamp/updatewebserver.cpp b/multi_lamp/updatewebserver.cpp new file mode 100644 index 0000000..de4c168 --- /dev/null +++ b/multi_lamp/updatewebserver.cpp @@ -0,0 +1,87 @@ +#include "updatewebserver.h" + +const char* UpdateWebserver::m_content = + "" + "" + "" + "" + "" + "" + + "Update" + + "" + "" + "" + "" + "" + + "
" + "

Update

" + + "
" + "" + "" + "
" + "
" + + "" + "" + "" + ""; + +UpdateWebserver::UpdateWebserver(IPAddress addr, int port) : + ESP8266WebServer(addr, port) +{ + init(); +} + +UpdateWebserver::UpdateWebserver(int port) : + ESP8266WebServer(port) +{ + init(); +} + +UpdateWebserver::~UpdateWebserver() +{ +} + +void UpdateWebserver::init() +{ + on("/update", HTTP_GET, [this]() { + sendHeader("Connection", "close"); + send(200, "text/html", m_content); + }); + + on("/update", HTTP_POST, [this]() { + sendHeader("Connection", "close"); + send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); + ESP.restart(); + }, [this]() { + HTTPUpload& upload = this->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(); + }); +} diff --git a/multi_lamp/updatewebserver.h b/multi_lamp/updatewebserver.h new file mode 100644 index 0000000..c86d462 --- /dev/null +++ b/multi_lamp/updatewebserver.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +class UpdateWebserver : public ESP8266WebServer +{ + static const char* m_content; + +public: + explicit UpdateWebserver(IPAddress addr, int port = 80); + explicit UpdateWebserver(int port = 80); + virtual ~UpdateWebserver(); + +private: + void init(); +}; \ No newline at end of file