Written by 2bndy5 in 2020
This is a simple example of using the RF24 class on a Raspberry Pi.
Remember to install the Python wrapper, then navigate to the "RF24/examples_linux" folder.
To run this example, enter
python3 getting_started.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 sending data from 1 nRF24L01 transceiver to another.
3This example was written to be used on 2 devices acting as 'nodes'.
5See 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"]
45 int(input(
"Which radio is this? Enter '0' or '1'. Defaults to '0' ")
or 0)
50radio.setPALevel(RF24_PA_LOW)
53radio.openWritingPipe(address[radio_number])
56radio.openReadingPipe(1, address[
not radio_number])
61radio.payloadSize = struct.calcsize(
"f")
75 """Transmits an incrementing float every second"""
81 buffer = struct.pack(
"<f", payload[0])
82 start_timer = time.monotonic_ns()
83 result = radio.write(buffer)
84 end_timer = time.monotonic_ns()
86 print(
"Transmission failed or timed out")
90 "Transmission successful! Time to Transmit:",
91 f
"{(end_timer - start_timer) / 1000} us. Sent: {payload[0]}",
95 print(failures,
"failures detected. Leaving TX role.")
99 """Listen for any payloads and print the transaction
101 :param int timeout: The number of seconds to wait (with no transmission)
102 until exiting function.
104 radio.startListening()
106 start_timer = time.monotonic()
107 while (time.monotonic() - start_timer) < timeout:
108 has_payload, pipe_number = radio.available_pipe()
111 buffer = radio.read(radio.payloadSize)
115 payload[0] = struct.unpack(
"<f", buffer[:4])[0]
118 f
"Received {radio.payloadSize} bytes",
119 f
"on pipe {pipe_number}: {payload[0]}",
121 start_timer = time.monotonic()
123 print(
"Nothing received in", timeout,
"seconds. Leaving RX role")
125 radio.stopListening()
128def set_role() -> bool:
129 """Set the role using stdin stream. Timeout arg for slave() can be
130 specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
133 - True when role is complete & app should continue running.
134 - False when app should exit
138 "*** Enter 'R' for receiver role.\n"
139 "*** Enter 'T' for transmitter role.\n"
140 "*** Enter 'Q' to quit example.\n"
144 user_input = user_input.split()
145 if user_input[0].upper().startswith(
"R"):
146 if len(user_input) > 1:
147 slave(int(user_input[1]))
151 if user_input[0].upper().startswith(
"T"):
154 if user_input[0].upper().startswith(
"Q"):
157 print(user_input[0],
"is an unrecognized input. Please try again.")
161if __name__ ==
"__main__":
165 except KeyboardInterrupt:
166 print(
" Keyboard Interrupt detected. Powering down radio.")
169 print(
" Run slave() on receiver\n Run master() on transmitter")
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.