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