esp-wemos-audioswitcher/firmware_esp/firmware_esp.ino

96 lines
2.3 KiB
Arduino
Raw Normal View History

2017-02-21 23:50:55 +00:00
#include <Homie.h>
#include <ArduinoOTA.h>
2017-06-10 21:53:38 +00:00
#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
2017-02-21 23:50:55 +00:00
long lastTickMillis;
2017-06-10 21:53:38 +00:00
uint8_t volume[ROOM_NUMBERS], volumeLast[ROOM_NUMBERS];
String relais[ROOM_NUMBERS], relaisLast[ROOM_NUMBERS];
2017-02-21 23:50:55 +00:00
HomieNode volumeNode("volume", "volume");
HomieNode switchNode("switches", "switch");
bool nodeInputHandlerVolume(const HomieRange& range, const String& value) {
2017-06-10 21:53:38 +00:00
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;
2017-02-21 23:50:55 +00:00
return true;
}
return false;
}
2017-06-10 21:53:38 +00:00
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;
}
2017-02-21 23:50:55 +00:00
2017-06-10 21:53:38 +00:00
return false;
2017-02-21 23:50:55 +00:00
}
void setup() {
2017-06-10 21:53:38 +00:00
Serial.begin(9600);
2017-02-21 23:50:55 +00:00
Homie_setFirmware("audiocontroller", "1.0.0");
2017-06-10 21:53:38 +00:00
Homie.disableLogging();
2017-02-21 23:50:55 +00:00
2017-06-10 21:53:38 +00:00
volumeNode.advertiseRange("volume", 1, ROOM_NUMBERS).settable(nodeInputHandlerVolume);
switchNode.advertiseRange("switch", 1, ROOM_NUMBERS).settable(nodeInputHandlerRelais);
2017-02-21 23:50:55 +00:00
Homie.setup();
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
2017-06-10 21:53:38 +00:00
2017-02-21 23:50:55 +00:00
}
void loop() {
Homie.loop();
ArduinoOTA.handle();
2017-06-10 21:53:38 +00:00
// 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();
}
}
}
2017-02-21 23:50:55 +00:00
2017-06-10 21:53:38 +00:00
2017-02-21 23:50:55 +00:00
}