2

I am using the rc-switch library for sending and receiving data. In rcswitch library, there is no method to send string data. I am using below method to send the string message in char buffer.

send_string("Hello World!");
---------------------------

void send_string(const char *str)
{
int i = 0;

for (char *p = str; *p; p++ ) {
mySwitch.send((i<<8) + *p, 16);
i++;
}
mySwitch.send((i<<8) + 255, 16);
}

And I am receiving is using this Serial.print((char)mySwitch.getReceivedValue()); build in method. But I am getting this message.

H
H
H
e
e
e
l
l
l
l
l
l
o
o
o



W
W
W
o
o
o
l
l
l
d
d
d
!
!
!

Now how can I get my original message?

  • stop sending characters multiple times – jsotola May 24 '18 at 4:40
  • I need to send string. How can I do this? – Chayan Mistry May 24 '18 at 4:48
  • check your code and remove the duplicate transmit code .... the problem may also be on the receiving end ... you may be printing each received character three times – jsotola May 24 '18 at 5:03
  • I need something like this stackoverflow.com/a/44749986/5280371 – Chayan Mistry May 24 '18 at 6:55
2

The communication protocol used by this library is unreliable. Thus, each code word is sent multiple times, 10 times by default. One solution around this problem is to send a “byte number” along with each byte you transmit. You would then transmit 16-bit words, where the MSB (most significant byte) is the byte number, and the LSB is the actual byte you want to transmit. Something like this:

int i = 0;
for (char *p = str; *p; p++) {
    mySwitch.send((i<<8) + *p, 16);
    i++;
}

Oh, wait! That's exactly what you are already doing! So it appears that you are already aware of this issue and you have even implemented a solution. How come you are asking this question then?

OK, I'll assume in the following that you just forgot to tells us that the code snippet above is not from you, and that you didn't even understand what it is doing. And you also forgot to provide a link to the source of that code. Please, next time read How do I ask a good question? before asking, and provide all the relevant information right in the question.

So now the problem is in the reading side. You have to use those byte numbers to remove the duplicates. One (untested) solution could look like this:

// Return a received byte, or -1 if none is available.
int receivedValue()
{
    if (!mySwitch.available())  // no byte available
        return -1;
    static uint8_t last_byte_number = 0xff;
    uint16_t code = mySwitch.getReceivedValue();
    uint8_t this_byte_number = code >> 8;
    if (last_byte_number == this_byte_number)  // this is a duplicate
        return -1;
    last_byte_number = this_byte_number;
    return code & 0xff;  // LSB is the byte to return
}

And you could use it to print the data on the serial monitor like this:

int c = receivedValue();
if (c != -1)  // if we really got a new byte
    Serial.write(c);
  • Many thanks. It's working but when I using Serial.print((char)c); it's not working properly. And how I convert it to String would you help, please? – Chayan Mistry May 24 '18 at 9:01
  • @ChayanMistry: Don't use String objects. You can get a C string in the same manner as when reading from a serial port. – Edgar Bonet May 24 '18 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.