and follow the prompts.
1"""
2A simple example of streaming data from 1 nRF24L01 transceiver to another.
3
4This example was written to be used on 2 devices acting as 'nodes'.
5
6See documentation at https://nRF24.github.io/RF24
7"""
8
9import time
10from RF24 import RF24, RF24_PA_LOW, RF24_DRIVER, RF24_TX_DF
11
12print(__file__)
13
14
20CSN_PIN = 0
21if RF24_DRIVER == "MRAA":
22 CE_PIN = 15
23elif RF24_DRIVER == "wiringPi":
24 CE_PIN = 3
25else:
26 CE_PIN = 22
27radio =
RF24(CE_PIN, CSN_PIN)
28
29
30if not radio.begin():
31 raise RuntimeError("radio hardware is not responding")
32
33
34
35address = [b"1Node", b"2Node"]
36
37
38
39
40
41
42radio_number = bool(
43 int(input("Which radio is this? Enter '0' or '1'. Defaults to '0' ") or 0)
44)
45
46
47
48radio.setPALevel(RF24_PA_LOW)
49
50
51radio.stopListening(address[radio_number])
52
53
54radio.openReadingPipe(1, address[not radio_number])
55
56
57
58
59SIZE = 32
60
61
62
63radio.payloadSize = SIZE
64
65
66
67
68
69
70
71
72def make_buffer(buf_iter: int) -> bytes:
73 """Returns a dynamically created payloads
74
75 :param int buf_iter: The position of the payload in the data stream
76 """
77
78
79
80
81 buff = bytes([buf_iter + (65 if 0 <= buf_iter < 26 else 71)])
82 for j in range(SIZE - 1):
83 char = bool(j >= (SIZE - 1) / 2 + abs((SIZE - 1) / 2 - buf_iter))
84 char |= bool(j < (SIZE - 1) / 2 - abs((SIZE - 1) / 2 - buf_iter))
85 buff += bytes([char + 48])
86 return buff
87
88
89def master(count: int = 1):
90 """Uses all 3 levels of the TX FIFO to send a stream of data
91
92 :param int count: how many times to transmit the stream of data.
93 """
94 radio.stopListening()
95 radio.flush_tx()
96 failures = 0
97 for multiplier in range(count):
98 buf_iter = 0
99 start_timer = time.monotonic_ns()
100 while buf_iter < SIZE:
101 buffer = make_buffer(buf_iter)
102
103 if not radio.writeFast(buffer):
104 flags = radio.getStatusFlags()
105 if flags & RF24_TX_DF:
106 failures += 1
107
108 radio.ce(False)
109 flags = radio.clearStatusFlags(RF24_TX_DF)
110 radio.ce(True)
111 if failures > 99 and buf_iter < 7 and multiplier < 2:
112
113 print("Too many failures detected. Aborting at payload ", buffer[0])
114 multiplier = count
115 break
116 else:
117 buf_iter += 1
118 end_timer = time.monotonic_ns()
119 print(
120 f"Time to transmit data = {(end_timer - start_timer) / 1000} us.",
121 f"Detected {failures} failures.",
122 )
123
124
125def slave(timeout: int = 6):
126 """Listen for any payloads and print them out (suffixed with received
127 counter)
128
129 :param int timeout: The number of seconds to wait (with no transmission)
130 until exiting function.
131 """
132 radio.startListening()
133 count = 0
134 start_timer = time.monotonic()
135 while (time.monotonic() - start_timer) < timeout:
136 if radio.available():
137 count += 1
138
139 receive_payload = radio.read(radio.payloadSize)
140 print("Received:", receive_payload, "-", count)
141 start_timer = time.monotonic()
142
143 print("Nothing received in", timeout, "seconds. Leaving RX role")
144
145 radio.stopListening()
146
147
148def set_role() -> bool:
149 """Set the role using stdin stream. Role args can be specified using space
150 delimiters (e.g. 'R 10' calls `slave(10)` & 'T 3' calls `master(3)`)
151
152 :return:
153 - True when role is complete & app should continue running.
154 - False when app should exit
155 """
156 user_input = (
157 input(
158 "*** Enter 'R' for receiver role.\n"
159 "*** Enter 'T' for transmitter role.\n"
160 "*** Enter 'Q' to quit example.\n"
161 )
162 or "?"
163 )
164 user_input = user_input.split()
165 if user_input[0].upper().startswith("R"):
166 if len(user_input) > 1:
167 slave(int(user_input[1]))
168 else:
169 slave()
170 return True
171 if user_input[0].upper().startswith("T"):
172 if len(user_input) > 1:
173 master(int(user_input[1]))
174 else:
175 master()
176 return True
177 if user_input[0].upper().startswith("Q"):
178 radio.powerDown()
179 return False
180 print(user_input[0], "is an unrecognized input. Please try again.")
181 return set_role()
182
183
184if __name__ == "__main__":
185 try:
186 while set_role():
187 pass
188 except KeyboardInterrupt:
189 print(" Keyboard Interrupt detected. Powering down radio.")
190 radio.powerDown()
191else:
192 print(" Run slave() on receiver\n Run master() on transmitter")
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.