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);