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.
33RF24 radio(CE_PIN, CSN_PIN);
49struct timespec startTimer, endTimer;
52int main(
int argc,
char** argv)
57 cout <<
"radio hardware is not responding!!" << endl;
66 cout << argv[0] << endl;
69 uint8_t address[2][6] = {
"1Node",
"2Node"};
74 cout <<
"Which radio is this? Enter '0' or '1'. Defaults to '0' ";
77 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
81 radio.setPayloadSize(
sizeof(payload));
89 radio.openWritingPipe(address[radioNumber]);
92 radio.openReadingPipe(1, address[!radioNumber]);
110 while (!input.length()) {
111 cout <<
"*** PRESS 'T' to begin transmitting to the other node\n";
112 cout <<
"*** PRESS 'R' to begin receiving from the other node\n";
113 cout <<
"*** PRESS 'Q' to exit" << endl;
115 if (input.length() >= 1) {
116 if (input[0] ==
'T' || input[0] ==
't')
118 else if (input[0] ==
'R' || input[0] ==
'r')
120 else if (input[0] ==
'Q' || input[0] ==
'q')
123 cout << input[0] <<
" is an invalid input. Please try again." << endl;
134 radio.stopListening();
136 unsigned int failure = 0;
137 while (failure < 6) {
138 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
139 bool report = radio.write(&payload,
sizeof(
float));
140 uint32_t timerElapsed = getMicros();
144 cout <<
"Transmission successful! Time to transmit = ";
145 cout << timerElapsed;
146 cout <<
" us. Sent: " << payload << endl;
151 cout <<
"Transmission failed or timed out" << endl;
158 cout << failure <<
" failures detected. Leaving TX role." << endl;
167 radio.startListening();
169 time_t startTimer = time(
nullptr);
170 while (time(
nullptr) - startTimer < 6) {
172 if (radio.available(&pipe)) {
173 uint8_t bytes = radio.getPayloadSize();
174 radio.read(&payload, bytes);
175 cout <<
"Received " << (
unsigned int)bytes;
176 cout <<
" bytes on pipe " << (
unsigned int)pipe;
177 cout <<
": " << payload << endl;
178 startTimer = time(
nullptr);
181 cout <<
"Nothing received in 6 seconds. Leaving RX role." << endl;
182 radio.stopListening();
193 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
194 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
195 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
197 return ((seconds)*1000 + useconds) + 0.5;
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.