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
3
4
5
6
13#include <ctime>
14#include <iostream>
15#include <string>
16#include <time.h>
18
19using namespace std;
20
21
22
23
24
25
26#define CSN_PIN 0
27#ifdef MRAA
28 #define CE_PIN 15
29#elif defined(RF24_WIRINGPI)
30 #define CE_PIN 3
31#else
32 #define CE_PIN 22
33#endif
34
35RF24 radio(CE_PIN, CSN_PIN);
36
37
38
39
40
41
42
43
44float payload = 0.0;
45
46void setRole();
47void master();
48void slave();
49
50
51struct timespec startTimer, endTimer;
52uint32_t getMicros();
53
54int main(int argc, char** argv)
55{
56
57
58 if (!radio.begin()) {
59 cout << "radio hardware is not responding!!" << endl;
60 return 0;
61 }
62
63
64
65 bool radioNumber = 1;
66
67
68 cout << argv[0] << endl;
69
70
71 uint8_t address[2][6] = {"1Node", "2Node"};
72
73
74
75
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
82
83 radio.setPayloadSize(sizeof(payload));
84
85
86
87
89
90
91 radio.stopListening(address[radioNumber]);
92
93
94 radio.openReadingPipe(1, address[!radioNumber]);
95
96
97
98
99
100
101 setRole();
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 = "";
128 }
129}
130
134void master()
135{
136 radio.stopListening();
137
138 unsigned int failure = 0;
139 while (failure < 6) {
140 clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);
141 bool report = radio.write(&payload, sizeof(float));
142 uint32_t timerElapsed = getMicros();
143
144 if (report) {
145
146 cout << "Transmission successful! Time to transmit = ";
147 cout << timerElapsed;
148 cout << " us. Sent: " << payload << endl;
149 payload += 0.01;
150 }
151 else {
152
153 cout << "Transmission failed or timed out" << endl;
154 failure++;
155 }
156
157
159 }
160 cout << failure << " failures detected. Leaving TX role." << endl;
161}
162
166void slave()
167{
168
169 radio.startListening();
170
171 time_t startTimer = time(nullptr);
172 while (time(nullptr) - startTimer < 6) {
173 uint8_t pipe;
174 if (radio.available(&pipe)) {
175 uint8_t bytes = radio.getPayloadSize();
176 radio.read(&payload, bytes);
177 cout << "Received " << (unsigned int)bytes;
178 cout << " bytes on pipe " << (unsigned int)pipe;
179 cout << ": " << payload << endl;
180 startTimer = time(nullptr);
181 }
182 }
183 cout << "Nothing received in 6 seconds. Leaving RX role." << endl;
184 radio.stopListening();
185}
186
190uint32_t getMicros()
191{
192
193
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.