A simple example of sending data from 1 nRF24L01 transceiver to another.
This example was written to be used on 2 devices acting as "nodes". Use ctrl+c
to quit at any time.
35RF24 radio(CE_PIN, CSN_PIN);
46unsigned int counter = 0;
47void makePayload(uint8_t);
51void printHelp(
string);
54struct timespec startTimer, endTimer;
57int main(
int argc,
char** argv)
62 cout <<
"radio hardware is not responding!!" << endl;
70 uint8_t address[2][6] = {
"1Node",
"2Node"};
79 bool foundArgNode =
false;
80 bool foundArgRole =
false;
84 if ((argc - 1) % 2 != 0) {
86 printHelp(
string(argv[0]));
93 bool invalidOption =
false;
94 if (strcmp(argv[a],
"-n") == 0 || strcmp(argv[a],
"--node") == 0) {
97 if (argv[a + 1][0] - 48 <= 1) {
98 radioNumber = (argv[a + 1][0] - 48) == 1;
102 invalidOption =
true;
105 else if (strcmp(argv[a],
"-r") == 0 || strcmp(argv[a],
"--role") == 0) {
108 if (argv[a + 1][0] - 48 <= 1) {
109 role = (argv[a + 1][0] - 48) == 1;
113 invalidOption =
true;
117 printHelp(
string(argv[0]));
122 if (!foundArgNode && !foundArgRole) {
124 printHelp(
string(argv[0]));
131 cout << argv[0] << endl;
135 cout <<
"Which radio is this? Enter '0' or '1'. Defaults to '0' ";
138 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
143 radio.setPayloadSize(SIZE);
151 radio.openWritingPipe(address[radioNumber]);
154 radio.openReadingPipe(1, address[!radioNumber]);
165 role ? master() : slave();
177 while (!input.length()) {
178 cout <<
"*** PRESS 'T' to begin transmitting to the other node\n";
179 cout <<
"*** PRESS 'R' to begin receiving from the other node\n";
180 cout <<
"*** PRESS 'Q' to exit" << endl;
182 if (input.length() >= 1) {
183 if (input[0] ==
'T' || input[0] ==
't')
185 else if (input[0] ==
'R' || input[0] ==
'r')
187 else if (input[0] ==
'Q' || input[0] ==
'q')
190 cout << input[0] <<
" is an invalid input. Please try again." << endl;
201 radio.stopListening();
203 unsigned int failures = 0;
205 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
208 if (!radio.writeFast(&buffer, SIZE)) {
216 if (failures >= 100) {
218 cout <<
"Too many failures detected. ";
219 cout <<
"Aborting at payload " << buffer[0];
223 uint32_t elapsedTime = getMicros();
224 cout <<
"Time to transmit data = ";
226 cout <<
" us. " << failures;
227 cout <<
" failures detected. Leaving TX role." << endl;
237 radio.startListening();
238 time_t startTimer = time(
nullptr);
239 while (time(
nullptr) - startTimer < 6) {
240 if (radio.available()) {
241 radio.read(&buffer, SIZE);
242 cout <<
"Received: " << buffer;
243 cout <<
" - " << counter << endl;
245 startTimer = time(
nullptr);
248 radio.stopListening();
250 cout <<
"Nothing received in 6 seconds. Leaving RX role." << endl;
257void makePayload(uint8_t i)
262 buffer[0] = i + (i < 26 ? 65 : 71);
263 for (uint8_t j = 0; j < SIZE - 1; ++j) {
264 char chr = j >= (SIZE - 1) / 2 + abs((SIZE - 1) / 2 - i);
265 chr |= j < (SIZE - 1) / 2 - abs((SIZE - 1) / 2 - i);
266 buffer[j + 1] = chr + 48;
278 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
279 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
280 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
282 return ((seconds)*1000 + useconds) + 0.5;
288void printHelp(
string progName)
290 cout <<
"usage: " << progName <<
" [-h] [-n {0,1}] [-r {0,1}]\n\n"
291 <<
"A simple example of streaming data from 1 nRF24L01 transceiver to another.\n"
292 <<
"\nThis example was written to be used on 2 devices acting as 'nodes'.\n"
293 <<
"\noptional arguments:\n -h, --help\t\tshow this help message and exit\n"
294 <<
" -n {0,1}, --node {0,1}\n\t\t\tthe identifying radio number\n"
295 <<
" -r {0,1}, --role {0,1}\n\t\t\t'1' specifies the TX role."
296 <<
" '0' specifies the RX role." << endl;
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.