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
23if RF24_DRIVER ==
"MRAA":
25elif RF24_DRIVER ==
"wiringPi":
29radio =
RF24(CE_PIN, CSN_PIN)
33 raise RuntimeError(
"radio hardware is not responding")
37address = [b
"1Node", b
"2Node"]
42 int(input(
"Which radio is this? Enter '0' or '1'. Defaults to '0' ")
or 0)
46radio.enableDynamicPayloads()
49radio.enableAckPayload()
53radio.setPALevel(RF24_PA_LOW)
56radio.openWritingPipe(address[radio_number])
59radio.openReadingPipe(1, address[
not radio_number])
73 """Transmits a message and an incrementing integer every second."""
78 buffer = b
"Hello \x00" + bytes(counter)
81 start_timer = time.monotonic_ns()
82 result = radio.write(buffer)
83 end_timer = time.monotonic_ns()
86 decoded = buffer[:6].decode(
"utf-8")
88 "Transmission successful! Time to transmit:",
89 f
"{int((end_timer - start_timer) / 1000)} us.",
90 f
"Sent: {decoded}{counter[0]}",
93 has_payload, pipe_number = radio.available_pipe()
96 length = radio.getDynamicPayloadSize()
97 response = radio.read(length)
98 decoded = bytes(response[:6]).decode(
"utf-8")
100 f
"Received {length} on pipe {pipe_number}:",
101 f
"{decoded}{response[7:8][0]}",
104 if response[7:8][0] < 255:
105 counter[0] = response[7:8][0] + 1
109 print(
"Received an empty ACK packet")
112 print(
"Transmission failed or timed out")
114 print(failures,
"failures detected. Leaving TX role.")
117def slave(timeout: int = 6):
118 """Listen for any payloads and print the transaction
120 :param int timeout: The number of seconds to wait (with no transmission)
121 until exiting function.
123 radio.startListening()
126 buffer = b
"World \x00" + bytes(counter)
129 radio.writeAckPayload(1, buffer)
131 start_timer = time.monotonic()
132 while (time.monotonic() - start_timer) < timeout:
133 has_payload, pipe_number = radio.available_pipe()
135 length = radio.getDynamicPayloadSize()
136 received = radio.read(length)
138 counter[0] = received[7:8][0] + 1
if received[7:8][0] < 255
else 0
139 decoded = [bytes(received[:6]).decode(
"utf-8")]
140 decoded.append(buffer[:6].decode(
"utf-8"))
142 f
"Received {length} bytes on pipe {pipe_number}:",
143 f
"{decoded[0]}{received[7:8][0]}",
144 f
"Sent: {decoded[1]}{buffer[7:8][0]}",
146 buffer = b
"World \x00" + bytes(counter)
147 radio.writeAckPayload(1, buffer)
148 start_timer = time.monotonic()
150 print(
"Nothing received in", timeout,
"seconds. Leaving RX role")
152 radio.stopListening()
155def set_role() -> bool:
156 """Set the role using stdin stream. Timeout arg for slave() can be
157 specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
160 - True when role is complete & app should continue running.
161 - False when app should exit
165 "*** Enter 'R' for receiver role.\n"
166 "*** Enter 'T' for transmitter role.\n"
167 "*** Enter 'Q' to quit example.\n"
171 user_input = user_input.split()
172 if user_input[0].upper().startswith(
"R"):
173 if len(user_input) > 1:
174 slave(int(user_input[1]))
178 if user_input[0].upper().startswith(
"T"):
181 if user_input[0].upper().startswith(
"Q"):
184 print(user_input[0],
"is an unrecognized input. Please try again.")
188if __name__ ==
"__main__":
192 except KeyboardInterrupt:
193 print(
" Keyboard Interrupt detected. Exiting...")
196 print(
" Run slave() on receiver\n Run master() on transmitter")
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.