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 the Serial Monitor to change each node's behavior.
1
2
3
4
5
6
16#include <SPI.h>
19
20#define CE_PIN 7
21#define CSN_PIN 8
22
23RF24 radio(CE_PIN, CSN_PIN);
24
25
26
27
28
29
30
31uint8_t address[6][5] = { { 0x78, 0x78, 0x78, 0x78, 0x78 },
32 { 0xF1, 0xB6, 0xB5, 0xB4, 0xB3 },
33 { 0xCD, 0xB6, 0xB5, 0xB4, 0xB3 },
34 { 0xA3, 0xB6, 0xB5, 0xB4, 0xB3 },
35 { 0x0F, 0xB6, 0xB5, 0xB4, 0xB3 },
36 { 0x05, 0xB6, 0xB5, 0xB4, 0xB3 } };
37
38
39char role = 'R';
40
41
42
43
44
45struct PayloadStruct {
46 unsigned long nodeID;
47 unsigned long payloadID;
48};
49PayloadStruct payload;
50
51
52
53
54void setRole();
55
56void setup() {
57
58 Serial.begin(115200);
59 while (!Serial) {
60
61 }
62
63
64 if (!radio.begin()) {
65 Serial.println(F("radio hardware is not responding!!"));
66 while (1) {}
67 }
68
69
70 Serial.println(F("RF24/examples/MulticeiverDemo"));
71 Serial.println(F("*** Enter a number between 0 and 5 (inclusive) to change"));
72 Serial.println(F(" the identifying node number that transmits."));
73
74
75
76
78
79
80
81 radio.setPayloadSize(sizeof(payload));
82
83
84
85 setRole();
86
87
88
89
90
91
92}
93
94void loop() {
95
96 if (role <= 53) {
97
98
99 unsigned long start_timer = micros();
100 bool report = radio.write(&payload, sizeof(payload));
101 unsigned long end_timer = micros();
102
103 if (report) {
104
105
106 Serial.print(F("Transmission of payloadID "));
107 Serial.print(payload.payloadID);
108 Serial.print(F(" as node "));
109 Serial.print(payload.nodeID);
110 Serial.print(F(" successful!"));
111 Serial.print(F(" Time to transmit: "));
112 Serial.print(end_timer - start_timer);
113 Serial.println(F(" us"));
114 } else {
115 Serial.println(F("Transmission failed or timed out"));
116 }
117 payload.payloadID++;
118
119
121
122 } else if (role == 'R') {
123
124
125 uint8_t pipe;
126 if (radio.available(&pipe)) {
127 uint8_t bytes = radio.getPayloadSize();
128 radio.read(&payload, bytes);
129 Serial.print(F("Received "));
130 Serial.print(bytes);
131 Serial.print(F(" bytes on pipe "));
132 Serial.print(pipe);
133 Serial.print(F(" from node "));
134 Serial.print(payload.nodeID);
135 Serial.print(F(". PayloadID: "));
136 Serial.println(payload.payloadID);
137 }
138 }
139
140 if (Serial.available()) {
141
142
143 char c = Serial.read();
144 if (toupper(c) == 'R' && role <= 53) {
145
146
147 role = 'R';
148 Serial.println(F("*** CHANGING ROLE TO RECEIVER ***"));
149 Serial.println(F("--- Enter a number between 0 and 5 (inclusive) to act as"));
150 Serial.println(F(" a unique node number that transmits to the RX node."));
151 setRole();
152
153 } else if (c >= 48 && c <= 53 && c != role) {
154
155
156 role = c - 48;
157 Serial.print(F("*** CHANGING ROLE TO NODE "));
158 Serial.print(c);
159 Serial.println(F(" ***"));
160 Serial.println(F("--- Enter a number between 0 and 5 (inclusive) to change"));
161 Serial.println(F(" the identifying node number that transmits."));
162 Serial.println(F("--- PRESS 'R' to act as the RX node."));
163 setRole();
164 }
165 }
166
167}
168
169void setRole() {
170 if (role == 'R') {
171
172
173
174 for (uint8_t i = 0; i < 6; ++i)
175 radio.openReadingPipe(i, address[i]);
176
177 radio.startListening();
178
179 } else {
180
181
182
183 payload.nodeID = role;
184 payload.payloadID = 0;
185
186
187 radio.stopListening(address[role]);
188
189
190
191
192 radio.setRetries(((role * 3) % 12) + 3, 15);
193 }
194}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.