Written by 2bndy5 in 2020
A simple example of sending data from 1 nRF24L01 transceiver to another with manually transmitted (non-automatic) Acknowledgement (ACK) payloads. This example still uses ACK packets, but they have no payloads. Instead the acknowledging response is sent with write()
. This tactic allows for more updated acknowledgement payload data, where actual ACK payloads' data are outdated by 1 transmission because they have to loaded before receiving a transmission.
This example was written to be used on 2 devices acting as "nodes". Use the Serial Monitor to change each node's behavior.
26RF24 radio(CE_PIN, CSN_PIN);
29uint8_t address[][6] = {
"1Node",
"2Node" };
53 payload.message[6] = 0;
62 Serial.println(F(
"radio hardware is not responding!!"));
67 Serial.println(F(
"RF24/examples/ManualAcknowledgements"));
70 Serial.println(F(
"Which radio is this? Enter '0' or '1'. Defaults to '0'"));
71 while (!Serial.available()) {
74 char input = Serial.parseInt();
75 radioNumber = input == 1;
76 Serial.print(F(
"radioNumber = "));
77 Serial.println((
int)radioNumber);
80 Serial.println(F(
"*** PRESS 'T' to begin transmitting to the other node"));
89 radio.setPayloadSize(
sizeof(payload));
92 radio.openWritingPipe(address[radioNumber]);
95 radio.openReadingPipe(1, address[!radioNumber]);
100 memcpy(payload.message,
"Hello ", 6);
101 radio.stopListening();
105 memcpy(payload.message,
"World ", 6);
106 radio.startListening();
121 unsigned long start_timer = micros();
122 bool report = radio.write(&payload,
sizeof(payload));
127 radio.startListening();
128 unsigned long start_timeout =
millis();
129 while (!radio.available()) {
130 if (
millis() - start_timeout > 200)
133 unsigned long end_timer = micros();
134 radio.stopListening();
137 Serial.print(F(
"Transmission successful!"));
139 if (radio.available(&pipe)) {
140 Serial.print(F(
" Round-trip delay: "));
141 Serial.print(end_timer - start_timer);
142 Serial.print(F(
" us. Sent: "));
143 Serial.print(payload.message);
144 Serial.print(payload.counter);
145 PayloadStruct received;
146 radio.read(&received,
sizeof(received));
147 Serial.print(F(
" Received "));
148 Serial.print(radio.getPayloadSize());
149 Serial.print(F(
" bytes on pipe "));
151 Serial.print(F(
": "));
152 Serial.print(received.message);
153 Serial.println(received.counter);
154 payload.counter = received.counter;
156 Serial.println(F(
" Recieved no response."));
159 Serial.println(F(
"Transmission failed or timed out"));
169 if (radio.available(&pipe)) {
170 PayloadStruct received;
171 radio.read(&received,
sizeof(received));
172 payload.counter = received.counter + 1;
175 radio.stopListening();
177 radio.writeFast(&payload,
sizeof(payload));
178 bool report = radio.txStandBy(150);
180 radio.startListening();
183 Serial.print(F(
"Received "));
184 Serial.print(radio.getPayloadSize());
185 Serial.print(F(
" bytes on pipe "));
187 Serial.print(F(
": "));
188 Serial.print(received.message);
189 Serial.print(received.counter);
192 Serial.print(F(
" Sent: "));
193 Serial.print(payload.message);
194 Serial.println(payload.counter);
196 Serial.println(
" Response failed.");
201 if (Serial.available()) {
204 char c = toupper(Serial.read());
205 if (c ==
'T' && !role) {
209 memcpy(payload.message,
"Hello ", 6);
210 Serial.println(F(
"*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
211 radio.stopListening();
213 }
else if (c ==
'R' && role) {
217 memcpy(payload.message,
"World ", 6);
218 Serial.println(F(
"*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
219 radio.startListening();
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.