A simple example of sending data from 1 nRF24L01 transceiver to another.
This example was written to be used on 2 devices acting as "nodes". Use ctrl+c
to quit at any time.
1
2
3
4
5
6
13#include <cmath>
14#include <ctime>
15#include <cstring>
16#include <iostream>
17#include <string>
18#include <time.h>
20
21using namespace std;
22
23
24
25
26
27
28#define CSN_PIN 0
29#ifdef MRAA
30 #define CE_PIN 15
31#elif defined(RF24_WIRINGPI)
32 #define CE_PIN 3
33#else
34 #define CE_PIN 22
35#endif
36
37RF24 radio(CE_PIN, CSN_PIN);
38
39
40
41
42
43
44
45
46#define SIZE 32
47char buffer[SIZE + 1];
48unsigned int counter = 0;
49void makePayload(uint8_t);
50void setRole();
51void master();
52void slave();
53void printHelp(string);
54
55
56struct timespec startTimer, endTimer;
57uint32_t getMicros();
58
59int main(int argc, char** argv)
60{
61
62
63 if (!radio.begin()) {
64 cout << "radio hardware is not responding!!" << endl;
65 return 0;
66 }
67
68
69 buffer[SIZE] = 0;
70
71
72 uint8_t address[2][6] = {"1Node", "2Node"};
73
74
75
76
77
78
79 bool radioNumber = 1;
80
81 bool foundArgNode = false;
82 bool foundArgRole = false;
83 bool role = false;
84 if (argc > 1) {
85
86 if ((argc - 1) % 2 != 0) {
87
88 printHelp(string(argv[0]));
89 return 0;
90 }
91 else {
92
93 int a = 1;
94 while (a < argc) {
95 bool invalidOption = false;
96 if (strcmp(argv[a], "-n") == 0 || strcmp(argv[a], "--node") == 0) {
97
98 foundArgNode = true;
99 if (argv[a + 1][0] - 48 <= 1) {
100 radioNumber = (argv[a + 1][0] - 48) == 1;
101 }
102 else {
103
104 invalidOption = true;
105 }
106 }
107 else if (strcmp(argv[a], "-r") == 0 || strcmp(argv[a], "--role") == 0) {
108
109 foundArgRole = true;
110 if (argv[a + 1][0] - 48 <= 1) {
111 role = (argv[a + 1][0] - 48) == 1;
112 }
113 else {
114
115 invalidOption = true;
116 }
117 }
118 if (invalidOption) {
119 printHelp(string(argv[0]));
120 return 0;
121 }
122 a += 2;
123 }
124 if (!foundArgNode && !foundArgRole) {
125
126 printHelp(string(argv[0]));
127 return 0;
128 }
129 }
130 }
131
132
133 cout << argv[0] << endl;
134
135 if (!foundArgNode) {
136
137 cout << "Which radio is this? Enter '0' or '1'. Defaults to '0' ";
138 string input;
139 getline(cin, input);
140 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
141 }
142
143
144
145 radio.setPayloadSize(SIZE);
146
147
148
149
151
152
153 radio.stopListening(address[radioNumber]);
154
155
156 radio.openReadingPipe(1, address[!radioNumber]);
157
158
159
160
161
162
163 if (!foundArgRole) {
164 setRole();
165 }
166 else {
167 role ? master() : slave();
168 }
169 return 0;
170}
171
176void setRole()
177{
178 string input = "";
179 while (!input.length()) {
180 cout << "*** PRESS 'T' to begin transmitting to the other node\n";
181 cout << "*** PRESS 'R' to begin receiving from the other node\n";
182 cout << "*** PRESS 'Q' to exit" << endl;
183 getline(cin, input);
184 if (input.length() >= 1) {
185 if (input[0] == 'T' || input[0] == 't')
186 master();
187 else if (input[0] == 'R' || input[0] == 'r')
188 slave();
189 else if (input[0] == 'Q' || input[0] == 'q')
190 break;
191 else
192 cout << input[0] << " is an invalid input. Please try again." << endl;
193 }
194 input = "";
195 }
196}
197
201void master()
202{
203 radio.stopListening();
204
205 unsigned int failures = 0;
206 uint8_t i = 0;
207 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
208 while (i < SIZE) {
209 makePayload(i);
210 if (!radio.writeFast(&buffer, SIZE)) {
211 uint8_t flags = radio.getStatusFlags();
213 failures++;
214
215
219 }
220
221 }
222 else {
223 i++;
224 }
225
226 if (failures >= 100) {
227
228 cout << "Too many failures detected. ";
229 cout << "Aborting at payload " << buffer[0];
230 break;
231 }
232 }
233
234 uint32_t elapsedTime = getMicros();
235 cout << "Time to transmit data = ";
236 cout << elapsedTime;
237 cout << " us. " << failures;
238 cout << " failures detected. Leaving TX role." << endl;
239}
240
244void slave()
245{
246
247 counter = 0;
248 radio.startListening();
249 time_t startTimer = time(nullptr);
250 while (time(nullptr) - startTimer < 6) {
251 if (radio.available()) {
252 counter++;
253 radio.read(&buffer, SIZE);
254 cout << "Received: " << buffer;
255 cout << " - " << counter << endl;
256 startTimer = time(nullptr);
257 }
258 }
259 radio.stopListening();
260
261 cout << "Nothing received in 6 seconds. Leaving RX role." << endl;
262}
263
268void makePayload(uint8_t i)
269{
270
271
272
273 buffer[0] = i + (i < 26 ? 65 : 71);
274 for (uint8_t j = 0; j < SIZE - 1; ++j) {
275 char chr = j >= (SIZE - 1) / 2 + abs((SIZE - 1) / 2 - i);
276 chr |= j < (SIZE - 1) / 2 - abs((SIZE - 1) / 2 - i);
277 buffer[j + 1] = chr + 48;
278 }
279}
280
284uint32_t getMicros()
285{
286
287
288
289 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
290 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
291 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
292
293 return ((seconds)*1000 + useconds) + 0.5;
294}
295
299void printHelp(string progName)
300{
301 cout << "usage: " << progName << " [-h] [-n {0,1}] [-r {0,1}]\n\n"
302 << "A simple example of streaming data from 1 nRF24L01 transceiver to another.\n"
303 << "\nThis example was written to be used on 2 devices acting as 'nodes'.\n"
304 << "\noptional arguments:\n -h, --help\t\tshow this help message and exit\n"
305 << " -n {0,1}, --node {0,1}\n\t\t\tthe identifying radio number\n"
306 << " -r {0,1}, --role {0,1}\n\t\t\t'1' specifies the TX role."
307 << " '0' specifies the RX role." << endl;
308}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
@ RF24_TX_DF
Represents an event where TX Data Failed to send.