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.
30#elif defined(RF24_WIRINGPI)
36RF24 radio(CE_PIN, CSN_PIN);
58struct timespec startTimer, endTimer;
61int main(
int argc,
char** argv)
65 cout <<
"radio hardware is not responding!!" << endl;
70 uint8_t address[2][6] = {
"1Node",
"2Node"};
79 cout << argv[0] << endl;
82 cout <<
"Which radio is this? Enter '0' or '1'. Defaults to '0' ";
85 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
88 radio.enableDynamicPayloads();
92 radio.enableAckPayload();
100 radio.openWritingPipe(address[radioNumber]);
103 radio.openReadingPipe(1, address[!radioNumber]);
121 while (!input.length()) {
122 cout <<
"*** PRESS 'T' to begin transmitting to the other node\n";
123 cout <<
"*** PRESS 'R' to begin receiving from the other node\n";
124 cout <<
"*** PRESS 'Q' to exit" << endl;
126 if (input.length() >= 1) {
127 if (input[0] ==
'T' || input[0] ==
't')
129 else if (input[0] ==
'R' || input[0] ==
'r')
131 else if (input[0] ==
'Q' || input[0] ==
'q')
134 cout << input[0] <<
" is an invalid input. Please try again." << endl;
145 memcpy(payload.message,
"Hello ", 6);
146 radio.stopListening();
148 unsigned int failures = 0;
149 while (failures < 6) {
150 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
151 bool report = radio.write(&payload,
sizeof(payload));
152 uint32_t timerElapsed = getMicros();
156 cout <<
"Transmission successful! Time to transmit = ";
157 cout << timerElapsed;
158 cout <<
" us. Sent: ";
159 cout << payload.message;
160 cout << (
unsigned int)payload.counter;
163 if (radio.available(&pipe)) {
164 PayloadStruct received;
165 radio.read(&received,
sizeof(received));
166 cout <<
" Received ";
167 cout << (
unsigned int)radio.getDynamicPayloadSize();
168 cout <<
" bytes on pipe " << (
unsigned int)pipe;
169 cout <<
": " << received.message;
170 cout << (
unsigned int)received.counter << endl;
171 payload.counter = received.counter + 1;
174 cout <<
" Received an empty ACK packet." << endl;
178 cout <<
"Transmission failed or timed out" << endl;
185 cout << failures <<
" failures detected. Leaving TX role." << endl;
193 memcpy(payload.message,
"World ", 6);
196 radio.writeAckPayload(1, &payload,
sizeof(payload));
198 radio.startListening();
199 time_t startTimer = time(
nullptr);
200 while (time(
nullptr) - startTimer < 6) {
202 if (radio.available(&pipe)) {
203 uint8_t bytes = radio.getDynamicPayloadSize();
204 PayloadStruct received;
205 radio.read(&received,
sizeof(received));
206 cout <<
"Received " << (
unsigned int)bytes;
207 cout <<
" bytes on pipe " << (
unsigned int)pipe;
208 cout <<
": " << received.message;
209 cout << (
unsigned int)received.counter;
211 cout << payload.message;
212 cout << (
unsigned int)payload.counter << endl;
213 startTimer = time(
nullptr);
216 payload.counter = received.counter + 1;
218 radio.writeAckPayload(1, &payload,
sizeof(payload));
221 cout <<
"Nothing received in 6 seconds. Leaving RX role." << endl;
222 radio.stopListening();
233 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
234 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
235 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
237 return ((seconds)*1000 + useconds) + 0.5;
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.