From a33fdaa645fa7192d2078c1bcad229342de841cd Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE <0xfeedc0de64@gmail.com> Date: Fri, 20 Apr 2018 22:10:32 +0200 Subject: [PATCH] Updated sketch to use new central server --- relaiswebserver.cpp | 39 ++++--- relaiswebserver.h | 4 +- WifiLamp.ino => single_lamp/single_lamp.ino | 115 ++++++++++++++++---- 3 files changed, 124 insertions(+), 34 deletions(-) rename WifiLamp.ino => single_lamp/single_lamp.ino (78%) diff --git a/relaiswebserver.cpp b/relaiswebserver.cpp index 9cab43c..ca685fa 100644 --- a/relaiswebserver.cpp +++ b/relaiswebserver.cpp @@ -47,8 +47,7 @@ void RelaisWebserver::handleRequest(HttpClientConnection *connection, const Http for(auto _client : m_relaisServer->clients()) { - if(!_client->name().isEmpty() && - _client->name() == parts.at(2)) + if(clientId(_client) == parts.at(2)) { client = _client; break; @@ -79,6 +78,12 @@ void RelaisWebserver::handleRequest(HttpClientConnection *connection, const Http redirectRoot(connection, request); return; } + else if(parts.at(3) == "delete") + { + client->deleteLater(); + redirectRoot(connection, request); + return; + } else { handle404(connection, request); @@ -102,24 +107,25 @@ void RelaisWebserver::handleRoot(HttpClientConnection *connection, const HttpReq output.append("Name"); output.append("Status"); output.append("Actions"); + output.append("Administration"); output.append(""); output.append(""); output.append(""); for(auto client : m_relaisServer->clients()) { output.append(""); - output.append("" % client->peerAddress().toString() % ':' % QString::number(client->peerPort()) % ""); - output.append("" % client->name() % ""); - output.append("" % client->status() % ""); - if(!client->name().isEmpty()) - { - output.append("name().toHtmlEscaped() % "/toggle\">" % tr("toggle") % " "); - if(client->status() != QStringLiteral("on")) - output.append("name().toHtmlEscaped() % "/on\">" % tr("on") % " "); - if(client->status() != QStringLiteral("off")) - output.append("name().toHtmlEscaped() % "/off\">" % tr("off") % " "); - output.append(""); - } + output.append("" % clientId(client).toHtmlEscaped() % ""); + output.append("" % client->name().toHtmlEscaped() % ""); + output.append("" % client->status().toHtmlEscaped() % ""); + output.append("" % tr("toggle") % " "); + + if(client->status() != QStringLiteral("on")) + output.append("" % tr("on") % " "); + + if(client->status() != QStringLiteral("off")) + output.append("" % tr("off") % " "); + output.append(""); + output.append("" % tr("Delete") % ""); output.append(""); } output.append(""); @@ -159,3 +165,8 @@ void RelaisWebserver::handle404(HttpClientConnection *connection, const HttpRequ connection->sendResponse(response); } + +QString RelaisWebserver::clientId(RelaisClient *client) +{ + return client->peerAddress().toString() % ':' % QString::number(client->peerPort()); +} diff --git a/relaiswebserver.h b/relaiswebserver.h index ca1d7da..1044498 100644 --- a/relaiswebserver.h +++ b/relaiswebserver.h @@ -4,9 +4,10 @@ #include "httpserver.h" -class RelaisServer; class HttpClientConnection; class HttpRequest; +class RelaisClient; +class RelaisServer; class RelaisWebserver : public HttpServer { @@ -22,6 +23,7 @@ private: void handleRoot(HttpClientConnection *connection, const HttpRequest &request); void redirectRoot(HttpClientConnection *connection, const HttpRequest &request); void handle404(HttpClientConnection *connection, const HttpRequest &request); + static QString clientId(RelaisClient *client); RelaisServer *m_relaisServer; }; diff --git a/WifiLamp.ino b/single_lamp/single_lamp.ino similarity index 78% rename from WifiLamp.ino rename to single_lamp/single_lamp.ino index 4a2e50b..9d01c6c 100644 --- a/WifiLamp.ino +++ b/single_lamp/single_lamp.ino @@ -4,8 +4,7 @@ const char* ssid = "McDonalds Free WiFi 2.4GHz"; const char* password = "Passwort_123"; - -const int relaisPin = D1; +const char* host = "192.168.0.243"; const char* success = "success"; @@ -72,16 +71,96 @@ const char* updateContent = "" "" ""; +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 relaisClient("daniel_decke", D1); void setup() { Serial.begin(115200); Serial.println(); - - pinMode(relaisPin, OUTPUT); - digitalWrite(relaisPin, HIGH); WiFi.begin(ssid, password); + + relaisClient.begin(); server.on("/", HTTP_GET, []() { server.sendHeader("Connection", "close"); @@ -94,21 +173,21 @@ void setup() { }); server.on("/on", HTTP_GET, []() { - digitalWrite(relaisPin, HIGH); + relaisClient.on(); server.sendHeader("Connection", "close"); server.send(200, "text/plain", success); }); server.on("/off", HTTP_GET, []() { - digitalWrite(relaisPin, LOW); + relaisClient.off(); server.sendHeader("Connection", "close"); server.send(200, "text/plain", success); }); server.on("/toggle", HTTP_GET, []() { - digitalWrite(relaisPin, digitalRead(relaisPin) ? LOW : HIGH); + relaisClient.toggle(); server.sendHeader("Connection", "close"); server.send(200, "text/plain", success); @@ -116,7 +195,7 @@ void setup() { server.on("/status", HTTP_GET, []() { server.sendHeader("Connection", "close"); - server.send(200, "text/plain", digitalRead(relaisPin) ? "on" : "off"); + server.send(200, "text/plain", relaisClient.status() ? "on" : "off"); }); server.on("/update", HTTP_POST, []() { @@ -150,18 +229,16 @@ void setup() { }); server.begin(); - - if (WiFi.waitForConnectResult() != WL_CONNECTED) { - for(int i = 0; i < 2; i++) { - digitalWrite(relaisPin, LOW); - delay(500); - digitalWrite(relaisPin, HIGH); - delay(500); - } - } } void loop() { - server.handleClient(); + if(WiFi.status() == WL_CONNECTED) { + relaisClient.handleClient(); + server.handleClient(); + } else { + Serial.println("No wifi"); + delay(500); + } + delay(1); }