and follow the prompts.
2A simple example of sending data from 1 nRF24L01 transceiver to another
3with Acknowledgement (ACK) payloads attached to ACK packets.
5This example was written to be used on 2 devices acting as 'nodes'.
7See documentation at https://nRF24.github.io/RF24
11from 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"]
41 int(input(
"Which radio is this? Enter '0' or '1'. Defaults to '0' ")
or 0)
45radio.enableDynamicPayloads()
48radio.enableAckPayload()
52radio.setPALevel(RF24_PA_LOW)
55radio.openWritingPipe(address[radio_number])
58radio.openReadingPipe(1, address[
not radio_number])
72 """Transmits a message and an incrementing integer every second."""
77 buffer = b
"Hello \x00" + bytes(counter)
80 start_timer = time.monotonic_ns()
81 result = radio.write(buffer)
82 end_timer = time.monotonic_ns()
85 decoded = buffer[:6].decode(
"utf-8")
87 "Transmission successful! Time to transmit:",
88 f
"{int((end_timer - start_timer) / 1000)} us.",
89 f
"Sent: {decoded}{counter[0]}",
92 has_payload, pipe_number = radio.available_pipe()
95 length = radio.getDynamicPayloadSize()
96 response = radio.read(length)
97 decoded = bytes(response[:6]).decode(
"utf-8")
99 f
"Received {length} on pipe {pipe_number}:",
100 f
"{decoded}{response[7:8][0]}",
103 if response[7:8][0] < 255:
104 counter[0] = response[7:8][0] + 1
108 print(
"Received an empty ACK packet")
111 print(
"Transmission failed or timed out")
113 print(failures,
"failures detected. Leaving TX role.")
116def slave(timeout: int = 6):
117 """Listen for any payloads and print the transaction
119 :param int timeout: The number of seconds to wait (with no transmission)
120 until exiting function.
122 radio.startListening()
125 buffer = b
"World \x00" + bytes(counter)
128 radio.writeAckPayload(1, buffer)
130 start_timer = time.monotonic()
131 while (time.monotonic() - start_timer) < timeout:
132 has_payload, pipe_number = radio.available_pipe()
134 length = radio.getDynamicPayloadSize()
135 received = radio.read(length)
137 counter[0] = received[7:8][0] + 1
if received[7:8][0] < 255
else 0
138 decoded = [bytes(received[:6]).decode(
"utf-8")]
139 decoded.append(buffer[:6].decode(
"utf-8"))
141 f
"Received {length} bytes on pipe {pipe_number}:",
142 f
"{decoded[0]}{received[7:8][0]}",
143 f
"Sent: {decoded[1]}{buffer[7:8][0]}",
145 buffer = b
"World \x00" + bytes(counter)
146 radio.writeAckPayload(1, buffer)
147 start_timer = time.monotonic()
149 print(
"Nothing received in", timeout,
"seconds. Leaving RX role")
151 radio.stopListening()
154def set_role() -> bool:
155 """Set the role using stdin stream. Timeout arg for slave() can be
156 specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
159 - True when role is complete & app should continue running.
160 - False when app should exit
164 "*** Enter 'R' for receiver role.\n"
165 "*** Enter 'T' for transmitter role.\n"
166 "*** Enter 'Q' to quit example.\n"
170 user_input = user_input.split()
171 if user_input[0].upper().startswith(
"R"):
172 if len(user_input) > 1:
173 slave(int(user_input[1]))
177 if user_input[0].upper().startswith(
"T"):
180 if user_input[0].upper().startswith(
"Q"):
183 print(user_input[0],
"is an unrecognized input. Please try again.")
187if __name__ ==
"__main__":
191 except KeyboardInterrupt:
192 print(
" Keyboard Interrupt detected. Exiting...")
195 print(
" Run slave() on receiver\n Run master() on transmitter")
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.