Implemented tcp client for home automation
This commit is contained in:
@@ -68,6 +68,7 @@ HEADERS += src/pattern.h
|
|||||||
HEADERS += src/ledsettings.h
|
HEADERS += src/ledsettings.h
|
||||||
HEADERS += src/ledcontroller.h
|
HEADERS += src/ledcontroller.h
|
||||||
SOURCES += src/main.cpp
|
SOURCES += src/main.cpp
|
||||||
|
HEADERS += src/controlclient.h
|
||||||
HEADERS += src/patterns/confettipattern.h
|
HEADERS += src/patterns/confettipattern.h
|
||||||
HEADERS += src/patterns/fire2012pattern.h
|
HEADERS += src/patterns/fire2012pattern.h
|
||||||
HEADERS += src/patterns/sineleonpattern.h
|
HEADERS += src/patterns/sineleonpattern.h
|
||||||
|
55
src/controlclient.h
Normal file
55
src/controlclient.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <WiFiClient.h>
|
||||||
|
|
||||||
|
#include <lib8tion.h>
|
||||||
|
|
||||||
|
class ControlClient {
|
||||||
|
public:
|
||||||
|
ControlClient(const char *name, bool &power) :
|
||||||
|
m_name(name),
|
||||||
|
m_power(power)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void handleClient()
|
||||||
|
{
|
||||||
|
if(!m_client.connected())
|
||||||
|
{
|
||||||
|
if (m_client.connect(host, 1234))
|
||||||
|
{
|
||||||
|
m_client.println(m_name);
|
||||||
|
sendStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(m_client.connected())
|
||||||
|
{
|
||||||
|
while(m_client.available())
|
||||||
|
{
|
||||||
|
char c(m_client.read());
|
||||||
|
|
||||||
|
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() { m_power = true; sendStatus(); }
|
||||||
|
void off() { m_power = false; sendStatus(); }
|
||||||
|
void toggle() { m_power = !m_power; sendStatus(); }
|
||||||
|
bool status() { return m_power; }
|
||||||
|
void sendStatus() { m_client.println(status() ? "on" : "off"); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr auto host = "192.168.0.2";
|
||||||
|
|
||||||
|
WiFiClient m_client;
|
||||||
|
const char *m_name;
|
||||||
|
bool &m_power;
|
||||||
|
};
|
10
src/main.cpp
10
src/main.cpp
@@ -7,6 +7,7 @@
|
|||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
|
|
||||||
#include "ledcontroller.h"
|
#include "ledcontroller.h"
|
||||||
|
#include "controlclient.h"
|
||||||
|
|
||||||
constexpr auto WIFI_SSID = "McDonalds Free WiFi 2.4GHz";
|
constexpr auto WIFI_SSID = "McDonalds Free WiFi 2.4GHz";
|
||||||
constexpr auto WIFI_PASSWD = "Passwort_123";
|
constexpr auto WIFI_PASSWD = "Passwort_123";
|
||||||
@@ -14,11 +15,12 @@ constexpr auto WIFI_PASSWD = "Passwort_123";
|
|||||||
LedController ledController;
|
LedController ledController;
|
||||||
|
|
||||||
ESP8266WebServer server(80);
|
ESP8266WebServer server(80);
|
||||||
WiFiClient m_client;
|
|
||||||
|
|
||||||
bool power = true;
|
bool power = true;
|
||||||
bool rotatePattern = true;
|
bool rotatePattern = true;
|
||||||
|
|
||||||
|
ControlClient client("daniel_ledstrip", power);
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@@ -281,7 +283,10 @@ void setup()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
power = *val == "true";
|
if (*val == "true")
|
||||||
|
client.on();
|
||||||
|
else
|
||||||
|
client.off();
|
||||||
|
|
||||||
server.send(200, "text/html", "ok");
|
server.send(200, "text/html", "ok");
|
||||||
});
|
});
|
||||||
@@ -359,6 +364,7 @@ void loop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.handleClient();
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user