A simple example of sending data from 1 nRF24L01 transceiver to another with Acknowledgement (ACK) payloads attached to ACK packets.
This example was written to be used on 2 devices acting as "nodes". Use the Serial Monitor to change each node's behavior.
21RF24 radio(CE_PIN, CSN_PIN);
25uint8_t address[][6] = {
"1Node",
"2Node" };
55 Serial.println(F(
"radio hardware is not responding!!"));
60 Serial.println(F(
"RF24/examples/AcknowledgementPayloads"));
63 Serial.println(F(
"Which radio is this? Enter '0' or '1'. Defaults to '0'"));
64 while (!Serial.available()) {
67 char input = Serial.parseInt();
68 radioNumber = input == 1;
69 Serial.print(F(
"radioNumber = "));
70 Serial.println((
int)radioNumber);
73 Serial.println(F(
"*** PRESS 'T' to begin transmitting to the other node"));
81 radio.enableDynamicPayloads();
85 radio.enableAckPayload();
88 radio.openWritingPipe(address[radioNumber]);
91 radio.openReadingPipe(1, address[!radioNumber]);
97 memcpy(payload.message,
"Hello ", 6);
98 radio.stopListening();
102 memcpy(payload.message,
"World ", 6);
104 radio.writeAckPayload(1, &payload,
sizeof(payload));
106 radio.startListening();
120 unsigned long start_timer = micros();
121 bool report = radio.write(&payload,
sizeof(payload));
122 unsigned long end_timer = micros();
125 Serial.print(F(
"Transmission successful! "));
126 Serial.print(F(
"Time to transmit = "));
127 Serial.print(end_timer - start_timer);
128 Serial.print(F(
" us. Sent: "));
129 Serial.print(payload.message);
130 Serial.print(payload.counter);
132 if (radio.available(&pipe)) {
133 PayloadStruct received;
134 radio.read(&received,
sizeof(received));
135 Serial.print(F(
" Received "));
136 Serial.print(radio.getDynamicPayloadSize());
137 Serial.print(F(
" bytes on pipe "));
139 Serial.print(F(
": "));
140 Serial.print(received.message);
141 Serial.println(received.counter);
144 payload.counter = received.counter + 1;
147 Serial.println(F(
" Received: an empty ACK packet"));
152 Serial.println(F(
"Transmission failed or timed out"));
162 if (radio.available(&pipe)) {
163 uint8_t bytes = radio.getDynamicPayloadSize();
164 PayloadStruct received;
165 radio.read(&received,
sizeof(received));
166 Serial.print(F(
"Received "));
168 Serial.print(F(
" bytes on pipe "));
170 Serial.print(F(
": "));
171 Serial.print(received.message);
172 Serial.print(received.counter);
173 Serial.print(F(
" Sent: "));
174 Serial.print(payload.message);
175 Serial.println(payload.counter);
178 payload.counter = received.counter + 1;
180 radio.writeAckPayload(1, &payload,
sizeof(payload));
184 if (Serial.available()) {
187 char c = toupper(Serial.read());
188 if (c ==
'T' && !role) {
192 Serial.println(F(
"*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
194 memcpy(payload.message,
"Hello ", 6);
195 radio.stopListening();
197 }
else if (c ==
'R' && role) {
201 Serial.println(F(
"*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
202 memcpy(payload.message,
"World ", 6);
205 radio.writeAckPayload(1, &payload,
sizeof(payload));
206 radio.startListening();
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.