The idea is I can set the Channel and Socket and State through Serial without writing multiple lines of (mySwitch.switchOff(4,1) OR mySwitch.switchOn(4,2)) and calling them on if statements.
So if I send SR410#
S=Set / R=Remote / 4=Channel / 1=Socket / 0=Off / #=End of msg.
Or SR321#
Set Remote Channel 3 Socket 2 Turn On.
Here is the Code.
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
String inputString = "";
boolean stringComplete = false;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
inputString.reserve(200);
mySwitch.enableTransmit(9);
}
void loop() {
// print inputString when a # arrives:
if (stringComplete) {
Serial.println(inputString);
// Check first letter for S or G:
if (inputString[0] == 'S') {
ActionSet();
}
if (inputString[0] == 'G') {
ActionGet();
}
// clear the string and reset stringComplete:
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '#') {
stringComplete = true;
}
}
}
void ActionSet() {
if (inputString[1] == 'R') {
DeviceRemote();
}
}
void ActionGet() {
LedBlink();
}
void LedBlink() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
}
void DeviceRemote() {
int Ch = inputString[2]; //Channel
int Sk = inputString[3]; //Socket
int Vl = inputString[4]; //Value 0 or 1 'On or Off'
if (Vl == '0') {
mySwitch.switchOff(Ch, Sk);
Serial.println(inputString);
LedBlink();
}
if (Vl == '1') {
mySwitch.switchOn(Ch, Sk);
Serial.println(inputString);
LedBlink();
}
}