Written by 2bndy5 in 2020
This is a simple example of using the RF24 class on a Raspberry Pi for streaming multiple payloads.
Remember to install the Python wrapper, then navigate to the "RF24/examples_linux" folder.
To run this example, enter
python3 streaming_data.py
and follow the prompts.
- Note
- this example requires python v3.7 or newer because it measures transmission time with
time.monotonic_ns()
.
2A simple example of streaming data from 1 nRF24L01 transceiver to another.
4This example was written to be used on 2 devices acting as 'nodes'.
6See documentation at https://nRF24.github.io/RF24
10from RF24
import RF24, RF24_PA_LOW, RF24_DRIVER
22if RF24_DRIVER ==
"MRAA":
24elif RF24_DRIVER ==
"wiringPi":
28radio =
RF24(CE_PIN, CSN_PIN)
32 raise RuntimeError(
"radio hardware is not responding")
36address = [b
"1Node", b
"2Node"]
44 int(input(
"Which radio is this? Enter '0' or '1'. Defaults to '0' ")
or 0)
49radio.setPALevel(RF24_PA_LOW)
52radio.openWritingPipe(address[radio_number])
55radio.openReadingPipe(1, address[
not radio_number])
64radio.payloadSize = SIZE
73def make_buffer(buf_iter: int) -> bytes:
74 """Returns a dynamically created payloads
76 :param int buf_iter: The position of the payload in the data stream
82 buff = bytes([buf_iter + (65
if 0 <= buf_iter < 26
else 71)])
83 for j
in range(SIZE - 1):
84 char = bool(j >= (SIZE - 1) / 2 + abs((SIZE - 1) / 2 - buf_iter))
85 char |= bool(j < (SIZE - 1) / 2 - abs((SIZE - 1) / 2 - buf_iter))
86 buff += bytes([char + 48])
90def master(count: int = 1):
91 """Uses all 3 levels of the TX FIFO to send a stream of data
93 :param int count: how many times to transmit the stream of data.
98 start_timer = time.monotonic_ns()
99 for multiplier
in range(count):
101 while buf_iter < SIZE:
102 buffer = make_buffer(buf_iter)
104 if not radio.writeFast(buffer):
106 if failures > 99
and buf_iter < 7
and multiplier < 2:
108 print(
"Too many failures detected. Aborting at payload ", buffer[0])
114 end_timer = time.monotonic_ns()
116 f
"Time to transmit data = {(end_timer - start_timer) / 1000} us.",
117 f
"Detected {failures} failures.",
121def slave(timeout: int = 6):
122 """Listen for any payloads and print them out (suffixed with received
125 :param int timeout: The number of seconds to wait (with no transmission)
126 until exiting function.
128 radio.startListening()
130 start_timer = time.monotonic()
131 while (time.monotonic() - start_timer) < timeout:
132 if radio.available():
135 receive_payload = radio.read(radio.payloadSize)
136 print(
"Received:", receive_payload,
"-", count)
137 start_timer = time.monotonic()
139 print(
"Nothing received in", timeout,
"seconds. Leaving RX role")
141 radio.stopListening()
144def set_role() -> bool:
145 """Set the role using stdin stream. Role args can be specified using space
146 delimiters (e.g. 'R 10' calls `slave(10)` & 'T 3' calls `master(3)`)
149 - True when role is complete & app should continue running.
150 - False when app should exit
154 "*** Enter 'R' for receiver role.\n"
155 "*** Enter 'T' for transmitter role.\n"
156 "*** Enter 'Q' to quit example.\n"
160 user_input = user_input.split()
161 if user_input[0].upper().startswith(
"R"):
162 if len(user_input) > 1:
163 slave(int(user_input[1]))
167 if user_input[0].upper().startswith(
"T"):
168 if len(user_input) > 1:
169 master(int(user_input[1]))
173 if user_input[0].upper().startswith(
"Q"):
176 print(user_input[0],
"is an unrecognized input. Please try again.")
180if __name__ ==
"__main__":
184 except KeyboardInterrupt:
185 print(
" Keyboard Interrupt detected. Powering down radio.")
188 print(
" Run slave() on receiver\n Run master() on transmitter")
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.