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)")
40if RF24_DRIVER ==
"MRAA":
42elif RF24_DRIVER ==
"wiringPi":
46radio =
RF24(CE_PIN, CSN_PIN)
53address = [b
"1Node", b
"2Node"]
61 int(input(
"Which radio is this? Enter '0' or '1'. Defaults to '0' ")
or 0)
66 raise OSError(
"nRF24L01 hardware isn't responding")
70radio.enableDynamicPayloads()
71radio.enableAckPayload()
75radio.setPALevel(RF24_PA_LOW)
78radio.openWritingPipe(address[radio_number])
81radio.openReadingPipe(1, address[
not radio_number])
93tx_payloads = (b
"Ping ", b
"Pong ", b
"Radio", b
"1FAIL")
94ack_payloads = (b
"Yak ", b
"Back", b
" ACK")
97def interrupt_handler():
98 """This function is called when IRQ pin is detected active LOW"""
99 print(
"\tIRQ pin went active LOW.")
100 tx_ds, tx_df, rx_dr = radio.whatHappened()
101 print(f
"\ttx_ds: {tx_ds}, tx_df: {tx_df}, rx_dr: {rx_dr}")
102 if pl_iterator[0] == 0:
103 print(
" 'data ready' event test", (
"passed" if rx_dr
else "failed"))
104 elif pl_iterator[0] == 1:
105 print(
" 'data sent' event test", (
"passed" if tx_ds
else "failed"))
106 elif pl_iterator[0] == 2:
107 print(
" 'data fail' event test", (
"passed" if tx_df
else "failed"))
111irq_line = gpiod.request_lines(
113 consumer=
"RF24_interrupt_py-example",
114 config={IRQ_PIN: gpiod.LineSettings(edge_detection=Edge.FALLING)},
118def _wait_for_irq(timeout: float = 5):
119 """Wait till IRQ_PIN goes active (LOW).
120 IRQ pin is LOW when activated. Otherwise it is always HIGH
123 if not irq_line.wait_edge_events(timeout):
124 print(f
"\tInterrupt event not detected for {timeout} seconds!")
127 for event
in irq_line.read_edge_events():
128 if event.line_offset == IRQ_PIN
and event.event_type
is event.Type.FALLING_EDGE:
134 """Transmits 4 times and reports results
136 1. successfully receive ACK payload first
137 2. successfully transmit on second
138 3. send a third payload to fill RX node's RX FIFO
139 (supposedly making RX node unresponsive)
140 4. intentionally fail transmit on the fourth
142 radio.stopListening()
145 print(
"\nConfiguring IRQ pin to only ignore 'on data sent' event")
146 radio.maskIRQ(
True,
False,
False)
147 print(
" Pinging slave node for an ACK payload...")
149 radio.startFastWrite(tx_payloads[0],
False)
154 print(
"\nConfiguring IRQ pin to only ignore 'on data ready' event")
155 radio.maskIRQ(
False,
False,
True)
156 print(
" Pinging slave node again...")
158 radio.startFastWrite(tx_payloads[1],
False)
163 print(
"\nSending one extra payload to fill RX FIFO on slave node.")
164 print(
"Disabling IRQ pin for all events.")
165 radio.maskIRQ(
True,
True,
True)
166 if radio.write(tx_payloads[2]):
167 print(
"Slave node should not be listening anymore.")
169 print(
"Slave node was unresponsive.")
172 print(
"\nConfiguring IRQ pin to go active for all events.")
173 radio.maskIRQ(
False,
False,
False)
174 print(
" Sending a ping to inactive slave node...")
177 radio.startFastWrite(tx_payloads[3],
False)
183 print(
"\nComplete RX FIFO:", radio.read(12))
187 """Only listen for 3 payload from the master node"""
191 print(
"\nDisabling IRQ pin for all events.")
192 radio.maskIRQ(
True,
True,
True)
194 radio.writeAckPayload(1, ack_payloads[0])
195 radio.writeAckPayload(1, ack_payloads[1])
196 radio.writeAckPayload(1, ack_payloads[2])
197 radio.startListening()
198 start_timer = time.monotonic()
199 while not radio.rxFifoFull()
and time.monotonic() - start_timer < timeout:
203 radio.stopListening()
204 if radio.available():
207 print(
"Complete RX FIFO:", radio.read(15))
211 """Set the role using stdin stream. Timeout arg for slave() can be
212 specified using a space delimiter (e.g. 'R 10' calls `slave(10)`)
215 - True when role is complete & app should continue running.
216 - False when app should exit
220 f
"Make sure the IRQ pin is connected to the GPIO{IRQ_PIN}\n"
221 "*** Enter 'R' for receiver role.\n"
222 "*** Enter 'T' for transmitter role.\n"
223 "*** Enter 'Q' to quit example.\n"
227 user_input = user_input.split()
228 if user_input[0].upper().startswith(
"R"):
229 slave(*[int(x)
for x
in user_input[1:2]])
231 if user_input[0].upper().startswith(
"T"):
234 if user_input[0].upper().startswith(
"Q"):
237 print(user_input[0],
"is an unrecognized input. Please try again.")
241if __name__ ==
"__main__":
245 except KeyboardInterrupt:
246 print(
" Keyboard Interrupt detected. Exiting...")
250 f
"Make sure the IRQ pin is connected to the GPIO{IRQ_PIN}",
251 "Run slave() on receiver",
252 "Run master() on transmitter",
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.