Written by TMRh20
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.
31unsigned long timeoutPeriod = 3000;
36const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL };
40volatile unsigned long counter;
41unsigned long rxTimer, startTime, stopTime, payloads = 0;
42bool tx = 1, rx = 0, role = 0, transferInProgress = 0;
55 radio.setRetries(2, 15);
57 radio.openWritingPipe(pipes[0]);
58 radio.openReadingPipe(1, pipes[1]);
60 radio.startListening();
63 printf(
"\n\rRF24/examples/Transfer Rates/\n\r");
64 printf(
"*** PRESS 'T' to begin transmitting to the other node\n\r");
66 randomSeed(analogRead(0));
67 for (
int i = 0; i < 32; i++) {
68 data[i] = random(255);
80 printf(
"Initiating Extended Timeout Data Transfer\n\r");
82 unsigned long cycles = 1000;
84 unsigned long transferCMD[] = {
'H',
'S', cycles };
85 radio.writeFast(&transferCMD, 12);
86 if (radio.txStandBy(timeoutPeriod)) {
91 for (
unsigned long i = 0; i < cycles; i++)
95 if (!radio.writeBlocking(&data, 32, timeoutPeriod)) {
109 radio.txStandBy(timeoutPeriod);
113 Serial.println(
"Communication not established");
116 float rate = cycles * 32 / (stopTime - startTime);
118 Serial.print(
"Transfer complete at ");
120 Serial.println(
" KB/s");
121 Serial.print(counter);
122 Serial.print(
" of ");
123 Serial.print(cycles);
124 Serial.println(
" Packets Failed to Send");
132 if (!transferInProgress) {
133 if (radio.available()) {
134 radio.read(&data, 32);
136 if (data[0] ==
'H' && data[4] ==
'S') {
138 payloads |= data[9] << 8;
140 transferInProgress = 1;
141 startTime = rxTimer =
millis();
145 if (radio.available()) {
146 radio.read(&data, 32);
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;
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();
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");
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.