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.
39RF24 radio(CE_PIN, CSN_PIN);
61struct timespec startTimer, endTimer;
64int main(
int argc,
char** argv)
69 cout <<
"radio hardware is not responding!!" << endl;
74 payload.message[6] = 0;
77 uint8_t address[2][6] = {
"1Node",
"2Node"};
87 cout << argv[0] << endl;
90 cout <<
"Which radio is this? Enter '0' or '1'. Defaults to '0' ";
93 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
102 radio.setPayloadSize(
sizeof(payload));
105 radio.openWritingPipe(address[radioNumber]);
108 radio.openReadingPipe(1, address[!radioNumber]);
126 while (!input.length()) {
127 cout <<
"*** PRESS 'T' to begin transmitting to the other node\n";
128 cout <<
"*** PRESS 'R' to begin receiving from the other node\n";
129 cout <<
"*** PRESS 'Q' to exit" << endl;
131 if (input.length() >= 1) {
132 if (input[0] ==
'T' || input[0] ==
't')
134 else if (input[0] ==
'R' || input[0] ==
'r')
136 else if (input[0] ==
'Q' || input[0] ==
'q')
139 cout << input[0] <<
" is an invalid input. Please try again." << endl;
151 memcpy(payload.message,
"Hello ", 6);
152 radio.stopListening();
154 unsigned int failures = 0;
155 while (failures < 6) {
156 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
157 bool report = radio.write(&payload,
sizeof(payload));
162 radio.startListening();
163 unsigned long start_timeout =
millis();
164 while (!radio.available()) {
165 if (
millis() - start_timeout > 200)
168 unsigned long elapsedTime = getMicros();
169 radio.stopListening();
173 cout <<
"Transmission successful! ";
174 if (radio.available(&pipe)) {
175 uint8_t bytes = radio.getPayloadSize();
176 cout <<
"Round trip delay = ";
178 cout <<
" us. Sent: " << payload.message;
179 cout << (
unsigned int)payload.counter;
180 PayloadStruct received;
181 radio.read(&received,
sizeof(received));
182 cout <<
" Recieved " << (
unsigned int)bytes;
183 cout <<
" on pipe " << (
unsigned int)pipe;
184 cout <<
": " << received.message;
185 cout << (
unsigned int)received.counter;
187 payload.counter = received.counter;
190 cout <<
"Recieved no response." << endl;
194 cout <<
"Transmission failed or timed out";
203 cout << failures <<
" failures detected. Leaving TX role." << endl;
211 memcpy(payload.message,
"World ", 6);
212 radio.startListening();
214 time_t startTimer = time(
nullptr);
215 while (time(
nullptr) - startTimer < 6) {
217 if (radio.available(&pipe)) {
218 uint8_t bytes = radio.getPayloadSize();
219 PayloadStruct received;
220 radio.read(&received,
sizeof(received));
221 payload.counter = received.counter + 1;
224 radio.stopListening();
225 radio.writeFast(&payload,
sizeof(payload));
226 bool report = radio.txStandBy(150);
227 radio.startListening();
230 cout <<
"Received " << (
unsigned int)bytes;
231 cout <<
" bytes on pipe ";
232 cout << (
unsigned int)pipe;
233 cout <<
": " << received.message;
234 cout << (
unsigned int)received.counter;
237 cout <<
" Sent: " << payload.message;
238 cout << (
unsigned int)payload.counter;
242 cout <<
" Response failed to send." << endl;
244 startTimer = time(
nullptr);
248 cout <<
"Nothing received in 6 seconds. Leaving RX role." << endl;
249 radio.stopListening();
260 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
261 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
262 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
264 return ((seconds)*1000 + useconds) + 0.5;
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.