This example demonstrates the use of and extended timeout period and auto-retries/auto-reUse to increase reliability in noisy or low signal scenarios.
Write this sketch to two different nodes. Put one of the nodes into 'transmit' mode by connecting with the serial monitor and sending a 'T'. The data
transfer will begin, with the receiver displaying the payload count and the data transfer rate.
1
2
3
4
5
6
7
8
20
21
22
23#include <SPI.h>
27
28
29
31unsigned long timeoutPeriod = 3000;
32
33
34
35
36const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL };
37
38byte data[32];
39
40volatile unsigned long counter;
41unsigned long rxTimer, startTime, stopTime, payloads = 0;
42bool tx = 1, rx = 0, role = 0, transferInProgress = 0;
43
44
45void setup(void) {
46
47 Serial.begin(115200);
49
50 radio.begin();
51 radio.setChannel(1);
54 radio.setAutoAck(1);
55 radio.setRetries(2, 15);
57 radio.openWritingPipe(pipes[0]);
58 radio.openReadingPipe(1, pipes[1]);
59
60 radio.startListening();
61 radio.printDetails();
62
63 printf("\n\rRF24/examples/Transfer Rates/\n\r");
64 printf("*** PRESS 'T' to begin transmitting to the other node\n\r");
65
66 randomSeed(analogRead(0));
67 for (int i = 0; i < 32; i++) {
68 data[i] = random(255);
69 }
70 radio.powerUp();
71}
72
73
74
75void loop(void) {
76
77
78 if (role == tx) {
80 printf("Initiating Extended Timeout Data Transfer\n\r");
81
82 unsigned long cycles = 1000;
83
84 unsigned long transferCMD[] = { 'H', 'S', cycles };
85 radio.writeFast(&transferCMD, 12);
86 if (radio.txStandBy(timeoutPeriod)) {
87
89 boolean timedOut = 0;
90
91 for (unsigned long i = 0; i < cycles; i++)
92 {
93 data[0] = i;
94
95 if (!radio.writeBlocking(&data, 32, timeoutPeriod)) {
96 timedOut = 1;
97 counter = cycles;
98 break;
99 }
100 }
101
102
104
105
106 if (timedOut) {
107 radio.txStandBy();
108 } else {
109 radio.txStandBy(timeoutPeriod);
110 }
111
112 } else {
113 Serial.println("Communication not established");
114 }
115
116 float rate = cycles * 32 / (stopTime - startTime);
117
118 Serial.print("Transfer complete at ");
119 Serial.print(rate);
120 Serial.println(" KB/s");
121 Serial.print(counter);
122 Serial.print(" of ");
123 Serial.print(cycles);
124 Serial.println(" Packets Failed to Send");
125 counter = 0;
126 }
127
128
129
130 if (role == rx) {
131
132 if (!transferInProgress) {
133 if (radio.available()) {
134 radio.read(&data, 32);
135
136 if (data[0] == 'H' && data[4] == 'S') {
137 payloads = data[8];
138 payloads |= data[9] << 8;
139 counter = 0;
140 transferInProgress = 1;
141 startTime = rxTimer =
millis();
142 }
143 }
144 } else {
145 if (radio.available()) {
146 radio.read(&data, 32);
148 counter++;
149 }
else if (
millis() - rxTimer > timeoutPeriod) {
150 Serial.println("Transfer Failed");
151 transferInProgress = 0;
152 } else if (counter >= payloads) {
153 startTime =
millis() - startTime;
154 float numBytes = counter * 32;
155 Serial.print("Rate: ");
156 Serial.print(numBytes / startTime);
157 Serial.println(" KB/s");
158 Serial.print("Payload Count: ");
159 Serial.println(counter);
160 transferInProgress = 0;
161 }
162 }
163 }
164
165
166
167
168
169 if (Serial.available()) {
170 char c = toupper(Serial.read());
171 if (c == 'T' && role == rx) {
172 printf("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK\n\r");
173 radio.openWritingPipe(pipes[1]);
174 radio.openReadingPipe(1, pipes[0]);
175 radio.stopListening();
176 role = tx;
177 } else if (c == 'R' && role == tx) {
178 radio.openWritingPipe(pipes[0]);
179 radio.openReadingPipe(1, pipes[1]);
180 radio.startListening();
181 printf("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK\n\r");
182 role = rx;
183 }
184 }
185}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.