Optimized high speed nRF24L01+ driver class documentation v1.4.8
TMRh20 2020 - Optimized fork of the nRF24L01+ driver
Loading...
Searching...
No Matches
examples_linux/acknowledgementPayloads.cpp

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 ctrl+c to quit at any time.

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 <ctime> // time()
15#include <iostream> // cin, cout, endl
16#include <string> // string, getline()
17#include <time.h> // CLOCK_MONOTONIC_RAW, timespec, clock_gettime()
18#include <RF24/RF24.h> // RF24, RF24_PA_LOW, delay()
19
20using namespace std;
21
22/****************** Linux ***********************/
23// Radio CE Pin, CSN Pin, SPI Speed
24// CE Pin uses GPIO number with BCM and SPIDEV drivers, other platforms use their own pin numbering
25// CS Pin addresses the SPI bus number at /dev/spidev<a>.<b>
26// ie: RF24 radio(<ce_pin>, <a>*10+<b>); spidev1.0 is 10, spidev1.1 is 11 etc..
27#define CSN_PIN 0
28#ifdef MRAA
29 #define CE_PIN 15 // GPIO22
30#else
31 #define CE_PIN 22
32#endif
33// Generic:
34RF24 radio(CE_PIN, CSN_PIN);
35/****************** Linux (BBB,x86,etc) ***********************/
36// See http://nRF24.github.io/RF24/pages.html for more information on usage
37// See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
38// See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
39
40// For this example, we'll be using a payload containing
41// a string & an integer number that will be incremented
42// on every successful transmission.
43// Make a data structure to store the entire payload of different datatypes
44struct PayloadStruct
45{
46 char message[7]; // only using 6 characters for TX & ACK payloads
47 uint8_t counter;
48};
49PayloadStruct payload;
50
51void setRole(); // prototype to set the node's role
52void master(); // prototype of the TX node's behavior
53void slave(); // prototype of the RX node's behavior
54
55// custom defined timer for evaluating transmission time in microseconds
56struct timespec startTimer, endTimer;
57uint32_t getMicros(); // prototype to get elapsed time in microseconds
58
59int main(int argc, char** argv)
60{
61 // perform hardware check
62 if (!radio.begin()) {
63 cout << "radio hardware is not responding!!" << endl;
64 return 0; // quit now
65 }
66
67 // Let these addresses be used for the pair
68 uint8_t address[2][6] = {"1Node", "2Node"};
69 // It is very helpful to think of an address as a path instead of as
70 // an identifying device destination
71
72 // to use different addresses on a pair of radios, we need a variable to
73 // uniquely identify which address this radio will use to transmit
74 bool radioNumber = 1; // 0 uses address[0] to transmit, 1 uses address[1] to transmit
75
76 // print example's name
77 cout << argv[0] << endl;
78
79 // Set the radioNumber via the terminal on startup
80 cout << "Which radio is this? Enter '0' or '1'. Defaults to '0' ";
81 string input;
82 getline(cin, input);
83 radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
84
85 // to use ACK payloads, we need to enable dynamic payload lengths
86 radio.enableDynamicPayloads(); // ACK payloads are dynamically sized
87
88 // Acknowledgement packets have no payloads by default. We need to enable
89 // this feature for all nodes (TX & RX) to use ACK payloads.
90 radio.enableAckPayload();
91
92 // Set the PA Level low to try preventing power supply related problems
93 // because these examples are likely run with nodes in close proximity to
94 // each other.
95 radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.
96
97 // set the TX address of the RX node into the TX pipe
98 radio.openWritingPipe(address[radioNumber]); // always uses pipe 0
99
100 // set the RX address of the TX node into a RX pipe
101 radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1
102
103 // For debugging info
104 // radio.printDetails(); // (smaller) function that prints raw register values
105 // radio.printPrettyDetails(); // (larger) function that prints human readable data
106
107 // ready to execute program now
108 setRole(); // calls master() or slave() based on user input
109 return 0;
110}
111
116void setRole()
117{
118 string input = "";
119 while (!input.length()) {
120 cout << "*** PRESS 'T' to begin transmitting to the other node\n";
121 cout << "*** PRESS 'R' to begin receiving from the other node\n";
122 cout << "*** PRESS 'Q' to exit" << endl;
123 getline(cin, input);
124 if (input.length() >= 1) {
125 if (input[0] == 'T' || input[0] == 't')
126 master();
127 else if (input[0] == 'R' || input[0] == 'r')
128 slave();
129 else if (input[0] == 'Q' || input[0] == 'q')
130 break;
131 else
132 cout << input[0] << " is an invalid input. Please try again." << endl;
133 }
134 input = ""; // stay in the while loop
135 } // while
136} // setRole()
137
141void master()
142{
143 memcpy(payload.message, "Hello ", 6); // set the payload message
144 radio.stopListening(); // put radio in TX mode
145
146 unsigned int failures = 0; // keep track of failures
147 while (failures < 6) {
148 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer); // start the timer
149 bool report = radio.write(&payload, sizeof(payload)); // transmit & save the report
150 uint32_t timerElapsed = getMicros(); // end the timer
151
152 if (report) {
153 // payload was delivered
154 cout << "Transmission successful! Time to transmit = ";
155 cout << timerElapsed; // print the timer result
156 cout << " us. Sent: ";
157 cout << payload.message; // print outgoing message
158 cout << (unsigned int)payload.counter; // print outgoing counter counter
159
160 uint8_t pipe;
161 if (radio.available(&pipe)) {
162 PayloadStruct received;
163 radio.read(&received, sizeof(received)); // get incoming ACK payload
164 cout << " Received ";
165 cout << (unsigned int)radio.getDynamicPayloadSize(); // print incoming payload size
166 cout << " bytes on pipe " << (unsigned int)pipe; // print pipe that received it
167 cout << ": " << received.message; // print incoming message
168 cout << (unsigned int)received.counter << endl; // print incoming counter
169 payload.counter = received.counter + 1; // save incoming counter & increment for next outgoing
170 } // if got an ACK payload
171 else {
172 cout << " Received an empty ACK packet." << endl; // ACK had no payload
173 }
174 } // if delivered
175 else {
176 cout << "Transmission failed or timed out" << endl; // payload was not delivered
177 failures++; // increment failures
178 }
179
180 // to make this example readable in the terminal
181 delay(1000); // slow transmissions down by 1 second
182 } // while
183 cout << failures << " failures detected. Leaving TX role." << endl;
184} // master
185
189void slave()
190{
191 memcpy(payload.message, "World ", 6); // set the payload message
192
193 // load the payload for the first received transmission on pipe 0
194 radio.writeAckPayload(1, &payload, sizeof(payload));
195
196 radio.startListening(); // put radio in RX mode
197 time_t startTimer = time(nullptr); // start a timer
198 while (time(nullptr) - startTimer < 6) { // use 6 second timeout
199 uint8_t pipe;
200 if (radio.available(&pipe)) { // is there a payload? get the pipe number that recieved it
201 uint8_t bytes = radio.getDynamicPayloadSize(); // get the size of the payload
202 PayloadStruct received;
203 radio.read(&received, sizeof(received)); // fetch payload from RX FIFO
204 cout << "Received " << (unsigned int)bytes; // print the size of the payload
205 cout << " bytes on pipe " << (unsigned int)pipe; // print the pipe number
206 cout << ": " << received.message;
207 cout << (unsigned int)received.counter; // print received payload
208 cout << " Sent: ";
209 cout << payload.message;
210 cout << (unsigned int)payload.counter << endl; // print ACK payload sent
211 startTimer = time(nullptr); // reset timer
212
213 // save incoming counter & increment for next outgoing
214 payload.counter = received.counter + 1;
215 // load the payload for the first received transmission on pipe 0
216 radio.writeAckPayload(1, &payload, sizeof(payload));
217 } // if received something
218 } // while
219 cout << "Nothing received in 6 seconds. Leaving RX role." << endl;
220 radio.stopListening(); // recommended idle behavior is TX mode
221} // slave
222
226uint32_t getMicros()
227{
228 // this function assumes that the timer was started using
229 // `clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);`
230
231 clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
232 uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
233 uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
234
235 return ((seconds)*1000 + useconds) + 0.5;
236}
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
Definition: RF24.h:116
@ RF24_PA_LOW
Definition: RF24.h:50
#define delay(milisec)