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'.
9from RF24
import RF24, RF24_PA_LOW
12parser = argparse.ArgumentParser(
13 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
20 help=
"the identifying radio number (or node ID number)",
27 help=
"'1' specifies the TX role. '0' specifies the RX role.",
40radio =
RF24(CE_PIN, CSN_PIN)
53 """Transmits an incrementing float every second"""
59 buffer = struct.pack(
"<f", payload[0])
60 start_timer = time.monotonic_ns()
61 result = radio.write(buffer)
62 end_timer = time.monotonic_ns()
64 print(
"Transmission failed or timed out")
68 "Transmission successful! Time to Transmit:",
69 f
"{(end_timer - start_timer) / 1000} us. Sent: {payload[0]}",
73 print(failures,
"failures detected. Leaving TX role.")
77 """Listen for any payloads and print the transaction
79 :param int timeout: The number of seconds to wait (with no transmission)
80 until exiting function.
82 radio.startListening()
84 start_timer = time.monotonic()
85 while (time.monotonic() - start_timer) < timeout:
86 has_payload, pipe_number = radio.available_pipe()
89 buffer = radio.read(radio.payloadSize)
93 payload[0] = struct.unpack(
"<f", buffer[:4])[0]
96 f
"Received {radio.payloadSize} bytes",
97 f
"on pipe {pipe_number}: {payload[0]}",
99 start_timer = time.monotonic()
101 print(
"Nothing received in", timeout,
"seconds. Leaving RX role")
103 radio.stopListening()
106def set_role() -> bool:
107 """Set the role using stdin stream. Timeout arg for slave() can be
108 specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
111 -
True when role
is complete & app should
continue running.
112 -
False when app should exit
116 "*** Enter 'R' for receiver role.\n"
117 "*** Enter 'T' for transmitter role.\n"
118 "*** Enter 'Q' to quit example.\n"
122 user_input = user_input.split()
123 if user_input[0].upper().startswith(
"R"):
124 if len(user_input) > 1:
125 slave(int(user_input[1]))
129 if user_input[0].upper().startswith(
"T"):
132 if user_input[0].upper().startswith(
"Q"):
135 print(user_input[0],
"is an unrecognized input. Please try again.")
139if __name__ ==
"__main__":
141 args = parser.parse_args()
144 if not radio.begin():
145 raise RuntimeError(
"radio hardware is not responding")
149 address = [b
"1Node", b
"2Node"]
158 radio_number = args.node
159 if args.node
is None:
161 int(input(
"Which radio is this? Enter '0' or '1'. Defaults to '0' ")
or 0)
166 radio.setPALevel(RF24_PA_LOW)
169 radio.openWritingPipe(address[radio_number])
172 radio.openReadingPipe(1, address[
not radio_number])
177 radio.payloadSize = struct.calcsize(
"f")
186 if args.role
is None:
195 except KeyboardInterrupt:
196 print(
" Keyboard Interrupt detected. Powering down radio.")
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.