This is an example of how to use payloads of a varying (dynamic) size on Arduino.
24const uint64_t addresses[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
33const short role_pin = 5;
34typedef enum { role_ping_out = 1,
35 role_pong_back } role_e;
36const char* role_friendly_name[] = {
"invalid",
"Ping out",
"Pong back" };
41const int min_payload_size = 4;
42const int max_payload_size = 32;
43const int payload_size_increment = 1;
44int send_payload_size = min_payload_size;
46char receive_payload[max_payload_size + 1];
54 if (digitalRead(role_pin)) {
57 role = role_pong_back;
64 Serial.println(F(
"RF24/examples/pingpair_dyn/"));
65 Serial.print(F(
"ROLE: "));
66 Serial.println(role_friendly_name[role]);
70 radio.enableDynamicPayloads();
71 radio.setRetries(5, 15);
74 if (role == role_ping_out) {
75 radio.openWritingPipe(addresses[0]);
76 radio.openReadingPipe(1, addresses[1]);
78 radio.openWritingPipe(addresses[1]);
79 radio.openReadingPipe(1, addresses[0]);
82 radio.startListening();
91 if (role == role_ping_out) {
93 static char send_payload[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";
95 radio.stopListening();
98 Serial.print(F(
"Now sending length "));
99 Serial.println(send_payload_size);
100 radio.write(send_payload, send_payload_size);
102 radio.startListening();
104 unsigned long started_waiting_at =
millis();
105 bool timeout =
false;
106 while (!radio.available() && !timeout)
108 if (
millis() - started_waiting_at > 500)
114 Serial.println(F(
"Failed, response timed out."));
118 uint8_t len = radio.getDynamicPayloadSize();
124 radio.read(receive_payload, len);
127 receive_payload[len] = 0;
130 Serial.print(F(
"Got response size="));
132 Serial.print(F(
" value="));
133 Serial.println(receive_payload);
136 send_payload_size += payload_size_increment;
137 if (send_payload_size > max_payload_size)
138 send_payload_size = min_payload_size;
147 if (role == role_pong_back) {
148 while (radio.available())
151 uint8_t len = radio.getDynamicPayloadSize();
157 radio.read(receive_payload, len);
160 receive_payload[len] = 0;
163 Serial.print(F(
"Got response size="));
165 Serial.print(F(
" value="));
166 Serial.println(receive_payload);
168 radio.stopListening();
175 radio.write(receive_payload, len);
176 Serial.println(F(
"Sent response."));
178 radio.startListening();
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
#define pinMode(pin, direction)
#define digitalWrite(pin, value)