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 ctrl+c
to quit at any time.
34RF24 radio(CE_PIN, CSN_PIN);
56struct timespec startTimer, endTimer;
59int main(
int argc,
char** argv)
63 cout <<
"radio hardware is not responding!!" << endl;
68 uint8_t address[2][6] = {
"1Node",
"2Node"};
77 cout << argv[0] << endl;
80 cout <<
"Which radio is this? Enter '0' or '1'. Defaults to '0' ";
83 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
86 radio.enableDynamicPayloads();
90 radio.enableAckPayload();
98 radio.openWritingPipe(address[radioNumber]);
101 radio.openReadingPipe(1, address[!radioNumber]);
119 while (!input.length()) {
120 cout <<
"*** PRESS 'T' to begin transmitting to the other node\n";
121 cout <<
"*** PRESS 'R' to begin receiving from the other node\n";
122 cout <<
"*** PRESS 'Q' to exit" << endl;
124 if (input.length() >= 1) {
125 if (input[0] ==
'T' || input[0] ==
't')
127 else if (input[0] ==
'R' || input[0] ==
'r')
129 else if (input[0] ==
'Q' || input[0] ==
'q')
132 cout << input[0] <<
" is an invalid input. Please try again." << endl;
143 memcpy(payload.message,
"Hello ", 6);
144 radio.stopListening();
146 unsigned int failures = 0;
147 while (failures < 6) {
148 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
149 bool report = radio.write(&payload,
sizeof(payload));
150 uint32_t timerElapsed = getMicros();
154 cout <<
"Transmission successful! Time to transmit = ";
155 cout << timerElapsed;
156 cout <<
" us. Sent: ";
157 cout << payload.message;
158 cout << (
unsigned int)payload.counter;
161 if (radio.available(&pipe)) {
162 PayloadStruct received;
163 radio.read(&received,
sizeof(received));
164 cout <<
" Received ";
165 cout << (
unsigned int)radio.getDynamicPayloadSize();
166 cout <<
" bytes on pipe " << (
unsigned int)pipe;
167 cout <<
": " << received.message;
168 cout << (
unsigned int)received.counter << endl;
169 payload.counter = received.counter + 1;
172 cout <<
" Received an empty ACK packet." << endl;
176 cout <<
"Transmission failed or timed out" << endl;
183 cout << failures <<
" failures detected. Leaving TX role." << endl;
191 memcpy(payload.message,
"World ", 6);
194 radio.writeAckPayload(1, &payload,
sizeof(payload));
196 radio.startListening();
197 time_t startTimer = time(
nullptr);
198 while (time(
nullptr) - startTimer < 6) {
200 if (radio.available(&pipe)) {
201 uint8_t bytes = radio.getDynamicPayloadSize();
202 PayloadStruct received;
203 radio.read(&received,
sizeof(received));
204 cout <<
"Received " << (
unsigned int)bytes;
205 cout <<
" bytes on pipe " << (
unsigned int)pipe;
206 cout <<
": " << received.message;
207 cout << (
unsigned int)received.counter;
209 cout << payload.message;
210 cout << (
unsigned int)payload.counter << endl;
211 startTimer = time(
nullptr);
214 payload.counter = received.counter + 1;
216 radio.writeAckPayload(1, &payload,
sizeof(payload));
219 cout <<
"Nothing received in 6 seconds. Leaving RX role." << endl;
220 radio.stopListening();
231 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
232 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
233 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
235 return ((seconds)*1000 + useconds) + 0.5;
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.