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 the Serial Monitor to change each node's behavior.
1
2
3
4
5
6
14#include <SPI.h>
17
18#define CE_PIN 7
19#define CSN_PIN 8
20
21RF24 radio(CE_PIN, CSN_PIN);
22
23
24
25uint8_t address[][6] = { "1Node", "2Node" };
26
27
28
29
30
31bool radioNumber = 1;
32
33
34bool role = false;
35
36
37
38
39
40struct PayloadStruct {
41 char message[7];
42 uint8_t counter;
43};
44PayloadStruct payload;
45
46void setup() {
47
48 Serial.begin(115200);
49 while (!Serial) {
50
51 }
52
53
54 if (!radio.begin()) {
55 Serial.println(F("radio hardware is not responding!!"));
56 while (1) {}
57 }
58
59
60 Serial.println(F("RF24/examples/AcknowledgementPayloads"));
61
62
63 Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
64 while (!Serial.available()) {
65
66 }
67 char input = Serial.parseInt();
68 radioNumber = input == 1;
69 Serial.print(F("radioNumber = "));
70 Serial.println((int)radioNumber);
71
72
73 Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
74
75
76
77
79
80
81 radio.enableDynamicPayloads();
82
83
84
85 radio.enableAckPayload();
86
87
88 radio.stopListening(address[radioNumber]);
89
90
91 radio.openReadingPipe(1, address[!radioNumber]);
92
93
94 if (role) {
95
96 memcpy(payload.message, "Hello ", 6);
97 } else {
98
99
100 memcpy(payload.message, "World ", 6);
101
102 radio.writeAckPayload(1, &payload, sizeof(payload));
103
104 radio.startListening();
105 }
106
107
108
109
110
111}
112
113void loop() {
114
115 if (role) {
116
117
118 unsigned long start_timer = micros();
119 bool report = radio.write(&payload, sizeof(payload));
120 unsigned long end_timer = micros();
121
122 if (report) {
123 Serial.print(F("Transmission successful! "));
124 Serial.print(F("Time to transmit = "));
125 Serial.print(end_timer - start_timer);
126 Serial.print(F(" us. Sent: "));
127 Serial.print(payload.message);
128 Serial.print(payload.counter);
129 uint8_t pipe;
130 if (radio.available(&pipe)) {
131 PayloadStruct received;
132 radio.read(&received, sizeof(received));
133 Serial.print(F(" Received "));
134 Serial.print(radio.getDynamicPayloadSize());
135 Serial.print(F(" bytes on pipe "));
136 Serial.print(pipe);
137 Serial.print(F(": "));
138 Serial.print(received.message);
139 Serial.println(received.counter);
140
141
142 payload.counter = received.counter + 1;
143
144 } else {
145 Serial.println(F(" Received: an empty ACK packet"));
146 }
147
148
149 } else {
150 Serial.println(F("Transmission failed or timed out"));
151 }
152
153
155
156 } else {
157
158
159 uint8_t pipe;
160 if (radio.available(&pipe)) {
161 uint8_t bytes = radio.getDynamicPayloadSize();
162 PayloadStruct received;
163 radio.read(&received, sizeof(received));
164 Serial.print(F("Received "));
165 Serial.print(bytes);
166 Serial.print(F(" bytes on pipe "));
167 Serial.print(pipe);
168 Serial.print(F(": "));
169 Serial.print(received.message);
170 Serial.print(received.counter);
171 Serial.print(F(" Sent: "));
172 Serial.print(payload.message);
173 Serial.println(payload.counter);
174
175
176 payload.counter = received.counter + 1;
177
178 radio.writeAckPayload(1, &payload, sizeof(payload));
179 }
180 }
181
182 if (Serial.available()) {
183
184
185 char c = toupper(Serial.read());
186 if (c == 'T' && !role) {
187
188
189 role = true;
190 Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
191
192 memcpy(payload.message, "Hello ", 6);
193 radio.stopListening();
194
195 } else if (c == 'R' && role) {
196
197
198 role = false;
199 Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
200 memcpy(payload.message, "World ", 6);
201
202
203 radio.writeAckPayload(1, &payload, sizeof(payload));
204 radio.startListening();
205 }
206 }
207}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.