added serial code for communication

This commit is contained in:
Lucas Pleß 2017-06-10 23:53:38 +02:00
parent 9e4a28db2e
commit 4f6f68bc09
2 changed files with 123 additions and 59 deletions

View File

@ -1,18 +1,31 @@
#define PIN_DATA_VOL_A 5
#define PIN_DATA_VOL_B 6
#define PIN_DATA_VOL_C 7
#define PIN_DATA_VOL_D 8
#define PIN_DATA_VOL_A 8
#define PIN_DATA_VOL_B 7
#define PIN_DATA_VOL_C 6
#define PIN_DATA_VOL_D 5
#define PIN_DATA_CLOCK 9
#define PIN_SR_DATA 2
#define PIN_SR_LTCH 3
#define PIN_SR_CLOCK 4
#define PIN_LED 13
// 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 tick;
uint16_t relaisData = 0xffff;
void setup() {
Serial.begin(9600);
Serial.setTimeout(100);
pinMode(PIN_DATA_VOL_A, OUTPUT);
pinMode(PIN_DATA_VOL_B, OUTPUT);
pinMode(PIN_DATA_VOL_C, OUTPUT);
@ -26,12 +39,19 @@ void setup() {
// shift out one time 0xffff to set all pins high. our relais boards
// are active-low
shiftRelais(relaisData);
setVolume(1, 50);
setVolume(2, 50);
setVolume(3, 50);
setVolume(4, 50);
setVolume(5, 50);
}
void shiftRelais(uint16_t data) {
digitalWrite(PIN_SR_LTCH, LOW);
shiftOut(PIN_SR_DATA, PIN_SR_CLOCK, MSBFIRST, (data >> 8));
shiftOut(PIN_SR_DATA, PIN_SR_CLOCK, MSBFIRST, data);
shiftOut(PIN_SR_DATA, PIN_SR_CLOCK, MSBFIRST, data & 0xff);
digitalWrite(PIN_SR_LTCH, HIGH);
}
@ -75,9 +95,9 @@ void switchInputs(uint8_t channel, bool state) {
break;
case 5:
if(state) {
relaisData |= _BV(9) | _BV(8);
relaisData |= (_BV(15) | _BV(14));
} else {
relaisData &= ~(_BV(9) | _BV(8));
relaisData &= ~(_BV(15) | _BV(14));
}
break;
}
@ -119,37 +139,53 @@ void setVolume(uint8_t volume, uint8_t dataPin) {
}
uint8_t volume = 0;
uint8_t room = 0;
bool flag = false;
void loop() {
long currentMillis = millis();
if (Serial.available() > 0) {
String ser = Serial.readString();
if(currentMillis - tick > 500) {
int room = ser.charAt(0) - 48;
setVolume(volume, PIN_DATA_VOL_A);
setVolume(volume, PIN_DATA_VOL_B);
setVolume(volume, PIN_DATA_VOL_C);
setVolume(volume, PIN_DATA_VOL_D);
if(room >= 1 && room <= 5) {
volume++;
if(volume > 100) volume = 0;
switchInputs(room++, flag);
if(room == 5) {
room = 0;
flag = !flag;
}
tick = currentMillis;
}
char command = ser.charAt(1);
if(command == 'v') { //volume
int datapin=0;
switch(room){
case 1:
datapin=PIN_DATA_VOL_A;
break;
case 2:
datapin=PIN_DATA_VOL_B;
break;
case 3:
datapin=PIN_DATA_VOL_C;
break;
case 4:
datapin=PIN_DATA_VOL_D;
break;
}
int vol=ser.substring(2).toInt();
setVolume(vol,datapin);
} else if(command == 'r') { // relais
char state = ser.charAt(2);
if (state=='a') {
switchInputs(room,true);
} else if (state=='b') {
switchInputs(room,false);
}
} // end if command check
} // end if room check
} // end serial available check
}

View File

@ -1,52 +1,61 @@
#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) {
Homie.getLogger() << "VOL " << range.index << " set to " << value << endl;
int r = value.toInt();
if(r >= 0 && r <= 127) {
volumeNode.setProperty("output").setRange(range).send(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;
}
bool nodeInputHandlerVolume(const HomieRange& range, const String& value) {
Homie.getLogger() << "VOL " << range.index << " set to " << value << endl;
switchNode.setProperty("switch").setRange(range).send(value);
return true;
}
void setup() {
Serial.begin(115200);
Serial << endl << endl;
Homie_setFirmware("audiocontroller", "1.0.0");
Serial.begin(9600);
volumeNode.advertiseRange("volume", 1, 4).settable(nodeInputHandlerVolume);
switchNode.advertiseRange("switch", 1, 4).settable(nodeInputHandlerVolume);
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();
Homie.getLogger() << "started" << endl;
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
}
@ -54,14 +63,33 @@ void loop() {
Homie.loop();
ArduinoOTA.handle();
long currentMillis = millis();
// 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(currentMillis - lastTickMillis >= 100) {
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();
}
}
}
lastTickMillis = currentMillis;
}
}