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