In order to send a long payload I've created the following function:
void splitMSG(const char *msg, const int arraySize, const int len)
{
Serial.print("Array size: ");
Serial.println(arraySize);
byte numPackets = (int)(arraySize / len);
byte P_iterator = 0;
if (arraySize % len > 0)
{
numPackets++;
}
Serial.print("num_Packets: ");
Serial.println(numPackets);
while (P_iterator < numPackets)
{
char t[len + 1];
char *ptr1 = msg + P_iterator * (len);
strncpy(t, ptr1, len);
t[len + 1] = '\0';
Serial.println(t);
radio.write(&t, sizeof(t));
P_iterator++;
}
}
The split message is sent to the serial monitor to verify it is as expected.
**On the sending device :
void loop()
{
radio.stopListening();
char send_msg[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // <----- input msg
splitMSG(send_msg, sizeof(send_msg), 10); // <----- split to 10 bytes a packet
delay(1000);
// Serial.println("±±±±±±±±");
}
On the serial monitor I get:
08:06:17.569 -> Array size: 37
08:06:17.569 -> num_Packets: 4
08:06:17.569 -> 1234567890
08:06:17.569 -> ABCDEFGHIJ
08:06:17.569 -> KLMNOPQRST
08:06:17.569 -> UVWXYZ
**On the receiving device:
void loop()
{
if (radio.available())
{
while (radio.available())
{
char buf[32];
radio.read(&buf, sizeof(buf));
Serial.print("got msg: ");
Serial.println(buf);
radio.flush_rx();
}
}
delay(100);
}
On the serial monitor I get:
08:08:18.462 -> got msg: 1234567890⸮⸮⸮⸮
08:08:18.561 -> got msg: ABCDEFGHIJKLMNOPQRSTUVWXYZ
08:08:19.555 -> got msg: 1234567890ABCDEFGHIJKLMNOPQRST⸮⸮⸮⸮
08:08:20.549 -> got msg: 1234567890ABCDEFGHIJKLMNOPQRST⸮⸮⸮⸮
08:08:21.576 -> got msg: 1234567890ABCDEFGHIJKLMNOPQRST⸮⸮⸮⸮
08:08:22.570 -> got msg: 1234567890ABCDEFGHIJKLMNOPQRST⸮⸮⸮⸮
08:08:22.669 -> got msg: UVWXYZ
Does the problem with radio.write
' buffer , or the way I split the message ?