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

Written by 2bndy5 in 2020

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

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