Written by 2bndy5 in 2020
A simple example of sending data from 1 nRF24L01 transceiver to another.
This example was written * This example was written to be used on up to 6 devices acting as TX nodes & only 1 device acting as the RX node (that's a maximum of 7 devices). acting as "nodes". Use ctrl+c
to quit at any time.
29#elif defined(RF24_WIRINGPI)
35RF24 radio(CE_PIN, CSN_PIN);
51struct timespec startTimer, endTimer;
54int main(
int argc,
char** argv)
59 cout <<
"radio hardware is not responding!!" << endl;
68 cout << argv[0] << endl;
71 uint8_t address[2][6] = {
"1Node",
"2Node"};
76 cout <<
"Which radio is this? Enter '0' or '1'. Defaults to '0' ";
79 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
83 radio.setPayloadSize(
sizeof(payload));
91 radio.openWritingPipe(address[radioNumber]);
94 radio.openReadingPipe(1, address[!radioNumber]);
112 while (!input.length()) {
113 cout <<
"*** PRESS 'T' to begin transmitting to the other node\n";
114 cout <<
"*** PRESS 'R' to begin receiving from the other node\n";
115 cout <<
"*** PRESS 'Q' to exit" << endl;
117 if (input.length() >= 1) {
118 if (input[0] ==
'T' || input[0] ==
't')
120 else if (input[0] ==
'R' || input[0] ==
'r')
122 else if (input[0] ==
'Q' || input[0] ==
'q')
125 cout << input[0] <<
" is an invalid input. Please try again." << endl;
136 radio.stopListening();
138 unsigned int failure = 0;
139 while (failure < 6) {
140 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
141 bool report = radio.write(&payload,
sizeof(
float));
142 uint32_t timerElapsed = getMicros();
146 cout <<
"Transmission successful! Time to transmit = ";
147 cout << timerElapsed;
148 cout <<
" us. Sent: " << payload << endl;
153 cout <<
"Transmission failed or timed out" << endl;
160 cout << failure <<
" failures detected. Leaving TX role." << endl;
169 radio.startListening();
171 time_t startTimer = time(
nullptr);
172 while (time(
nullptr) - startTimer < 6) {
174 if (radio.available(&pipe)) {
175 uint8_t bytes = radio.getPayloadSize();
176 radio.read(&payload, bytes);
177 cout <<
"Received " << (
unsigned int)bytes;
178 cout <<
" bytes on pipe " << (
unsigned int)pipe;
179 cout <<
": " << payload << endl;
180 startTimer = time(
nullptr);
183 cout <<
"Nothing received in 6 seconds. Leaving RX role." << endl;
184 radio.stopListening();
195 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
196 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
197 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
199 return ((seconds)*1000 + useconds) + 0.5;
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.