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 ctrl+c to quit at any time.
35#elif defined(RF24_WIRINGPI)
41RF24 radio(CE_PIN, CSN_PIN);
63struct timespec startTimer, endTimer;
66int main(
int argc,
char** argv)
71 cout <<
"radio hardware is not responding!!" << endl;
76 payload.message[6] = 0;
79 uint8_t address[2][6] = {
"1Node",
"2Node"};
89 cout << argv[0] << endl;
92 cout <<
"Which radio is this? Enter '0' or '1'. Defaults to '0' ";
95 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
104 radio.setPayloadSize(
sizeof(payload));
107 radio.openWritingPipe(address[radioNumber]);
110 radio.openReadingPipe(1, address[!radioNumber]);
128 while (!input.length()) {
129 cout <<
"*** PRESS 'T' to begin transmitting to the other node\n";
130 cout <<
"*** PRESS 'R' to begin receiving from the other node\n";
131 cout <<
"*** PRESS 'Q' to exit" << endl;
133 if (input.length() >= 1) {
134 if (input[0] ==
'T' || input[0] ==
't')
136 else if (input[0] ==
'R' || input[0] ==
'r')
138 else if (input[0] ==
'Q' || input[0] ==
'q')
141 cout << input[0] <<
" is an invalid input. Please try again." << endl;
153 memcpy(payload.message,
"Hello ", 6);
154 radio.stopListening();
156 unsigned int failures = 0;
157 while (failures < 6) {
158 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
159 bool report = radio.write(&payload,
sizeof(payload));
164 radio.startListening();
165 unsigned long start_timeout =
millis();
166 while (!radio.available()) {
167 if (
millis() - start_timeout > 200)
170 unsigned long elapsedTime = getMicros();
171 radio.stopListening();
175 cout <<
"Transmission successful! ";
176 if (radio.available(&pipe)) {
177 uint8_t bytes = radio.getPayloadSize();
178 cout <<
"Round trip delay = ";
180 cout <<
" us. Sent: " << payload.message;
181 cout << (
unsigned int)payload.counter;
182 PayloadStruct received;
183 radio.read(&received,
sizeof(received));
184 cout <<
" Received " << (
unsigned int)bytes;
185 cout <<
" on pipe " << (
unsigned int)pipe;
186 cout <<
": " << received.message;
187 cout << (
unsigned int)received.counter;
189 payload.counter = received.counter;
192 cout <<
"Received no response." << endl;
196 cout <<
"Transmission failed or timed out";
205 cout << failures <<
" failures detected. Leaving TX role." << endl;
213 memcpy(payload.message,
"World ", 6);
214 radio.startListening();
216 time_t startTimer = time(
nullptr);
217 while (time(
nullptr) - startTimer < 6) {
219 if (radio.available(&pipe)) {
220 uint8_t bytes = radio.getPayloadSize();
221 PayloadStruct received;
222 radio.read(&received,
sizeof(received));
223 payload.counter = received.counter + 1;
226 radio.stopListening();
227 radio.writeFast(&payload,
sizeof(payload));
228 bool report = radio.txStandBy(150);
229 radio.startListening();
232 cout <<
"Received " << (
unsigned int)bytes;
233 cout <<
" bytes on pipe ";
234 cout << (
unsigned int)pipe;
235 cout <<
": " << received.message;
236 cout << (
unsigned int)received.counter;
239 cout <<
" Sent: " << payload.message;
240 cout << (
unsigned int)payload.counter;
244 cout <<
" Response failed to send." << endl;
246 startTimer = time(
nullptr);
250 cout <<
"Nothing received in 6 seconds. Leaving RX role." << endl;
251 radio.stopListening();
262 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
263 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
264 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
266 return ((seconds)*1000 + useconds) + 0.5;
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.