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.
31#elif defined(RF24_WIRINGPI)
37RF24 radio(CE_PIN, CSN_PIN);
48unsigned int counter = 0;
49void makePayload(uint8_t);
53void printHelp(
string);
56struct timespec startTimer, endTimer;
59int main(
int argc,
char** argv)
64 cout <<
"radio hardware is not responding!!" << endl;
72 uint8_t address[2][6] = {
"1Node",
"2Node"};
81 bool foundArgNode =
false;
82 bool foundArgRole =
false;
86 if ((argc - 1) % 2 != 0) {
88 printHelp(
string(argv[0]));
95 bool invalidOption =
false;
96 if (strcmp(argv[a],
"-n") == 0 || strcmp(argv[a],
"--node") == 0) {
99 if (argv[a + 1][0] - 48 <= 1) {
100 radioNumber = (argv[a + 1][0] - 48) == 1;
104 invalidOption =
true;
107 else if (strcmp(argv[a],
"-r") == 0 || strcmp(argv[a],
"--role") == 0) {
110 if (argv[a + 1][0] - 48 <= 1) {
111 role = (argv[a + 1][0] - 48) == 1;
115 invalidOption =
true;
119 printHelp(
string(argv[0]));
124 if (!foundArgNode && !foundArgRole) {
126 printHelp(
string(argv[0]));
133 cout << argv[0] << endl;
137 cout <<
"Which radio is this? Enter '0' or '1'. Defaults to '0' ";
140 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
145 radio.setPayloadSize(SIZE);
153 radio.openWritingPipe(address[radioNumber]);
156 radio.openReadingPipe(1, address[!radioNumber]);
167 role ? master() : slave();
179 while (!input.length()) {
180 cout <<
"*** PRESS 'T' to begin transmitting to the other node\n";
181 cout <<
"*** PRESS 'R' to begin receiving from the other node\n";
182 cout <<
"*** PRESS 'Q' to exit" << endl;
184 if (input.length() >= 1) {
185 if (input[0] ==
'T' || input[0] ==
't')
187 else if (input[0] ==
'R' || input[0] ==
'r')
189 else if (input[0] ==
'Q' || input[0] ==
'q')
192 cout << input[0] <<
" is an invalid input. Please try again." << endl;
203 radio.stopListening();
205 unsigned int failures = 0;
207 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
210 if (!radio.writeFast(&buffer, SIZE)) {
218 if (failures >= 100) {
220 cout <<
"Too many failures detected. ";
221 cout <<
"Aborting at payload " << buffer[0];
226 uint32_t elapsedTime = getMicros();
227 cout <<
"Time to transmit data = ";
229 cout <<
" us. " << failures;
230 cout <<
" failures detected. Leaving TX role." << endl;
240 radio.startListening();
241 time_t startTimer = time(
nullptr);
242 while (time(
nullptr) - startTimer < 6) {
243 if (radio.available()) {
244 radio.read(&buffer, SIZE);
245 cout <<
"Received: " << buffer;
246 cout <<
" - " << counter << endl;
248 startTimer = time(
nullptr);
251 radio.stopListening();
253 cout <<
"Nothing received in 6 seconds. Leaving RX role." << endl;
260void makePayload(uint8_t i)
265 buffer[0] = i + (i < 26 ? 65 : 71);
266 for (uint8_t j = 0; j < SIZE - 1; ++j) {
267 char chr = j >= (SIZE - 1) / 2 + abs((SIZE - 1) / 2 - i);
268 chr |= j < (SIZE - 1) / 2 - abs((SIZE - 1) / 2 - i);
269 buffer[j + 1] = chr + 48;
281 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
282 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
283 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
285 return ((seconds)*1000 + useconds) + 0.5;
291void printHelp(
string progName)
293 cout <<
"usage: " << progName <<
" [-h] [-n {0,1}] [-r {0,1}]\n\n"
294 <<
"A simple example of streaming data from 1 nRF24L01 transceiver to another.\n"
295 <<
"\nThis example was written to be used on 2 devices acting as 'nodes'.\n"
296 <<
"\noptional arguments:\n -h, --help\t\tshow this help message and exit\n"
297 <<
" -n {0,1}, --node {0,1}\n\t\t\tthe identifying radio number\n"
298 <<
" -r {0,1}, --role {0,1}\n\t\t\t'1' specifies the TX role."
299 <<
" '0' specifies the RX role." << endl;
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.