#include <ESP8266WiFi.h>
#include <string>
#include <Rotary.h> // Rotary-master
#include <OneButton.h> // OneButton
Rotary encoder = Rotary(D1, D2);
OneButton button(D5, true);
const char* ssid = "sssiddd";
const char* password = "tralyalya";
const char* server = "192.168.11.1";
const int port = 9090;
const char* player = "00:00:00:00:00:1f ";
// Use WiFiClient class to create TCP connections
WiFiClient client;
void connectWifi() {
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void clitx(String cmd) {
Serial.print("Get LMS ststus: ");
Serial.print(player + cmd + "\r\n");
// This will send the request to the server
client.print(player + cmd + "\r\n");
}
void clisubscribe(){
client.connect(server, port);
clitx("subscribe mixer,pause,power"); // подписываемся на оповещение об изменении статуса
delay(100);
clitx("mixer volume ?"); // получаем текущюю
delay(100);
clitx("power ?");
delay(100);
clitx("pause ?");
}
void setup() {
pinMode(D3, INPUT_PULLUP); //enc button
pinMode(D8, OUTPUT); //Red led
pinMode(D6, OUTPUT); //Green led
Serial.begin(115200);
delay(10);
Serial.println("Sync,Sync,Sync,Sync,Sync");
Serial.println ( "Connect to Router requested" );
connectWifi(); // signal WiFi connected
Serial.print("Free Heap[B]: ");
Serial.println(ESP.getFreeHeap());
// give the WiFi a second to initialize:
delay(500);
Serial.print("connecting to ");
Serial.print(server);
Serial.print(":");
Serial.println("9090");
if (!client.connect(server, port)) {
Serial.println("connection failed");
return;
}
Serial.println("connection OK");
delay(10);
// 00:00:00:00:00:1f subscribe mixer,pause,power
clisubscribe();
delay(100);
client.println();
delay(200);
if (client.available()) Serial.println("CLI available...");
digitalWrite(D6, LOW);
digitalWrite(D8, LOW);
} // setup()
void loop() {
while(client.available()){
String clirx = client.readStringUntil('\r\n');
if ( clirx == ("00%3A00%3A00%3A00%3A00%3A1f power 1 \r")) digitalWrite(D8, HIGH);
if ( clirx == ("00%3A00%3A00%3A00%3A00%3A1f power 0 \r")) digitalWrite(D8, LOW);
// тут можно проверки делать еще...
Serial.println("Recieving: " + clirx);
}
String clirx="";
// проверка что есть соединение, если нет, то перезапуск
if (client.connected() != 1 || WiFi.status() != WL_CONNECTED)
{
Serial.print("Reconnect to CLI ");
client.connect(server, port);
while (!client.connect(server, port)) {
delay(500);
Serial.print(".");
}
Serial.println(".");
Serial.println("Reconnect OK.");
//ESP.reset();
clisubscribe();
}
// идем дальше...
}