A simple example of streaming data from 1 nRF24L01 transceiver to another.
This example was written to be used on 2 devices acting as "nodes". Use the Serial Monitor to change each node's behavior.
1
2
3
4
5
6
13#include <SPI.h>
16
17#define CE_PIN 7
18#define CSN_PIN 8
19
20RF24 radio(CE_PIN, CSN_PIN);
21
22
23uint8_t address[][6] = { "1Node", "2Node" };
24
25
26
27
28
29bool radioNumber;
30
31
32bool role = false;
33
34
35
36
37#define SIZE 32
38char buffer[SIZE + 1];
39uint8_t counter = 0;
40void makePayload(uint8_t);
41
42
43void setup() {
44
45 buffer[SIZE] = 0;
46
47 Serial.begin(115200);
48 while (!Serial) {
49
50 }
51
52
53 if (!radio.begin()) {
54 Serial.println(F("radio hardware is not responding!!"));
55 while (1) {}
56 }
57
58
59 Serial.println(F("RF24/examples/StreamingData"));
60
61
62 Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
63 while (!Serial.available()) {
64
65 }
66 char input = Serial.parseInt();
67 radioNumber = input == 1;
68 Serial.print(F("radioNumber = "));
69 Serial.println((int)radioNumber);
70
71
72 Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
73
74
75
76
78
79
80
81 radio.setPayloadSize(SIZE);
82
83
84 radio.stopListening(address[radioNumber]);
85
86
87 radio.openReadingPipe(1, address[!radioNumber]);
88
89
90 if (!role) {
91 radio.startListening();
92 }
93
94
95
96
97
98
99}
100
101
102void loop() {
103
104 if (role) {
105
106
107 radio.flush_tx();
108 uint8_t i = 0;
109 uint8_t failures = 0;
110 unsigned long start_timer = micros();
111 while (i < SIZE) {
112 makePayload(i);
113 if (!radio.writeFast(&buffer, SIZE)) {
114 uint8_t flags = radio.getStatusFlags();
116 failures++;
117
121 }
122
123 } else {
124 i++;
125 }
126
127 if (failures >= 100) {
128 Serial.print(F("Too many failures detected. Aborting at payload "));
129 Serial.println(buffer[0]);
130 break;
131 }
132 }
133 unsigned long end_timer = micros();
134
135 Serial.print(F("Time to transmit = "));
136 Serial.print(end_timer - start_timer);
137 Serial.print(F(" us with "));
138 Serial.print(failures);
139 Serial.println(F(" failures detected"));
140
141
143
144 } else {
145
146
147 if (radio.available()) {
148 radio.read(&buffer, SIZE);
149 Serial.print(F("Received: "));
150 Serial.print(buffer);
151 Serial.print(F(" - "));
152 Serial.println(counter++);
153 }
154 }
155
156 if (Serial.available()) {
157
158
159 char c = toupper(Serial.read());
160 if (c == 'T' && !role) {
161
162
163 role = true;
164 counter = 0;
165 Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
166 radio.stopListening();
167
168 } else if (c == 'R' && role) {
169
170
171 role = false;
172 Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
173 radio.startListening();
174 }
175 }
176
177}
178
179
180void makePayload(uint8_t i) {
181
182
183
184
185
186 buffer[0] = i + (i < 26 ? 65 : 71);
187 for (uint8_t j = 0; j < SIZE - 1; ++j) {
188 char chr = j >= (SIZE - 1) / 2 + abs((SIZE - 1) / 2 - i);
189 chr |= j < (SIZE - 1) / 2 - abs((SIZE - 1) / 2 - i);
190 buffer[j + 1] = chr + 48;
191 }
192}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
@ RF24_TX_DF
Represents an event where TX Data Failed to send.