A simple example of sending data from 1 nRF24L01 transceiver to another with manually transmitted (non-automatic) Acknowledgement (ACK) payloads. This example still uses ACK packets, but they have no payloads. Instead the acknowledging response is sent with write(). This tactic allows for more updated acknowledgement payload data, where actual ACK payloads' data are outdated by 1 transmission because they have to loaded before receiving a transmission.
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
19#include <SPI.h>
22
23#define CE_PIN 7
24#define CSN_PIN 8
25
26RF24 radio(CE_PIN, CSN_PIN);
27
28
29uint8_t address[][6] = { "1Node", "2Node" };
30
31
32
33
34
35bool radioNumber = 1;
36
37
38bool role = false;
39
40
41
42
43
44struct PayloadStruct {
45 char message[7];
46 uint8_t counter;
47};
48PayloadStruct payload;
49
50void setup() {
51
52
53 payload.message[6] = 0;
54
55 Serial.begin(115200);
56 while (!Serial) {
57
58 }
59
60
61 if (!radio.begin()) {
62 Serial.println(F("radio hardware is not responding!!"));
63 while (1) {}
64 }
65
66
67 Serial.println(F("RF24/examples/ManualAcknowledgements"));
68
69
70 Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
71 while (!Serial.available()) {
72
73 }
74 char input = Serial.parseInt();
75 radioNumber = input == 1;
76 Serial.print(F("radioNumber = "));
77 Serial.println((int)radioNumber);
78
79
80 Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
81
82
83
84
86
87
88
89 radio.setPayloadSize(sizeof(payload));
90
91
92 radio.stopListening(address[radioNumber]);
93
94
95 radio.openReadingPipe(1, address[!radioNumber]);
96
97 if (role) {
98
99
100 memcpy(payload.message, "Hello ", 6);
101 } else {
102
103
104 memcpy(payload.message, "World ", 6);
105 radio.startListening();
106 }
107
108
109
110
111
112
113}
114
115void loop() {
116
117 if (role) {
118
119
120 unsigned long start_timer = micros();
121 bool report = radio.write(&payload, sizeof(payload));
122
123 if (report) {
124
125
126 radio.startListening();
127 unsigned long start_timeout =
millis();
128 while (!radio.available()) {
129 if (
millis() - start_timeout > 200)
130 break;
132 }
133 unsigned long end_timer = micros();
134 radio.stopListening();
135
136
137 Serial.print(F("Transmission successful!"));
138 uint8_t pipe;
139 if (radio.available(&pipe)) {
140 Serial.print(F(" Round-trip delay: "));
141 Serial.print(end_timer - start_timer);
142 Serial.print(F(" us. Sent: "));
143 Serial.print(payload.message);
144 Serial.print(payload.counter);
145 PayloadStruct received;
146 radio.read(&received, sizeof(received));
147 Serial.print(F(" Received "));
148 Serial.print(radio.getPayloadSize());
149 Serial.print(F(" bytes on pipe "));
150 Serial.print(pipe);
151 Serial.print(F(": "));
152 Serial.print(received.message);
153 Serial.println(received.counter);
154 payload.counter = received.counter;
155 } else {
156 Serial.println(F(" Received no response."));
157 }
158 } else {
159 Serial.println(F("Transmission failed or timed out"));
160 }
161
162
164
165 } else {
166
167
168 uint8_t pipe;
169 if (radio.available(&pipe)) {
170 PayloadStruct received;
171 radio.read(&received, sizeof(received));
172 payload.counter = received.counter + 1;
173
174
175 radio.stopListening();
176
177 radio.writeFast(&payload, sizeof(payload));
178 bool report = radio.txStandBy(150);
179
180 radio.startListening();
181
182
183 Serial.print(F("Received "));
184 Serial.print(radio.getPayloadSize());
185 Serial.print(F(" bytes on pipe "));
186 Serial.print(pipe);
187 Serial.print(F(": "));
188 Serial.print(received.message);
189 Serial.print(received.counter);
190
191 if (report) {
192 Serial.print(F(" Sent: "));
193 Serial.print(payload.message);
194 Serial.println(payload.counter);
195 } else {
196 Serial.println(" Response failed.");
197 }
198 }
199 }
200
201 if (Serial.available()) {
202
203
204 char c = toupper(Serial.read());
205 if (c == 'T' && !role) {
206
207
208 role = true;
209 memcpy(payload.message, "Hello ", 6);
210 Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
211 radio.stopListening();
212
213 } else if (c == 'R' && role) {
214
215
216 role = false;
217 memcpy(payload.message, "World ", 6);
218 Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
219 radio.startListening();
220 }
221 }
222}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
#define delayMicroseconds(usec)