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.
37RF24 radio(CE_PIN, CSN_PIN);
49uint64_t address[6] = {0x7878787878LL,
63 unsigned int payloadID;
68void master(
unsigned int);
70void printHelp(
string);
73struct timespec startTimer, endTimer;
76int main(
int argc,
char** argv)
81 cout <<
"radio hardware is not responding!!" << endl;
87 unsigned int nodeNumber =
'R';
89 bool foundArgNode =
false;
91 if ((argc - 1) != 2) {
94 printHelp(
string(argv[0]));
97 else if (strcmp(argv[1],
"-n") == 0 || strcmp(argv[1],
"--node") == 0) {
100 if ((argv[2][0] - 48) < 6 && (argv[2][0] - 48) >= 0) {
101 nodeNumber = argv[2][0] - 48;
103 else if (argv[2][0] ==
'R' || argv[2][0] ==
'r') {
107 printHelp(
string(argv[0]));
113 printHelp(
string(argv[0]));
119 cout << argv[0] << endl;
128 radio.setPayloadSize(
sizeof(payload));
139 nodeNumber < 6 ? master(nodeNumber) : slave();
152 while (!input.length()) {
153 cout <<
"*** Enter a number between 0 and 5 (inclusive) to act as\n";
154 cout <<
" a unique node number that transmits to the RX node.\n";
155 cout <<
"*** PRESS 'R' to begin receiving from the other nodes\n";
156 cout <<
"*** PRESS 'Q' to exit" << endl;
158 if (input.length() >= 1) {
159 unsigned int toNumber = (
unsigned int)(input[0]) - 48;
160 if (toNumber < 6 && toNumber >= 0)
162 else if (input[0] ==
'R' || input[0] ==
'r')
164 else if (input[0] ==
'Q' || input[0] ==
'q')
167 cout << input[0] <<
" is an invalid input. Please try again." << endl;
176void master(
unsigned int role)
179 payload.nodeID = role;
180 payload.payloadID = 0;
183 radio.stopListening();
184 radio.openWritingPipe(address[role]);
189 radio.setRetries(((role * 3) % 12) + 3, 15);
191 unsigned int failures = 0;
192 while (failures < 6) {
193 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
194 bool report = radio.write(&payload,
sizeof(payload));
195 uint32_t timerElapsed = getMicros();
199 cout <<
"Transmission of PayloadID ";
200 cout << payload.payloadID;
201 cout <<
" as node " << payload.nodeID;
202 cout <<
" successful! Time to transmit = ";
203 cout << timerElapsed <<
" us" << endl;
208 cout <<
"Transmission failed or timed out" << endl;
215 cout << failures <<
" failures detected. Leaving TX role." << endl;
225 for (uint8_t i = 0; i < 6; ++i)
226 radio.openReadingPipe(i, address[i]);
228 radio.startListening();
230 time_t startTimer = time(
nullptr);
231 while (time(
nullptr) - startTimer < 6) {
233 if (radio.available(&pipe)) {
234 uint8_t bytes = radio.getPayloadSize();
235 radio.read(&payload, bytes);
236 cout <<
"Received " << (
unsigned int)bytes;
237 cout <<
" bytes on pipe " << (
unsigned int)pipe;
238 cout <<
" from node " << payload.nodeID;
239 cout <<
". PayloadID: " << payload.payloadID << endl;
240 startTimer = time(
nullptr);
243 cout <<
"Nothing received in 6 seconds. Leaving RX role." << endl;
254 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
255 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
256 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
258 return ((seconds)*1000 + useconds) + 0.5;
264void printHelp(
string progName)
266 cout <<
"usage: " << progName <<
" [-h] [-n {0,1,2,3,4,5,r,R}]\n\n"
267 <<
"A simple example of sending data from as many as 6 nRF24L01 transceivers to\n"
268 <<
"1 receiving transceiver. This technique is trademarked by\n"
269 <<
"Nordic Semiconductors as 'MultiCeiver'.\n"
270 <<
"\nThis example was written to be used on up to 6 devices acting as TX nodes with\n"
271 <<
"another device acting as a RX node (that's a total of 7 devices).\n"
272 <<
"\noptional arguments:\n -h, --help\t\tshow this help message and exit\n"
273 <<
" -n {0,1,2,3,4,5,r,R}, --node {0,1,2,3,4,5,r,R}"
274 <<
"\n\t\t\t0-5 specifies the identifying node ID number for the TX role."
275 <<
"\n\t\t\t'r' or 'R' specifies the RX role." << endl;
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.