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'.
10from RF24
import RF24, RF24_PA_LOW
13parser = argparse.ArgumentParser(
14 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
21 help=
"the identifying radio number (or node ID number)",
28 help=
"'1' specifies the TX role. '0' specifies the RX role.",
40radio =
RF24(CE_PIN, CSN_PIN)
53 """Transmits a message and an incrementing integer every second."""
58 buffer = b
"Hello \x00" + bytes(counter)
61 start_timer = time.monotonic_ns()
62 result = radio.write(buffer)
63 end_timer = time.monotonic_ns()
66 decoded = buffer[:6].decode(
"utf-8")
68 "Transmission successful! Time to transmit:",
69 f
"{int((end_timer - start_timer) / 1000)} us.",
70 f
"Sent: {decoded}{counter[0]}",
73 has_payload, pipe_number = radio.available_pipe()
76 length = radio.getDynamicPayloadSize()
77 response = radio.read(length)
78 decoded = bytes(response[:6]).decode(
"utf-8")
80 f
"Received {length} on pipe {pipe_number}:",
81 f
"{decoded}{response[7:8][0]}",
84 if response[7:8][0] < 255:
85 counter[0] = response[7:8][0] + 1
89 print(
"Received an empty ACK packet")
92 print(
"Transmission failed or timed out")
94 print(failures,
"failures detected. Leaving TX role.")
97def slave(timeout: int = 6):
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()
106 buffer = b
"World \x00" + bytes(counter)
109 radio.writeAckPayload(1, buffer)
111 start_timer = time.monotonic()
112 while (time.monotonic() - start_timer) < timeout:
113 has_payload, pipe_number = radio.available_pipe()
115 length = radio.getDynamicPayloadSize()
116 received = radio.read(length)
118 counter[0] = received[7:8][0] + 1
if received[7:8][0] < 255
else 0
119 decoded = [bytes(received[:6]).decode(
"utf-8")]
120 decoded.append(buffer[:6].decode(
"utf-8"))
122 f
"Received {length} bytes on pipe {pipe_number}:",
123 f
"{decoded[0]}{received[7:8][0]}",
124 f
"Sent: {decoded[1]}{buffer[7:8][0]}",
126 buffer = b
"World \x00" + bytes(counter)
127 radio.writeAckPayload(1, buffer)
128 start_timer = time.monotonic()
130 print(
"Nothing received in", timeout,
"seconds. Leaving RX role")
132 radio.stopListening()
135def set_role() -> bool:
136 """Set the role using stdin stream. Timeout arg for slave() can be
137 specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
140 -
True when role
is complete & app should
continue running.
141 -
False when app should exit
145 "*** Enter 'R' for receiver role.\n"
146 "*** Enter 'T' for transmitter role.\n"
147 "*** Enter 'Q' to quit example.\n"
151 user_input = user_input.split()
152 if user_input[0].upper().startswith(
"R"):
153 if len(user_input) > 1:
154 slave(int(user_input[1]))
158 if user_input[0].upper().startswith(
"T"):
161 if user_input[0].upper().startswith(
"Q"):
164 print(user_input[0],
"is an unrecognized input. Please try again.")
168if __name__ ==
"__main__":
170 args = parser.parse_args()
173 if not radio.begin():
174 raise RuntimeError(
"radio hardware is not responding")
178 address = [b
"1Node", b
"2Node"]
187 radio_number = args.node
188 if args.node
is None:
190 int(input(
"Which radio is this? Enter '0' or '1'. Defaults to '0' ")
or 0)
194 radio.enableDynamicPayloads()
197 radio.enableAckPayload()
201 radio.setPALevel(RF24_PA_LOW)
204 radio.openWritingPipe(address[radio_number])
207 radio.openReadingPipe(1, address[
not radio_number])
216 if args.role
is None:
225 except KeyboardInterrupt:
226 print(
" Keyboard Interrupt detected. Exiting...")
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.