Updated sketch to use new central server
This commit is contained in:
@@ -47,8 +47,7 @@ void RelaisWebserver::handleRequest(HttpClientConnection *connection, const Http
|
|||||||
|
|
||||||
for(auto _client : m_relaisServer->clients())
|
for(auto _client : m_relaisServer->clients())
|
||||||
{
|
{
|
||||||
if(!_client->name().isEmpty() &&
|
if(clientId(_client) == parts.at(2))
|
||||||
_client->name() == parts.at(2))
|
|
||||||
{
|
{
|
||||||
client = _client;
|
client = _client;
|
||||||
break;
|
break;
|
||||||
@@ -79,6 +78,12 @@ void RelaisWebserver::handleRequest(HttpClientConnection *connection, const Http
|
|||||||
redirectRoot(connection, request);
|
redirectRoot(connection, request);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else if(parts.at(3) == "delete")
|
||||||
|
{
|
||||||
|
client->deleteLater();
|
||||||
|
redirectRoot(connection, request);
|
||||||
|
return;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
handle404(connection, request);
|
handle404(connection, request);
|
||||||
@@ -102,24 +107,25 @@ void RelaisWebserver::handleRoot(HttpClientConnection *connection, const HttpReq
|
|||||||
output.append("<th>Name</th>");
|
output.append("<th>Name</th>");
|
||||||
output.append("<th>Status</th>");
|
output.append("<th>Status</th>");
|
||||||
output.append("<th>Actions</th>");
|
output.append("<th>Actions</th>");
|
||||||
|
output.append("<th>Administration</th>");
|
||||||
output.append("</tr>");
|
output.append("</tr>");
|
||||||
output.append("</thead>");
|
output.append("</thead>");
|
||||||
output.append("<tbody>");
|
output.append("<tbody>");
|
||||||
for(auto client : m_relaisServer->clients())
|
for(auto client : m_relaisServer->clients())
|
||||||
{
|
{
|
||||||
output.append("<tr>");
|
output.append("<tr>");
|
||||||
output.append("<td>" % client->peerAddress().toString() % ':' % QString::number(client->peerPort()) % "</td>");
|
output.append("<td>" % clientId(client).toHtmlEscaped() % "</td>");
|
||||||
output.append("<td>" % client->name() % "</td>");
|
output.append("<td>" % client->name().toHtmlEscaped() % "</td>");
|
||||||
output.append("<td>" % client->status() % "</td>");
|
output.append("<td>" % client->status().toHtmlEscaped() % "</td>");
|
||||||
if(!client->name().isEmpty())
|
output.append("<td><a href=\"/devices/" % clientId(client).toHtmlEscaped() % "/toggle\">" % tr("toggle") % "</a> ");
|
||||||
{
|
|
||||||
output.append("<td><a href=\"/devices/" % client->name().toHtmlEscaped() % "/toggle\">" % tr("toggle") % "</a> ");
|
if(client->status() != QStringLiteral("on"))
|
||||||
if(client->status() != QStringLiteral("on"))
|
output.append("<a href=\"/devices/" % clientId(client).toHtmlEscaped() % "/on\">" % tr("on") % "</a> ");
|
||||||
output.append("<a href=\"/devices/" % client->name().toHtmlEscaped() % "/on\">" % tr("on") % "</a> ");
|
|
||||||
if(client->status() != QStringLiteral("off"))
|
if(client->status() != QStringLiteral("off"))
|
||||||
output.append("<a href=\"/devices/" % client->name().toHtmlEscaped() % "/off\">" % tr("off") % "</a> ");
|
output.append("<a href=\"/devices/" % clientId(client).toHtmlEscaped() % "/off\">" % tr("off") % "</a> ");
|
||||||
output.append("</td>");
|
output.append("</td>");
|
||||||
}
|
output.append("<td><a href=\"/devices/" % clientId(client).toHtmlEscaped() % "/delete\">" % tr("Delete") % "</a></td>");
|
||||||
output.append("</tr>");
|
output.append("</tr>");
|
||||||
}
|
}
|
||||||
output.append("</tbody>");
|
output.append("</tbody>");
|
||||||
@@ -159,3 +165,8 @@ void RelaisWebserver::handle404(HttpClientConnection *connection, const HttpRequ
|
|||||||
|
|
||||||
connection->sendResponse(response);
|
connection->sendResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString RelaisWebserver::clientId(RelaisClient *client)
|
||||||
|
{
|
||||||
|
return client->peerAddress().toString() % ':' % QString::number(client->peerPort());
|
||||||
|
}
|
||||||
|
@@ -4,9 +4,10 @@
|
|||||||
|
|
||||||
#include "httpserver.h"
|
#include "httpserver.h"
|
||||||
|
|
||||||
class RelaisServer;
|
|
||||||
class HttpClientConnection;
|
class HttpClientConnection;
|
||||||
class HttpRequest;
|
class HttpRequest;
|
||||||
|
class RelaisClient;
|
||||||
|
class RelaisServer;
|
||||||
|
|
||||||
class RelaisWebserver : public HttpServer
|
class RelaisWebserver : public HttpServer
|
||||||
{
|
{
|
||||||
@@ -22,6 +23,7 @@ private:
|
|||||||
void handleRoot(HttpClientConnection *connection, const HttpRequest &request);
|
void handleRoot(HttpClientConnection *connection, const HttpRequest &request);
|
||||||
void redirectRoot(HttpClientConnection *connection, const HttpRequest &request);
|
void redirectRoot(HttpClientConnection *connection, const HttpRequest &request);
|
||||||
void handle404(HttpClientConnection *connection, const HttpRequest &request);
|
void handle404(HttpClientConnection *connection, const HttpRequest &request);
|
||||||
|
static QString clientId(RelaisClient *client);
|
||||||
|
|
||||||
RelaisServer *m_relaisServer;
|
RelaisServer *m_relaisServer;
|
||||||
};
|
};
|
||||||
|
@@ -4,8 +4,7 @@
|
|||||||
|
|
||||||
const char* ssid = "McDonalds Free WiFi 2.4GHz";
|
const char* ssid = "McDonalds Free WiFi 2.4GHz";
|
||||||
const char* password = "Passwort_123";
|
const char* password = "Passwort_123";
|
||||||
|
const char* host = "192.168.0.243";
|
||||||
const int relaisPin = D1;
|
|
||||||
|
|
||||||
const char* success = "success";
|
const char* success = "success";
|
||||||
|
|
||||||
@@ -72,16 +71,96 @@ const char* updateContent = "<!doctype html>"
|
|||||||
"</body>"
|
"</body>"
|
||||||
"</html>";
|
"</html>";
|
||||||
|
|
||||||
|
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);
|
ESP8266WebServer server(80);
|
||||||
|
ControlClient relaisClient("daniel_decke", D1);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
pinMode(relaisPin, OUTPUT);
|
|
||||||
digitalWrite(relaisPin, HIGH);
|
|
||||||
|
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
relaisClient.begin();
|
||||||
|
|
||||||
server.on("/", HTTP_GET, []() {
|
server.on("/", HTTP_GET, []() {
|
||||||
server.sendHeader("Connection", "close");
|
server.sendHeader("Connection", "close");
|
||||||
@@ -94,21 +173,21 @@ void setup() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
server.on("/on", HTTP_GET, []() {
|
server.on("/on", HTTP_GET, []() {
|
||||||
digitalWrite(relaisPin, HIGH);
|
relaisClient.on();
|
||||||
|
|
||||||
server.sendHeader("Connection", "close");
|
server.sendHeader("Connection", "close");
|
||||||
server.send(200, "text/plain", success);
|
server.send(200, "text/plain", success);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("/off", HTTP_GET, []() {
|
server.on("/off", HTTP_GET, []() {
|
||||||
digitalWrite(relaisPin, LOW);
|
relaisClient.off();
|
||||||
|
|
||||||
server.sendHeader("Connection", "close");
|
server.sendHeader("Connection", "close");
|
||||||
server.send(200, "text/plain", success);
|
server.send(200, "text/plain", success);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on("/toggle", HTTP_GET, []() {
|
server.on("/toggle", HTTP_GET, []() {
|
||||||
digitalWrite(relaisPin, digitalRead(relaisPin) ? LOW : HIGH);
|
relaisClient.toggle();
|
||||||
|
|
||||||
server.sendHeader("Connection", "close");
|
server.sendHeader("Connection", "close");
|
||||||
server.send(200, "text/plain", success);
|
server.send(200, "text/plain", success);
|
||||||
@@ -116,7 +195,7 @@ void setup() {
|
|||||||
|
|
||||||
server.on("/status", HTTP_GET, []() {
|
server.on("/status", HTTP_GET, []() {
|
||||||
server.sendHeader("Connection", "close");
|
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, []() {
|
server.on("/update", HTTP_POST, []() {
|
||||||
@@ -150,18 +229,16 @@ void setup() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
server.begin();
|
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() {
|
void loop() {
|
||||||
server.handleClient();
|
if(WiFi.status() == WL_CONNECTED) {
|
||||||
|
relaisClient.handleClient();
|
||||||
|
server.handleClient();
|
||||||
|
} else {
|
||||||
|
Serial.println("No wifi");
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
|
||||||
delay(1);
|
delay(1);
|
||||||
}
|
}
|
Reference in New Issue
Block a user