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

Written by 2bndy5 in 2020

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 * 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
14#include <SPI.h>
15#include "printf.h"
16#include "RF24.h"
17
18#define CE_PIN 7
19#define CSN_PIN 8
20// instantiate an object for the nRF24L01 transceiver
21RF24 radio(CE_PIN, CSN_PIN);
22
23// an identifying device destination
24// Let these addresses be used for the pair
25uint8_t address[][6] = { "1Node", "2Node" };
26// It is very helpful to think of an address as a path instead of as
27// an identifying device destination
28// to use different addresses on a pair of radios, we need a variable to
29
30// uniquely identify which address this radio will use to transmit
31bool radioNumber = 1; // 0 uses address[0] to transmit, 1 uses address[1] to transmit
32
33// Used to control whether this node is sending or receiving
34bool role = false; // true = TX role, false = RX role
35
36// For this example, we'll be using a payload containing
37// a string & an integer number that will be incremented
38// on every successful transmission.
39// Make a data structure to store the entire payload of different datatypes
40struct PayloadStruct {
41 char message[7]; // only using 6 characters for TX & ACK payloads
42 uint8_t counter;
43};
44PayloadStruct payload;
45
46void setup() {
47
48 Serial.begin(115200);
49 while (!Serial) {
50 // some boards need to wait to ensure access to serial over USB
51 }
52
53 // initialize the transceiver on the SPI bus
54 if (!radio.begin()) {
55 Serial.println(F("radio hardware is not responding!!"));
56 while (1) {} // hold in infinite loop
57 }
58
59 // print example's introductory prompt
60 Serial.println(F("RF24/examples/AcknowledgementPayloads"));
61
62 // To set the radioNumber via the Serial monitor on startup
63 Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
64 while (!Serial.available()) {
65 // wait for user input
66 }
67 char input = Serial.parseInt();
68 radioNumber = input == 1;
69 Serial.print(F("radioNumber = "));
70 Serial.println((int)radioNumber);
71
72 // role variable is hardcoded to RX behavior, inform the user of this
73 Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
74
75 // Set the PA Level low to try preventing power supply related problems
76 // because these examples are likely run with nodes in close proximity to
77 // each other.
78 radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.
79
80 // to use ACK payloads, we need to enable dynamic payload lengths (for all nodes)
81 radio.enableDynamicPayloads(); // ACK payloads are dynamically sized
82
83 // Acknowledgement packets have no payloads by default. We need to enable
84 // this feature for all nodes (TX & RX) to use ACK payloads.
85 radio.enableAckPayload();
86
87 // set the TX address of the RX node for use on the TX pipe (pipe 0)
88 radio.stopListening(address[radioNumber]); // put radio in TX mode
89
90 // set the RX address of the TX node into a RX pipe
91 radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1
92
93 // additional setup specific to the node's role
94 if (role) {
95 // setup the TX payload
96 memcpy(payload.message, "Hello ", 6); // set the payload message
97 } else {
98 // setup the ACK payload & load the first response into the FIFO
99
100 memcpy(payload.message, "World ", 6); // set the payload message
101 // load the payload for the first received transmission on pipe 0
102 radio.writeAckPayload(1, &payload, sizeof(payload));
103
104 radio.startListening(); // put radio in RX mode
105 }
106
107 // For debugging info
108 // printf_begin(); // needed only once for printing details
109 // radio.printDetails(); // (smaller) function that prints raw register values
110 // radio.printPrettyDetails(); // (larger) function that prints human readable data
111}
112
113void loop() {
114
115 if (role) {
116 // This device is a TX node
117
118 unsigned long start_timer = micros(); // start the timer
119 bool report = radio.write(&payload, sizeof(payload)); // transmit & save the report
120 unsigned long end_timer = micros(); // end the timer
121
122 if (report) {
123 Serial.print(F("Transmission successful! ")); // payload was delivered
124 Serial.print(F("Time to transmit = "));
125 Serial.print(end_timer - start_timer); // print the timer result
126 Serial.print(F(" us. Sent: "));
127 Serial.print(payload.message); // print the outgoing message
128 Serial.print(payload.counter); // print the outgoing counter
129 uint8_t pipe;
130 if (radio.available(&pipe)) { // is there an ACK payload? grab the pipe number that received it
131 PayloadStruct received;
132 radio.read(&received, sizeof(received)); // get incoming ACK payload
133 Serial.print(F(" Received "));
134 Serial.print(radio.getDynamicPayloadSize()); // print incoming payload size
135 Serial.print(F(" bytes on pipe "));
136 Serial.print(pipe); // print pipe number that received the ACK
137 Serial.print(F(": "));
138 Serial.print(received.message); // print incoming message
139 Serial.println(received.counter); // print incoming counter
140
141 // save incoming counter & increment for next outgoing
142 payload.counter = received.counter + 1;
143
144 } else {
145 Serial.println(F(" Received: an empty ACK packet")); // empty ACK packet received
146 }
147
148
149 } else {
150 Serial.println(F("Transmission failed or timed out")); // payload was not delivered
151 }
152
153 // to make this example readable in the serial monitor
154 delay(1000); // slow transmissions down by 1 second
155
156 } else {
157 // This device is a RX node
158
159 uint8_t pipe;
160 if (radio.available(&pipe)) { // is there a payload? get the pipe number that received it
161 uint8_t bytes = radio.getDynamicPayloadSize(); // get the size of the payload
162 PayloadStruct received;
163 radio.read(&received, sizeof(received)); // get incoming payload
164 Serial.print(F("Received "));
165 Serial.print(bytes); // print the size of the payload
166 Serial.print(F(" bytes on pipe "));
167 Serial.print(pipe); // print the pipe number
168 Serial.print(F(": "));
169 Serial.print(received.message); // print incoming message
170 Serial.print(received.counter); // print incoming counter
171 Serial.print(F(" Sent: "));
172 Serial.print(payload.message); // print outgoing message
173 Serial.println(payload.counter); // print outgoing counter
174
175 // save incoming counter & increment for next outgoing
176 payload.counter = received.counter + 1;
177 // load the payload for the first received transmission on pipe 0
178 radio.writeAckPayload(1, &payload, sizeof(payload));
179 }
180 } // role
181
182 if (Serial.available()) {
183 // change the role via the serial monitor
184
185 char c = toupper(Serial.read());
186 if (c == 'T' && !role) {
187 // Become the TX node
188
189 role = true;
190 Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
191
192 memcpy(payload.message, "Hello ", 6); // change payload message
193 radio.stopListening(); // this also discards any unused ACK payloads
194
195 } else if (c == 'R' && role) {
196 // Become the RX node
197
198 role = false;
199 Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
200 memcpy(payload.message, "World ", 6); // change payload message
201
202 // load the payload for the first received transmission on pipe 0
203 radio.writeAckPayload(1, &payload, sizeof(payload));
204 radio.startListening();
205 }
206 }
207} // loop
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
Definition RF24.h:160
@ RF24_PA_LOW
Definition RF24.h:50
#define delay(millisec)