esp-wemos-audioswitcher/firmware_esp/firmware_esp.ino

101 lines
2.3 KiB
C++

#include <Homie.h>
#include <ArduinoOTA.h>
#define ROOM_NUMBERS 5
// Serial Protocol:
// 9600 Baud
// 1 char: room number 1-5
// 2 char: command v (volume) / r (relais)
// relais command:
// 3 char: a or b
// volume command:
// 3-5 char: 0-100 volume
long lastTickMillis;
uint8_t volume[ROOM_NUMBERS], volumeLast[ROOM_NUMBERS];
String relais[ROOM_NUMBERS], relaisLast[ROOM_NUMBERS];
HomieNode volumeNode("volume", "volume");
HomieNode switchNode("switches", "switch");
bool nodeInputHandlerVolume(const HomieRange& range, const String& value) {
int vol = value.toInt();
if(range.index >= 1 && range.index <= ROOM_NUMBERS && vol >= 0 && vol <= 100) {
volumeNode.setProperty("volume").setRange(range).send(value);
volume[range.index-1] = vol;
return true;
}
return false;
}
bool nodeInputHandlerRelais(const HomieRange& range, const String& value) {
if(range.index >= 1 && range.index <= 5 && (value == "a" || value == "b")) {
switchNode.setProperty("switch").setRange(range).send(value);
relais[range.index-1] = value;
return true;
}
return false;
}
void setup() {
Serial.begin(9600);
Homie_setFirmware("audiocontroller", "1.0.0");
Homie.disableLogging();
volumeNode.advertiseRange("volume", 1, ROOM_NUMBERS).settable(nodeInputHandlerVolume);
switchNode.advertiseRange("switch", 1, ROOM_NUMBERS).settable(nodeInputHandlerRelais);
Homie.setup();
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
volume[0]=35;
volume[1]=35;
volume[2]=35;
volume[3]=35;
volume[4]=35;
}
void loop() {
Homie.loop();
ArduinoOTA.handle();
// wait for at least 200ms before sending new command because
// the arduino mini has a 100ms serial timeout
for(uint8_t i = 0; i < ROOM_NUMBERS; i++) {
if(volume[i] != volumeLast[i]) {
if(millis() - lastTickMillis >= 400) {
Serial.print(i+1);
Serial.print("v");
Serial.print(volume[i]);
volumeLast[i] = volume[i];
lastTickMillis = millis();
}
}
if(relais[i] != relaisLast[i]) {
if(millis() - lastTickMillis >= 400) {
Serial.print(i+1);
Serial.print("r");
Serial.print(relais[i]);
relaisLast[i] = relais[i];
lastTickMillis = millis();
}
}
}
}