Optimized high speed nRF24L01+ driver class documentation v1.4.10
TMRh20 2020 - Optimized fork of the nRF24L01+ driver
Loading...
Searching...
No Matches
examples/StreamingData/StreamingData.ino

Written by 2bndy5 in 2020

A simple example of streaming data from 1 nRF24L01 transceiver to another.

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 * See documentation at https://nRF24.github.io/RF24
3 * See License information at root directory of this library
4 * Author: Brendan Doherty 2bndy5
5 */
6
13#include <SPI.h>
14#include "printf.h"
15#include "RF24.h"
16
17#define CE_PIN 7
18#define CSN_PIN 8
19// instantiate an object for the nRF24L01 transceiver
20RF24 radio(CE_PIN, CSN_PIN);
21
22// Let these addresses be used for the pair
23uint8_t address[][6] = { "1Node", "2Node" };
24// It is very helpful to think of an address as a path instead of as
25// an identifying device destination
26
27// to use different addresses on a pair of radios, we need a variable to
28// uniquely identify which address this radio will use to transmit
29bool radioNumber; // 0 uses address[0] to transmit, 1 uses address[1] to transmit
30
31// Used to control whether this node is sending or receiving
32bool role = false; // true = TX node, false = RX node
33
34// For this example, we'll be sending 32 payloads each containing
35// 32 bytes of data that looks like ASCII art when printed to the serial
36// monitor. The TX node and RX node needs only a single 32 byte buffer.
37#define SIZE 32 // this is the maximum for this example. (minimum is 1)
38char buffer[SIZE + 1]; // for the RX node
39uint8_t counter = 0; // for counting the number of received payloads
40void makePayload(uint8_t); // prototype to construct a payload dynamically
41
42
43void setup() {
44
45 buffer[SIZE] = 0; // add a NULL terminating character (for easy printing)
46
47 Serial.begin(115200);
48 while (!Serial) {
49 // some boards need to wait to ensure access to serial over USB
50 }
51
52 // initialize the transceiver on the SPI bus
53 if (!radio.begin()) {
54 Serial.println(F("radio hardware is not responding!!"));
55 while (1) {} // hold in infinite loop
56 }
57
58 // print example's introductory prompt
59 Serial.println(F("RF24/examples/StreamingData"));
60
61 // To set the radioNumber via the Serial monitor on startup
62 Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
63 while (!Serial.available()) {
64 // wait for user input
65 }
66 char input = Serial.parseInt();
67 radioNumber = input == 1;
68 Serial.print(F("radioNumber = "));
69 Serial.println((int)radioNumber);
70
71 // role variable is hardcoded to RX behavior, inform the user of this
72 Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
73
74 // Set the PA Level low to try preventing power supply related problems
75 // because these examples are likely run with nodes in close proximity to
76 // each other.
77 radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.
78
79 // save on transmission time by setting the radio to only transmit the
80 // number of bytes we need to transmit
81 radio.setPayloadSize(SIZE); // default value is the maximum 32 bytes
82
83 // set the TX address of the RX node into the TX pipe
84 radio.openWritingPipe(address[radioNumber]); // always uses pipe 0
85
86 // set the RX address of the TX node into a RX pipe
87 radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1
88
89 // additional setup specific to the node's role
90 if (role) {
91 radio.stopListening(); // put radio in TX mode
92 } else {
93 radio.startListening(); // put radio in RX mode
94 }
95
96 // For debugging info
97 // printf_begin(); // needed only once for printing details
98 // radio.printDetails(); // (smaller) function that prints raw register values
99 // radio.printPrettyDetails(); // (larger) function that prints human readable data
100
101} // setup()
102
103
104void loop() {
105
106 if (role) {
107 // This device is a TX node
108
109 radio.flush_tx();
110 uint8_t i = 0;
111 uint8_t failures = 0;
112 unsigned long start_timer = micros(); // start the timer
113 while (i < SIZE) {
114 makePayload(i); // make the payload
115 if (!radio.writeFast(&buffer, SIZE)) {
116 failures++;
117 radio.reUseTX();
118 } else {
119 i++;
120 }
121
122 if (failures >= 100) {
123 Serial.print(F("Too many failures detected. Aborting at payload "));
124 Serial.println(buffer[0]);
125 break;
126 }
127 }
128 unsigned long end_timer = micros(); // end the timer
129
130 Serial.print(F("Time to transmit = "));
131 Serial.print(end_timer - start_timer); // print the timer result
132 Serial.print(F(" us with "));
133 Serial.print(failures); // print failures detected
134 Serial.println(F(" failures detected"));
135
136 // to make this example readable in the serial monitor
137 delay(1000); // slow transmissions down by 1 second
138
139 } else {
140 // This device is a RX node
141
142 if (radio.available()) { // is there a payload?
143 radio.read(&buffer, SIZE); // fetch payload from FIFO
144 Serial.print(F("Received: "));
145 Serial.print(buffer); // print the payload's value
146 Serial.print(F(" - "));
147 Serial.println(counter++); // print the received counter
148 }
149 } // role
150
151 if (Serial.available()) {
152 // change the role via the serial monitor
153
154 char c = toupper(Serial.read());
155 if (c == 'T' && !role) {
156 // Become the TX node
157
158 role = true;
159 counter = 0; //reset the RX node's counter
160 Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
161 radio.stopListening();
162
163 } else if (c == 'R' && role) {
164 // Become the RX node
165
166 role = false;
167 Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
168 radio.startListening();
169 }
170 }
171
172} // loop
173
174
175void makePayload(uint8_t i) {
176 // Make a single payload based on position in stream.
177 // This example employs function to save memory on certain boards.
178
179 // let the first character be an identifying alphanumeric prefix
180 // this lets us see which payload didn't get received
181 buffer[0] = i + (i < 26 ? 65 : 71);
182 for (uint8_t j = 0; j < SIZE - 1; ++j) {
183 char chr = j >= (SIZE - 1) / 2 + abs((SIZE - 1) / 2 - i);
184 chr |= j < (SIZE - 1) / 2 - abs((SIZE - 1) / 2 - i);
185 buffer[j + 1] = chr + 48;
186 }
187}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
Definition RF24.h:116
@ RF24_PA_LOW
Definition RF24.h:50
#define delay(millisec)