I am using remote controlled sockets in my home and have a remote connected to my arduino.
When I send SR410# through Serial I want the arduino to send channel ‘4’ and Socket ‘1’ to the remote. ‘mySwitch.switchOff(Ch, Sk);’
But insted of sending (4, 1) it sends 13.
How can I format it so it will send ‘(Ch, Sk)-(4, 1)’ and not (13).
Thanks For any help you can give me.
Here is the snip of code.
void DeviceRemote() {
//Set int from Serial in.
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); //<--Instead of sending (4, 1) it sends (13) to the remote.
Serial.println(Ch, Sk);
LedBlink();
}
if (Vl == '1') {
mySwitch.switchOff(Ch, Sk); //<--Instead of sending (4, 1) it sends (13) to the remote.
Serial.println(Ch, Sk);
LedBlink();
}
}
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();
}
}
If you send, using the Serial Monitor, “SR410#”, then in DeviceRemote(), you are setting Ch, Sk, and Vl to ‘4’, ‘1’, and ‘0’, which, of course, are not the same as 4, 1, and 0.
The mySwitch.switchOff() has two overloads - one takes a string and an int and the other takes two ints. You are calling the two int version, but the data that is in those two ints is not what you think it is.
Try:
int Ch = inputString[2] - '0'; //Channel
int Sk = inputString[3] - '0'; //Socket
int Vl = inputString[4] - '0'; //Value 0 or 1
so that, given “SR410”, Chm Sk, and Vl end up containing 4. 1. and 0, respectively.