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)
134 unsigned long end_timer = micros();
135 radio.stopListening();
138 Serial.print(F(
"Transmission successful!"));
140 if (radio.available(&pipe)) {
141 Serial.print(F(
" Round-trip delay: "));
142 Serial.print(end_timer - start_timer);
143 Serial.print(F(
" us. Sent: "));
144 Serial.print(payload.message);
145 Serial.print(payload.counter);
146 PayloadStruct received;
147 radio.read(&received,
sizeof(received));
148 Serial.print(F(
" Received "));
149 Serial.print(radio.getPayloadSize());
150 Serial.print(F(
" bytes on pipe "));
152 Serial.print(F(
": "));
153 Serial.print(received.message);
154 Serial.println(received.counter);
155 payload.counter = received.counter;
157 Serial.println(F(
" Received no response."));
160 Serial.println(F(
"Transmission failed or timed out"));
170 if (radio.available(&pipe)) {
171 PayloadStruct received;
172 radio.read(&received,
sizeof(received));
173 payload.counter = received.counter + 1;
176 radio.stopListening();
178 radio.writeFast(&payload,
sizeof(payload));
179 bool report = radio.txStandBy(150);
181 radio.startListening();
184 Serial.print(F(
"Received "));
185 Serial.print(radio.getPayloadSize());
186 Serial.print(F(
" bytes on pipe "));
188 Serial.print(F(
": "));
189 Serial.print(received.message);
190 Serial.print(received.counter);
193 Serial.print(F(
" Sent: "));
194 Serial.print(payload.message);
195 Serial.println(payload.counter);
197 Serial.println(
" Response failed.");
202 if (Serial.available()) {
205 char c = toupper(Serial.read());
206 if (c ==
'T' && !role) {
210 memcpy(payload.message,
"Hello ", 6);
211 Serial.println(F(
"*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
212 radio.stopListening();
214 }
else if (c ==
'R' && role) {
218 memcpy(payload.message,
"World ", 6);
219 Serial.println(F(
"*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
220 radio.startListening();
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
#define delayMicroseconds(usec)