A simple example of sending data from 1 nRF24L01 transceiver to another with Acknowledgement (ACK) payloads attached to ACK packets.
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
14#include <ctime>
15#include <iostream>
16#include <string>
17#include <time.h>
19
20using namespace std;
21
22
23
24
25
26
27#define CSN_PIN 0
28#ifdef MRAA
29 #define CE_PIN 15
30#elif defined(RF24_WIRINGPI)
31 #define CE_PIN 3
32#else
33 #define CE_PIN 22
34#endif
35
36RF24 radio(CE_PIN, CSN_PIN);
37
38
39
40
41
42
43
44
45
46struct PayloadStruct
47{
48 char message[7];
49 uint8_t counter;
50};
51PayloadStruct payload;
52
53void setRole();
54void master();
55void slave();
56
57
58struct timespec startTimer, endTimer;
59uint32_t getMicros();
60
61int main(int argc, char** argv)
62{
63
64 if (!radio.begin()) {
65 cout << "radio hardware is not responding!!" << endl;
66 return 0;
67 }
68
69
70 uint8_t address[2][6] = {"1Node", "2Node"};
71
72
73
74
75
76 bool radioNumber = 1;
77
78
79 cout << argv[0] << endl;
80
81
82 cout << "Which radio is this? Enter '0' or '1'. Defaults to '0' ";
83 string input;
84 getline(cin, input);
85 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
86
87
88 radio.enableDynamicPayloads();
89
90
91
92 radio.enableAckPayload();
93
94
95
96
98
99
100 radio.stopListening(address[radioNumber]);
101
102
103 radio.openReadingPipe(1, address[!radioNumber]);
104
105
106
107
108
109
110 setRole();
111 return 0;
112}
113
118void setRole()
119{
120 string input = "";
121 while (!input.length()) {
122 cout << "*** PRESS 'T' to begin transmitting to the other node\n";
123 cout << "*** PRESS 'R' to begin receiving from the other node\n";
124 cout << "*** PRESS 'Q' to exit" << endl;
125 getline(cin, input);
126 if (input.length() >= 1) {
127 if (input[0] == 'T' || input[0] == 't')
128 master();
129 else if (input[0] == 'R' || input[0] == 'r')
130 slave();
131 else if (input[0] == 'Q' || input[0] == 'q')
132 break;
133 else
134 cout << input[0] << " is an invalid input. Please try again." << endl;
135 }
136 input = "";
137 }
138}
139
143void master()
144{
145 memcpy(payload.message, "Hello ", 6);
146 radio.stopListening();
147
148 unsigned int failures = 0;
149 while (failures < 6) {
150 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
151 bool report = radio.write(&payload, sizeof(payload));
152 uint32_t timerElapsed = getMicros();
153
154 if (report) {
155
156 cout << "Transmission successful! Time to transmit = ";
157 cout << timerElapsed;
158 cout << " us. Sent: ";
159 cout << payload.message;
160 cout << (unsigned int)payload.counter;
161
162 uint8_t pipe;
163 if (radio.available(&pipe)) {
164 PayloadStruct received;
165 radio.read(&received, sizeof(received));
166 cout << " Received ";
167 cout << (unsigned int)radio.getDynamicPayloadSize();
168 cout << " bytes on pipe " << (unsigned int)pipe;
169 cout << ": " << received.message;
170 cout << (unsigned int)received.counter << endl;
171 payload.counter = received.counter + 1;
172 }
173 else {
174 cout << " Received an empty ACK packet." << endl;
175 }
176 }
177 else {
178 cout << "Transmission failed or timed out" << endl;
179 failures++;
180 }
181
182
184 }
185 cout << failures << " failures detected. Leaving TX role." << endl;
186}
187
191void slave()
192{
193 memcpy(payload.message, "World ", 6);
194
195
196 radio.writeAckPayload(1, &payload, sizeof(payload));
197
198 radio.startListening();
199 time_t startTimer = time(nullptr);
200 while (time(nullptr) - startTimer < 6) {
201 uint8_t pipe;
202 if (radio.available(&pipe)) {
203 uint8_t bytes = radio.getDynamicPayloadSize();
204 PayloadStruct received;
205 radio.read(&received, sizeof(received));
206 cout << "Received " << (unsigned int)bytes;
207 cout << " bytes on pipe " << (unsigned int)pipe;
208 cout << ": " << received.message;
209 cout << (unsigned int)received.counter;
210 cout << " Sent: ";
211 cout << payload.message;
212 cout << (unsigned int)payload.counter << endl;
213 startTimer = time(nullptr);
214
215
216 payload.counter = received.counter + 1;
217
218 radio.writeAckPayload(1, &payload, sizeof(payload));
219 }
220 }
221 cout << "Nothing received in 6 seconds. Leaving RX role." << endl;
222 radio.stopListening();
223}
224
228uint32_t getMicros()
229{
230
231
232
233 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
234 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
235 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
236
237 return ((seconds)*1000 + useconds) + 0.5;
238}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.