Written by 2bndy5 in 2020
A simple example of sending data from as many as 6 nRF24L01 transceivers to 1 receiving transceiver. This technique is trademarked by Nordic Semiconductors as "MultiCeiver".
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). Use ctrl+c
to quit at any time.
33#elif defined(RF24_WIRINGPI)
39RF24 radio(CE_PIN, CSN_PIN);
51uint64_t address[6] = {0x7878787878LL,
65 unsigned int payloadID;
70void master(
unsigned int);
72void printHelp(
string);
75struct timespec startTimer, endTimer;
78int main(
int argc,
char** argv)
83 cout <<
"radio hardware is not responding!!" << endl;
89 unsigned int nodeNumber =
'R';
91 bool foundArgNode =
false;
93 if ((argc - 1) != 2) {
96 printHelp(
string(argv[0]));
99 else if (strcmp(argv[1],
"-n") == 0 || strcmp(argv[1],
"--node") == 0) {
102 if ((argv[2][0] - 48) < 6 && (argv[2][0] - 48) >= 0) {
103 nodeNumber = argv[2][0] - 48;
105 else if (argv[2][0] ==
'R' || argv[2][0] ==
'r') {
109 printHelp(
string(argv[0]));
115 printHelp(
string(argv[0]));
121 cout << argv[0] << endl;
130 radio.setPayloadSize(
sizeof(payload));
141 nodeNumber < 6 ? master(nodeNumber) : slave();
154 while (!input.length()) {
155 cout <<
"*** Enter a number between 0 and 5 (inclusive) to act as\n";
156 cout <<
" a unique node number that transmits to the RX node.\n";
157 cout <<
"*** PRESS 'R' to begin receiving from the other nodes\n";
158 cout <<
"*** PRESS 'Q' to exit" << endl;
160 if (input.length() >= 1) {
161 unsigned int toNumber = (
unsigned int)(input[0]) - 48;
162 if (toNumber < 6 && toNumber >= 0)
164 else if (input[0] ==
'R' || input[0] ==
'r')
166 else if (input[0] ==
'Q' || input[0] ==
'q')
169 cout << input[0] <<
" is an invalid input. Please try again." << endl;
178void master(
unsigned int role)
181 payload.nodeID = role;
182 payload.payloadID = 0;
185 radio.stopListening();
186 radio.openWritingPipe(address[role]);
191 radio.setRetries(((role * 3) % 12) + 3, 15);
193 unsigned int failures = 0;
194 while (failures < 6) {
195 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
196 bool report = radio.write(&payload,
sizeof(payload));
197 uint32_t timerElapsed = getMicros();
201 cout <<
"Transmission of PayloadID ";
202 cout << payload.payloadID;
203 cout <<
" as node " << payload.nodeID;
204 cout <<
" successful! Time to transmit = ";
205 cout << timerElapsed <<
" us" << endl;
210 cout <<
"Transmission failed or timed out" << endl;
217 cout << failures <<
" failures detected. Leaving TX role." << endl;
227 for (uint8_t i = 0; i < 6; ++i)
228 radio.openReadingPipe(i, address[i]);
230 radio.startListening();
232 time_t startTimer = time(
nullptr);
233 while (time(
nullptr) - startTimer < 6) {
235 if (radio.available(&pipe)) {
236 uint8_t bytes = radio.getPayloadSize();
237 radio.read(&payload, bytes);
238 cout <<
"Received " << (
unsigned int)bytes;
239 cout <<
" bytes on pipe " << (
unsigned int)pipe;
240 cout <<
" from node " << payload.nodeID;
241 cout <<
". PayloadID: " << payload.payloadID << endl;
242 startTimer = time(
nullptr);
245 cout <<
"Nothing received in 6 seconds. Leaving RX role." << endl;
256 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
257 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
258 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
260 return ((seconds)*1000 + useconds) + 0.5;
266void printHelp(
string progName)
268 cout <<
"usage: " << progName <<
" [-h] [-n {0,1,2,3,4,5,r,R}]\n\n"
269 <<
"A simple example of sending data from as many as 6 nRF24L01 transceivers to\n"
270 <<
"1 receiving transceiver. This technique is trademarked by\n"
271 <<
"Nordic Semiconductors as 'MultiCeiver'.\n"
272 <<
"\nThis example was written to be used on up to 6 devices acting as TX nodes with\n"
273 <<
"another device acting as a RX node (that's a total of 7 devices).\n"
274 <<
"\noptional arguments:\n -h, --help\t\tshow this help message and exit\n"
275 <<
" -n {0,1,2,3,4,5,r,R}, --node {0,1,2,3,4,5,r,R}"
276 <<
"\n\t\t\t0-5 specifies the identifying node ID number for the TX role."
277 <<
"\n\t\t\t'r' or 'R' specifies the RX role." << endl;
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.