2017-11-27 21:13:38 +00:00
|
|
|
#include <Homie.h>
|
|
|
|
#include <ArduinoOTA.h>
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <SoftwareSerial.h>
|
|
|
|
|
|
|
|
// Timeout, wann die Verbindung als "zurückgesetzt" angesehen werden soll und wieder auf das erste Byte gewartet wird
|
|
|
|
#define TIMEOUT 500
|
|
|
|
|
|
|
|
#define FW_NAME "esp-videoswitcher"
|
|
|
|
#define FW_VERSION "1.0.0"
|
|
|
|
|
|
|
|
SoftwareSerial swSer(D2, D1);
|
|
|
|
int currentnumber = 0;
|
2017-11-27 22:08:41 +00:00
|
|
|
int inputbytes[4];
|
2017-11-28 10:28:46 +00:00
|
|
|
int commandbytes[4];
|
2017-11-27 22:08:41 +00:00
|
|
|
String output = "";
|
|
|
|
String output2 = "";
|
2017-11-27 21:13:38 +00:00
|
|
|
|
|
|
|
HomieNode switchNode("switch", "switch");
|
|
|
|
|
|
|
|
bool switchHandler(const HomieRange& range, const String& value) {
|
|
|
|
Homie.getLogger() << "switch " << ": " << value << endl;
|
2017-11-28 10:28:46 +00:00
|
|
|
|
|
|
|
if (sscanf(value, "%d %d %d %d", &commandbytes[0], &commandbytes[1], &commandbytes[2], &commandbytes[3]) == 4)
|
|
|
|
{
|
|
|
|
// Nur wenn wirklich 4 Werte übernommen wurden
|
|
|
|
switchNode.setProperty("switch").send(value);
|
|
|
|
// Sende an das Gerät
|
|
|
|
swSer.write(commandbytes[0]);
|
|
|
|
swSer.write(commandbytes[1]);
|
|
|
|
swSer.write(commandbytes[2]);
|
|
|
|
swSer.write(commandbytes[3]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
commandbytes[0] = 0;
|
|
|
|
commandbytes[1] = 0;
|
|
|
|
commandbytes[2] = 0;
|
|
|
|
commandbytes[3] = 0;
|
|
|
|
switchNode.setProperty("switch").send("ERROR");
|
|
|
|
}
|
2017-11-27 21:13:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
|
|
|
swSer.begin(9600);
|
|
|
|
Serial.println("\nKramer VS-162AV");
|
|
|
|
|
|
|
|
Homie_setFirmware(FW_NAME, FW_VERSION);
|
|
|
|
Homie_setBrand(FW_NAME);
|
|
|
|
|
|
|
|
switchNode.advertise("switch").settable(switchHandler);
|
|
|
|
|
|
|
|
Homie.setup();
|
|
|
|
|
|
|
|
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
|
|
|
|
ArduinoOTA.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
Homie.loop();
|
|
|
|
ArduinoOTA.handle();
|
|
|
|
|
|
|
|
while (swSer.available() > 0) {
|
|
|
|
int r1 = swSer.read();
|
2017-11-27 22:08:41 +00:00
|
|
|
Serial.print(currentnumber);
|
|
|
|
Serial.print(": ");
|
2017-11-27 21:13:38 +00:00
|
|
|
Serial.print(r1);
|
|
|
|
Serial.print(" ");
|
2017-11-27 22:08:41 +00:00
|
|
|
inputbytes[currentnumber] = r1;
|
|
|
|
|
|
|
|
output += String(r1, DEC);
|
|
|
|
output += " ";
|
|
|
|
if (currentnumber == 3) {
|
|
|
|
switchNode.setProperty("data").send(output);
|
|
|
|
output2 = "";
|
|
|
|
output2 += "OUTPUT ";
|
|
|
|
output2 += String(inputbytes[2]-128, DEC);
|
|
|
|
output2 += " INPUT ";
|
|
|
|
output2 += String(inputbytes[1]-128, DEC);
|
|
|
|
switchNode.setProperty("switchevent").send(output2);
|
|
|
|
switchNode.setProperty("input").send(String(inputbytes[1]-128, DEC));
|
|
|
|
switchNode.setProperty("output").send(String(inputbytes[2]-128, DEC));
|
2017-11-28 10:28:46 +00:00
|
|
|
|
2017-11-27 22:08:41 +00:00
|
|
|
currentnumber = 0;
|
|
|
|
inputbytes[0] = 0;
|
|
|
|
inputbytes[1] = 0;
|
|
|
|
inputbytes[2] = 0;
|
|
|
|
inputbytes[3] = 0;
|
|
|
|
output = "";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Serial.print(">");
|
|
|
|
Serial.print(currentnumber);
|
|
|
|
Serial.print("<");
|
|
|
|
currentnumber++;
|
|
|
|
}
|
2017-11-27 21:13:38 +00:00
|
|
|
}
|
|
|
|
}
|