and follow the prompts.
2Simple example of detecting (and verifying) the IRQ (interrupt) pin on the
5See documentation at https://nRF24.github.io/RF24
9from RF24
import RF24, RF24_PA_LOW, RF24_DRIVER
13 from gpiod.line
import Edge
14except ImportError
as exc:
16 "This script requires gpiod installed for observing the IRQ pin. Please run\n"
17 "\n pip install gpiod\n\nMore details at https://pypi.org/project/gpiod/"
21 chip_path =
"/dev/gpiochip4"
22 chip = gpiod.Chip(chip_path)
23except FileNotFoundError:
24 chip_path =
"/dev/gpiochip0"
25 chip = gpiod.Chip(chip_path)
29 info = chip.get_info()
30 print(f
"Using {info.name} [{info.label}] ({info.num_lines} lines)")
41if RF24_DRIVER ==
"MRAA":
43elif RF24_DRIVER ==
"wiringPi":
47radio =
RF24(CE_PIN, CSN_PIN)
54address = [b
"1Node", b
"2Node"]
62 int(input(
"Which radio is this? Enter '0' or '1'. Defaults to '0' ")
or 0)
67 raise OSError(
"nRF24L01 hardware isn't responding")
71radio.enableDynamicPayloads()
72radio.enableAckPayload()
76radio.setPALevel(RF24_PA_LOW)
79radio.openWritingPipe(address[radio_number])
82radio.openReadingPipe(1, address[
not radio_number])
94tx_payloads = (b
"Ping ", b
"Pong ", b
"Radio", b
"1FAIL")
95ack_payloads = (b
"Yak ", b
"Back", b
" ACK")
98def interrupt_handler():
99 """This function is called when IRQ pin is detected active LOW"""
100 print(
"\tIRQ pin went active LOW.")
101 tx_ds, tx_df, rx_dr = radio.whatHappened()
102 print(f
"\ttx_ds: {tx_ds}, tx_df: {tx_df}, rx_dr: {rx_dr}")
103 if pl_iterator[0] == 0:
104 print(
" 'data ready' event test", (
"passed" if rx_dr
else "failed"))
105 elif pl_iterator[0] == 1:
106 print(
" 'data sent' event test", (
"passed" if tx_ds
else "failed"))
107 elif pl_iterator[0] == 2:
108 print(
" 'data fail' event test", (
"passed" if tx_df
else "failed"))
112irq_line = gpiod.request_lines(
114 consumer=
"RF24_interrupt_py-example",
115 config={IRQ_PIN: gpiod.LineSettings(edge_detection=Edge.FALLING)},
119def _wait_for_irq(timeout: float = 5):
120 """Wait till IRQ_PIN goes active (LOW).
121 IRQ pin is LOW when activated. Otherwise it is always HIGH
124 if not irq_line.wait_edge_events(timeout):
125 print(f
"\tInterrupt event not detected for {timeout} seconds!")
128 for event
in irq_line.read_edge_events():
129 if event.line_offset == IRQ_PIN
and event.event_type
is event.Type.FALLING_EDGE:
135 """Transmits 4 times and reports results
137 1. successfully receive ACK payload first
138 2. successfully transmit on second
139 3. send a third payload to fill RX node's RX FIFO
140 (supposedly making RX node unresponsive)
141 4. intentionally fail transmit on the fourth
143 radio.stopListening()
146 print(
"\nConfiguring IRQ pin to only ignore 'on data sent' event")
147 radio.maskIRQ(
True,
False,
False)
148 print(
" Pinging slave node for an ACK payload...")
150 radio.startFastWrite(tx_payloads[0],
False)
155 print(
"\nConfiguring IRQ pin to only ignore 'on data ready' event")
156 radio.maskIRQ(
False,
False,
True)
157 print(
" Pinging slave node again...")
159 radio.startFastWrite(tx_payloads[1],
False)
164 print(
"\nSending one extra payload to fill RX FIFO on slave node.")
165 print(
"Disabling IRQ pin for all events.")
166 radio.maskIRQ(
True,
True,
True)
167 if radio.write(tx_payloads[2]):
168 print(
"Slave node should not be listening anymore.")
170 print(
"Slave node was unresponsive.")
173 print(
"\nConfiguring IRQ pin to go active for all events.")
174 radio.maskIRQ(
False,
False,
False)
175 print(
" Sending a ping to inactive slave node...")
178 radio.startFastWrite(tx_payloads[3],
False)
184 print(
"\nComplete RX FIFO:", radio.read(12))
188 """Only listen for 3 payload from the master node"""
192 print(
"\nDisabling IRQ pin for all events.")
193 radio.maskIRQ(
True,
True,
True)
195 radio.writeAckPayload(1, ack_payloads[0])
196 radio.writeAckPayload(1, ack_payloads[1])
197 radio.writeAckPayload(1, ack_payloads[2])
198 radio.startListening()
199 start_timer = time.monotonic()
200 while not radio.rxFifoFull()
and time.monotonic() - start_timer < timeout:
204 radio.stopListening()
205 if radio.available():
208 print(
"Complete RX FIFO:", radio.read(15))
212 """Set the role using stdin stream. Timeout arg for slave() can be
213 specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
216 - True when role is complete & app should continue running.
217 - False when app should exit
221 f
"Make sure the IRQ pin is connected to the GPIO{IRQ_PIN}\n"
222 "*** Enter 'R' for receiver role.\n"
223 "*** Enter 'T' for transmitter role.\n"
224 "*** Enter 'Q' to quit example.\n"
228 user_input = user_input.split()
229 if user_input[0].upper().startswith(
"R"):
230 slave(*[int(x)
for x
in user_input[1:2]])
232 if user_input[0].upper().startswith(
"T"):
235 if user_input[0].upper().startswith(
"Q"):
238 print(user_input[0],
"is an unrecognized input. Please try again.")
242if __name__ ==
"__main__":
246 except KeyboardInterrupt:
247 print(
" Keyboard Interrupt detected. Exiting...")
251 f
"Make sure the IRQ pin is connected to the GPIO{IRQ_PIN}",
252 "Run slave() on receiver",
253 "Run master() on transmitter",
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.