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
21if RF24_DRIVER ==
"MRAA":
23elif RF24_DRIVER ==
"wiringPi":
27radio =
RF24(CE_PIN, CSN_PIN)
31 raise RuntimeError(
"radio hardware is not responding")
35address = [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])
60radio.payloadSize = struct.calcsize(
"f")
74 """Transmits an incrementing float every second"""
80 buffer = struct.pack(
"<f", payload[0])
81 start_timer = time.monotonic_ns()
82 result = radio.write(buffer)
83 end_timer = time.monotonic_ns()
85 print(
"Transmission failed or timed out")
89 "Transmission successful! Time to Transmit:",
90 f
"{(end_timer - start_timer) / 1000} us. Sent: {payload[0]}",
94 print(failures,
"failures detected. Leaving TX role.")
98 """Listen for any payloads and print the transaction
100 :param int timeout: The number of seconds to wait (with no transmission)
101 until exiting function.
103 radio.startListening()
105 start_timer = time.monotonic()
106 while (time.monotonic() - start_timer) < timeout:
107 has_payload, pipe_number = radio.available_pipe()
110 buffer = radio.read(radio.payloadSize)
114 payload[0] = struct.unpack(
"<f", buffer[:4])[0]
117 f
"Received {radio.payloadSize} bytes",
118 f
"on pipe {pipe_number}: {payload[0]}",
120 start_timer = time.monotonic()
122 print(
"Nothing received in", timeout,
"seconds. Leaving RX role")
124 radio.stopListening()
127def set_role() -> bool:
128 """Set the role using stdin stream. Timeout arg for slave() can be
129 specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
132 - True when role is complete & app should continue running.
133 - False when app should exit
137 "*** Enter 'R' for receiver role.\n"
138 "*** Enter 'T' for transmitter role.\n"
139 "*** Enter 'Q' to quit example.\n"
143 user_input = user_input.split()
144 if user_input[0].upper().startswith(
"R"):
145 if len(user_input) > 1:
146 slave(int(user_input[1]))
150 if user_input[0].upper().startswith(
"T"):
153 if user_input[0].upper().startswith(
"Q"):
156 print(user_input[0],
"is an unrecognized input. Please try again.")
160if __name__ ==
"__main__":
164 except KeyboardInterrupt:
165 print(
" Keyboard Interrupt detected. Powering down radio.")
168 print(
" Run slave() on receiver\n Run master() on transmitter")
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.