nRF24L01 Features¶
Simple test¶
Ensure your device works with this simple test.
import time
import struct
import board
import digitalio
# if running this on a ATSAMD21 M0 based board
# from circuitpython_nrf24l01.rf24_lite import RF24
from circuitpython_nrf24l01.rf24 import RF24
# change these (digital output) pins accordingly
ce = digitalio.DigitalInOut(board.D4)
csn = digitalio.DigitalInOut(board.D5)
# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI() # init spi bus object
# we'll be using the dynamic payload size feature (enabled by default)
# initialize the nRF24L01 on the spi bus object
nrf = RF24(spi, csn, ce)
# set the Power Amplifier level to -12 dBm since this test example is
# usually run with nRF24L01 transceivers in close proximity
nrf.pa_level = -12
# addresses needs to be in a buffer protocol object (bytearray)
address = [b"1Node", b"2Node"]
# to use different addresses on a pair of radios, we need a variable to
# uniquely identify which address this radio will use to transmit
# 0 uses address[0] to transmit, 1 uses address[1] to transmit
radio_number = bool(
int(input("Which radio is this? Enter '0' or '1'. Defaults to '0' ") or 0)
)
# set TX address of RX node into the TX pipe
nrf.open_tx_pipe(address[radio_number]) # always uses pipe 0
# set RX address of TX node into an RX pipe
nrf.open_rx_pipe(1, address[not radio_number]) # using pipe 1
# using the python keyword global is bad practice. Instead we'll use a 1 item
# list to store our float number for the payloads sent
payload = [0.0]
# uncomment the following 3 lines for compatibility with TMRh20 library
# nrf.allow_ask_no_ack = False
# nrf.dynamic_payloads = False
# nrf.payload_length = 4
def master(count=5): # count = 5 will only transmit 5 packets
"""Transmits an incrementing integer every second"""
nrf.listen = False # ensures the nRF24L01 is in TX mode
while count:
# use struct.pack to packetize your data
# into a usable payload
buffer = struct.pack("<f", payload[0])
# "<f" means a single little endian (4 byte) float value.
start_timer = time.monotonic_ns() # start timer
result = nrf.send(buffer)
end_timer = time.monotonic_ns() # end timer
if not result:
print("send() failed or timed out")
else:
print(
"Transmission successful! Time to Transmit: "
"{} us. Sent: {}".format(
(end_timer - start_timer) / 1000, payload[0]
)
)
payload[0] += 0.01
time.sleep(1)
count -= 1
def slave(timeout=6):
"""Polls the radio and prints the received value. This method expires
after 6 seconds of no received transmission"""
nrf.listen = True # put radio into RX mode and power up
start = time.monotonic()
while (time.monotonic() - start) < timeout:
if nrf.available():
# grab information about the received payload
payload_size, pipe_number = (nrf.any(), nrf.pipe)
# fetch 1 payload from RX FIFO
buffer = nrf.read() # also clears nrf.irq_dr status flag
# expecting a little endian float, thus the format string "<f"
# buffer[:4] truncates padded 0s if dynamic payloads are disabled
payload[0] = struct.unpack("<f", buffer[:4])[0]
# print details about the received packet
print(
"Received {} bytes on pipe {}: {}".format(
payload_size, pipe_number, payload[0]
)
)
start = time.monotonic()
# recommended behavior is to keep in TX mode while idle
nrf.listen = False # put the nRF24L01 is in TX mode
ACK Payloads Example¶
-
Changed in version 2.0.0:
uses 2 addresses on pipes 1 & 0 to demonstrate proper addressing convention.
changed payloads to show use of c-strings’ NULL terminating character.
This is a test to show how to use custom acknowledgment payloads.
See also
More details are found in the documentation on ack
and load_ack()
.
import time
import board
import digitalio
# if running this on a ATSAMD21 M0 based board
# from circuitpython_nrf24l01.rf24_lite import RF24
from circuitpython_nrf24l01.rf24 import RF24
# change these (digital output) pins accordingly
ce = digitalio.DigitalInOut(board.D4)
csn = digitalio.DigitalInOut(board.D5)
# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI() # init spi bus object
# we'll be using the dynamic payload size feature (enabled by default)
# the custom ACK payload feature is disabled by default
# the custom ACK payload feature should not be enabled
# during instantiation due to its singular use nature
# meaning 1 ACK payload per 1 RX'd payload
nrf = RF24(spi, csn, ce)
# NOTE the the custom ACK payload feature will be enabled
# automatically when you call load_ack() passing:
# a buffer protocol object (bytearray) of
# length ranging [1,32]. And pipe number always needs
# to be an int ranging [0, 5]
# to enable the custom ACK payload feature
nrf.ack = True # False disables again
# set the Power Amplifier level to -12 dBm since this test example is
# usually run with nRF24L01 transceivers in close proximity
nrf.pa_level = -12
# addresses needs to be in a buffer protocol object (bytearray)
address = [b"1Node", b"2Node"]
# to use different addresses on a pair of radios, we need a variable to
# uniquely identify which address this radio will use to transmit
# 0 uses address[0] to transmit, 1 uses address[1] to transmit
radio_number = bool(
int(input("Which radio is this? Enter '0' or '1'. Defaults to '0' ") or 0)
)
# set TX address of RX node into the TX pipe
nrf.open_tx_pipe(address[radio_number]) # always uses pipe 0
# set RX address of TX node into an RX pipe
nrf.open_rx_pipe(1, address[not radio_number]) # using pipe 1
# using the python keyword global is bad practice. Instead we'll use a 1 item
# list to store our integer number for the payloads' counter
counter = [0]
def master(count=5): # count = 5 will only transmit 5 packets
"""Transmits a payload every second and prints the ACK payload"""
nrf.listen = False # put radio in TX mode
while count:
# construct a payload to send
# add b"\0" as a c-string NULL terminating char
buffer = b"Hello \0" + bytes([counter[0]])
start_timer = time.monotonic_ns() # start timer
result = nrf.send(buffer) # save the response (ACK payload)
end_timer = time.monotonic_ns() # stop timer
if result:
# print the received ACK that was automatically
# fetched and saved to "result" via send()
# print timer results upon transmission success
print(
"Transmission successful! Time to transmit: "
"{} us. Sent: {}{}".format(
int((end_timer - start_timer) / 1000),
buffer[:6].decode("utf-8"),
counter[0],
),
end=" ",
)
if isinstance(result, bool):
print(" Received an empty ACK packet")
else:
# result[:6] truncates c-string NULL termiating char
# received counter is a unsigned byte, thus result[7:8][0]
print(
" Received: {}{}".format(
bytes(result[:6]).decode("utf-8"), result[7:8][0]
)
)
counter[0] += 1 # increment payload counter
elif not result:
print("send() failed or timed out")
time.sleep(1) # let the RX node prepare a new ACK payload
count -= 1
def slave(timeout=6):
"""Prints the received value and sends an ACK payload"""
nrf.listen = True # put radio into RX mode, power it up
# setup the first transmission's ACK payload
# add b"\0" as a c-string NULL terminating char
buffer = b"World \0" + bytes([counter[0]])
# we must set the ACK payload data and corresponding
# pipe number [0, 5]. We'll be acknowledging pipe 1
nrf.load_ack(buffer, 1) # load ACK for first response
start = time.monotonic() # start timer
while (time.monotonic() - start) < timeout:
if nrf.available():
# grab information about the received payload
length, pipe_number = (nrf.any(), nrf.pipe)
# retreive the received packet's payload
received = nrf.read()
# increment counter from received payload
# received counter is a unsigned byte, thus result[7:8][0]
counter[0] = received[7:8][0] + 1
# the [:6] truncates the c-string NULL termiating char
print(
"Received {} bytes on pipe {}: {}{} Sent: {}{}".format(
length,
pipe_number,
bytes(received[:6]).decode("utf-8"),
received[7:8][0],
bytes(buffer[:6]).decode("utf-8"),
buffer[7:8][0],
)
)
start = time.monotonic() # reset timer
buffer = b"World \0" + bytes([counter[0]]) # build new ACK
nrf.load_ack(buffer, 1) # load ACK for next response
# recommended behavior is to keep in TX mode while idle
nrf.listen = False # put radio in TX mode
nrf.flush_tx() # flush any ACK payloads that remain
Multiceiver Example¶
New in version 1.2.2.
Changed in version 2.0.0: no longer uses ACK payloads for responding to node 1.
This example shows how use a group of 6 nRF24L01 transceivers to transmit to 1 nRF24L01 transceiver. This technique is called “Multiceiver” in the nRF24L01 Specifications Sheet
Note
This example follows the diagram illistrated in
figure 12 of section 7.7 of the nRF24L01 Specifications Sheet
Please note that if auto_ack
(on the base station) and arc
(on the
transmitting nodes) are disabled, then
figure 10 of section 7.7 of the nRF24L01 Specifications Sheet
would be a better illustration.
Hint
A paraphrased note from the the nRF24L01 Specifications Sheet:
Only when a data pipe receives a complete packet can other data pipes begin to receive data. When multiple [nRF24L01]s are transmitting to [one nRF24L01], the
ard
can be used to skew the auto retransmission so that they only block each other once.
This basically means that it might help packets get received if the ard
attribute
is set to various values among multiple transmitting nRF24L01 transceivers.
import time
import struct
import board
import digitalio
# if running this on a ATSAMD21 M0 based board
# from circuitpython_nrf24l01.rf24_lite import RF24
from circuitpython_nrf24l01.rf24 import RF24
# change these (digital output) pins accordingly
ce = digitalio.DigitalInOut(board.D4)
csn = digitalio.DigitalInOut(board.D5)
# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI() # init spi bus object
# we'll be using the dynamic payload size feature (enabled by default)
# initialize the nRF24L01 on the spi bus object
nrf = RF24(spi, csn, ce)
# set the Power Amplifier level to -12 dBm since this test example is
# usually run with nRF24L01 transceivers in close proximity
nrf.pa_level = -12
# setup the addresses for all transmitting nRF24L01 nodes
addresses = [
b"\x78" * 5,
b"\xF1\xB6\xB5\xB4\xB3",
b"\xCD\xB6\xB5\xB4\xB3",
b"\xA3\xB6\xB5\xB4\xB3",
b"\x0F\xB6\xB5\xB4\xB3",
b"\x05\xB6\xB5\xB4\xB3",
]
# uncomment the following 3 lines for compatibility with TMRh20 library
# nrf.allow_ask_no_ack = False
# nrf.dynamic_payloads = False
# nrf.payload_length = 8
def base(timeout=10):
"""Use the nRF24L01 as a base station for lisening to all nodes"""
# write the addresses to all pipes.
for pipe_n, addr in enumerate(addresses):
nrf.open_rx_pipe(pipe_n, addr)
nrf.listen = True # put base station into RX mode
start_timer = time.monotonic() # start timer
while time.monotonic() - start_timer < timeout:
while not nrf.fifo(False, True): # keep RX FIFO empty for reception
# show the pipe number that received the payload
# NOTE read() clears the pipe number and payload length data
print("Received", nrf.any(), "on pipe", nrf.pipe, end=" ")
node_id, payload_id = struct.unpack("<ii", nrf.read())
print("from node {}. PayloadID: {}".format(node_id, payload_id))
start_timer = time.monotonic() # reset timer with every payload
nrf.listen = False
def node(node_number=0, count=6):
"""start transmitting to the base station.
:param int node_number: the node's identifying index (from the
the `addresses` list)
:param int count: the number of times that the node will transmit
to the base station.
"""
nrf.listen = False
# set the TX address to the address of the base station.
nrf.open_tx_pipe(addresses[node_number])
counter = 0
# use the node_number to identify where the payload came from
while counter < count:
counter += 1
# payloads will include the node_number and a payload ID character
payload = struct.pack("<ii", node_number, counter)
# show something to see it isn't frozen
start_timer = time.monotonic_ns()
report = nrf.send(payload)
end_timer = time.monotonic_ns()
# show something to see it isn't frozen
if report:
print(
"Transmission of payloadID {} as node {} successfull! "
"Transmission time: {} us".format(
counter, node_number, (end_timer - start_timer) / 1000
)
)
else:
print("Transmission failed or timed out")
time.sleep(0.5) # slow down the test for readability
Scanner Example¶
New in version 2.0.0.
This example simply scans the entire RF frquency (2.4 GHz to 2.525 GHz)
and outputs a vertical graph of how many signals (per
channel
) were detected. This example
can be used to find a frequency with the least ambient interference from other
radio-emitting sources (i.e. WiFi, Bluetooth, or etc).
import time
import board
import digitalio
# if running this on a ATSAMD21 M0 based board
# from circuitpython_nrf24l01.rf24_lite import RF24
from circuitpython_nrf24l01.rf24 import RF24
# change these (digital output) pins accordingly
ce = digitalio.DigitalInOut(board.D4)
csn = digitalio.DigitalInOut(board.D5)
# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI() # init spi bus object
# we'll be using the dynamic payload size feature (enabled by default)
# initialize the nRF24L01 on the spi bus object
nrf = RF24(spi, csn, ce)
# turn off RX features specific to the nRF24L01 module
nrf.auto_ack = 0
nrf.dynamic_payloads = 0
def scan(timeout=30):
"""Traverse the spectrum of accessible frequencies and print any detection
of ambient signals.
:param int timeout: The number of seconds in which scanning is performed.
"""
# print the vertical header of channel numbers
print("0" * 100 + "1" * 26)
for i in range(13):
print(str(i % 10) * (10 if i < 12 else 6), sep="", end="")
print("") # endl
for i in range(126):
print(str(i % 10), sep="", end="")
print("\n" + "~" * 126)
signals = [0] * 126 # store the signal count for each channel
start_timer = time.monotonic() # start the timer
while time.monotonic() - start_timer < timeout:
for curr_channel in range(126): # for each channel
nrf.channel = curr_channel
time.sleep(0.00013) # let radio modulate to new channel
nrf.listen = 1 # start a RX session
time.sleep(0.00013) # wait 130 microseconds
signals[curr_channel] += nrf.rpd # if interference is present
nrf.listen = 0 # end the RX session
# ouptut the signal counts per channel
print(
hex(min(0x0F, signals[curr_channel]))[2:]
if signals[curr_channel]
else "-",
sep="",
end="" if curr_channel < 125 else "\r",
)
# print results 1 last time to end with a new line
for sig in signals:
print(hex(min(0x0F, sig))[2:] if sig else "-", sep="", end="")
print("")
Reading the scanner output¶
Hint
Make sure the terminal window used to run the scanner example is expanded to fit 125 characters. Otherwise the output will look weird.
The output of the scanner example is supposed to be read vertically (as columns). So, the following
000111789~~~13-
should be interpreted as
1
signal detected on channel017
3
signals detected on channel018
no signal (
-
) detected on channel019
The ~
is just a divider between the vertical header and the signal counts.
IRQ Pin Example¶
Changed in version 1.2.0: uses ACK payloads to trigger all 3 IRQ events.
Changed in version 2.0.0: uses 2 addresses on pipes 1 & 0 to demonstrate proper addressing convention.
This is a test to show how to use nRF24L01’s interrupt pin using the non-blocking
write()
. Also the ack
attribute is enabled to trigger the irq_dr
event when
the master node receives ACK payloads. Simply put, this example is the most advanced
example script (in this library), and it runs very quickly.
import time
import board
import digitalio
# if running this on a ATSAMD21 M0 based board
# from circuitpython_nrf24l01.rf24_lite import RF24
from circuitpython_nrf24l01.rf24 import RF24
# select your digital input pin that's connected to the IRQ pin on the nRF4L01
irq_pin = digitalio.DigitalInOut(board.D12)
irq_pin.switch_to_input() # make sure its an input object
# change these (digital output) pins accordingly
ce = digitalio.DigitalInOut(board.D4)
csn = digitalio.DigitalInOut(board.D5)
# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI() # init spi bus object
# we'll be using the dynamic payload size feature (enabled by default)
# initialize the nRF24L01 on the spi bus object
nrf = RF24(spi, csn, ce)
# this example uses the ACK payload to trigger the IRQ pin active for
# the "on data received" event
nrf.ack = True # enable ACK payloads
# set the Power Amplifier level to -12 dBm since this test example is
# usually run with nRF24L01 transceivers in close proximity
nrf.pa_level = -12
# address needs to be in a buffer protocol object (bytearray is preferred)
address = [b"1Node", b"2Node"]
# to use different addresses on a pair of radios, we need a variable to
# uniquely identify which address this radio will use to transmit
# 0 uses address[0] to transmit, 1 uses address[1] to transmit
radio_number = bool(
int(input("Which radio is this? Enter '0' or '1'. Defaults to '0' ") or 0)
)
# set TX address of RX node into the TX pipe
nrf.open_tx_pipe(address[radio_number]) # always uses pipe 0
# set RX address of TX node into an RX pipe
nrf.open_rx_pipe(1, address[not radio_number]) # using pipe 1
def _ping_and_prompt():
"""transmit 1 payload, wait till irq_pin goes active, print IRQ status
flags."""
ce.value = 1 # tell the nRF24L01 to prepare sending a single packet
time.sleep(0.00001) # mandatory 10 microsecond pulse starts transmission
ce.value = 0 # end 10 us pulse; use only 1 buffer from TX FIFO
while irq_pin.value: # IRQ pin is active when LOW
pass
print("IRQ pin went active LOW.")
nrf.update() # update irq_d? status flags
print(
"\tirq_ds: {}, irq_dr: {}, irq_df: {}".format(
nrf.irq_ds, nrf.irq_dr, nrf.irq_df
)
)
def master():
"""Transmits 3 times: successfully receive ACK payload first, successfully
transmit on second, and intentionally fail transmit on the third"""
nrf.listen = False # ensures the nRF24L01 is in TX mode
# NOTE nrf.write() internally calls nrf.clear_status_flags() first
# load 2 buffers into the TX FIFO; write_only=True leaves CE pin LOW
nrf.write(b"Ping ", write_only=True)
nrf.write(b"Pong ", write_only=True)
# on data ready test
print("\nConfiguring IRQ pin to only ignore 'on data sent' event")
nrf.interrupt_config(data_sent=False)
print(" Pinging slave node for an ACK payload...", end=" ")
_ping_and_prompt() # CE pin is managed by this function
print(
"\t'on data ready' event test{}successful".format(
" " if nrf.irq_dr else " un"
)
)
# on data sent test
print("\nConfiguring IRQ pin to only ignore 'on data ready' event")
nrf.interrupt_config(data_recv=False)
print(" Pinging slave node again... ", end=" ")
_ping_and_prompt() # CE pin is managed by this function
print(
"\t'on data sent' event test{}successful".format(
" " if nrf.irq_ds else " un"
)
)
# trigger slave node to exit by filling the slave node's RX FIFO
print("\nSending one extra payload to fill RX FIFO on slave node.")
if nrf.send(b"Radio", send_only=True):
# when send_only parameter is True, send() ignores RX FIFO usage
if nrf.fifo(False, False): # is RX FIFO full?
print("Slave node should not be listening anymore.")
else:
print(
"transmission succeeded, "
"but slave node might still be listening"
)
else:
print("Slave node was unresponsive.")
# on data fail test
print("\nConfiguring IRQ pin to go active for all events.")
nrf.interrupt_config()
print(" Sending a ping to inactive slave node...", end=" ")
nrf.flush_tx() # just in case any previous tests failed
nrf.write(b"Dummy", write_only=True) # CE pin is left LOW
_ping_and_prompt() # CE pin is managed by this function
print(
"\t'on data failed' event test{}successful".format(
" " if nrf.irq_df else " un"
)
)
nrf.flush_tx() # flush artifact payload in TX FIFO from last test
# all 3 ACK payloads received were 4 bytes each, and RX FIFO is full
# so, fetching 12 bytes from the RX FIFO also flushes RX FIFO
print("\nComplete RX FIFO:", nrf.read(12))
def slave(timeout=6): # will listen for 6 seconds before timing out
"""Only listen for 3 payload from the master node"""
# setup radio to recieve pings, fill TX FIFO with ACK payloads
nrf.load_ack(b"Yak ", 1)
nrf.load_ack(b"Back", 1)
nrf.load_ack(b" ACK", 1)
nrf.listen = True # start listening & clear irq_dr flag
start_timer = time.monotonic() # start timer now
while not nrf.fifo(0, 0) and time.monotonic() - start_timer < timeout:
# if RX FIFO is not full and timeout is not reached, then keep going
pass
nrf.listen = False # put nRF24L01 in Standby-I mode when idling
if not nrf.fifo(False, True): # if RX FIFO is not empty
# all 3 payloads received were 5 bytes each, and RX FIFO is full
# so, fetching 15 bytes from the RX FIFO also flushes RX FIFO
print("Complete RX FIFO:", nrf.read(15))
nrf.flush_tx() # discard any pending ACK payloads
Library-Specific Features¶
Stream Example¶
Changed in version 1.2.3: added master_fifo()
to demonstrate using full TX FIFO to stream data.
Changed in version 2.0.0: uses 2 addresses on pipes 1 & 0 to demonstrate proper addressing convention.
This is a test to show how to stream data. The master()
uses the send()
function to transmit multiple payloads with 1 function call. However
master()
only uses 1 level of the nRF24L01’s TX FIFO. An alternate function,
called master_fifo()
uses all 3 levels of the nRF24L01’s TX FIFO to stream
data, but it uses the write()
function to do so.
import time
import board
import digitalio
# if running this on a ATSAMD21 M0 based board
# from circuitpython_nrf24l01.rf24_lite import RF24
from circuitpython_nrf24l01.rf24 import RF24
# change these (digital output) pins accordingly
ce = digitalio.DigitalInOut(board.D4)
csn = digitalio.DigitalInOut(board.D5)
# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI() # init spi bus object
# we'll be using the dynamic payload size feature (enabled by default)
# initialize the nRF24L01 on the spi bus object
nrf = RF24(spi, csn, ce)
# set the Power Amplifier level to -12 dBm since this test example is
# usually run with nRF24L01 transceivers in close proximity
nrf.pa_level = -12
# addresses needs to be in a buffer protocol object (bytearray)
address = [b"1Node", b"2Node"]
# to use different addresses on a pair of radios, we need a variable to
# uniquely identify which address this radio will use to transmit
# 0 uses address[0] to transmit, 1 uses address[1] to transmit
radio_number = bool(
int(input("Which radio is this? Enter '0' or '1'. Defaults to '0' ") or 0)
)
# set TX address of RX node into the TX pipe
nrf.open_tx_pipe(address[radio_number]) # always uses pipe 0
# set RX address of TX node into an RX pipe
nrf.open_rx_pipe(1, address[not radio_number]) # using pipe 1
# uncomment the following 2 lines for compatibility with TMRh20 library
# nrf.allow_ask_no_ack = False
# nrf.dynamic_payloads = False
def make_buffers(size=32):
"""return a list of payloads"""
buffers = []
# we'll use `size` for the number of payloads in the list and the
# payloads' length
for i in range(size):
# prefix payload with a sequential letter to indicate which
# payloads were lost (if any)
buff = bytes([i + (65 if 0 <= i < 26 else 71)])
for j in range(size - 1):
char = j >= (size - 1) / 2 + abs((size - 1) / 2 - i)
char |= j < (size - 1) / 2 - abs((size - 1) / 2 - i)
buff += bytes([char + 48])
buffers.append(buff)
del buff
return buffers
def master(count=1, size=32): # count = 5 will transmit the list 5 times
"""Transmits multiple payloads using `RF24.send()` and `RF24.resend()`."""
buffers = make_buffers(size) # make a list of payloads
nrf.listen = False # ensures the nRF24L01 is in TX mode
successful = 0 # keep track of success rate
for _ in range(count):
start_timer = time.monotonic_ns() # start timer
# NOTE force_retry=2 internally invokes `RF24.resend()` 2 times at
# most for payloads that fail to transmit.
result = nrf.send(buffers, force_retry=2) # result is a list
end_timer = time.monotonic_ns() # end timer
print("Transmission took", (end_timer - start_timer) / 1000, "us")
for r in result: # tally the resulting success rate
successful += 1 if r else 0
print(
"successfully sent {}% ({}/{})".format(
successful / (size * count) * 100, successful, size * count
)
)
def master_fifo(count=1, size=32):
"""Similar to the `master()` above except this function uses the full
TX FIFO and `RF24.write()` instead of `RF24.send()`"""
if size < 6:
print("setting size to 6;", size, "is not allowed for this test.")
size = 6
buf = make_buffers(size) # make a list of payloads
nrf.listen = False # ensures the nRF24L01 is in TX mode
for c in range(count): # transmit the same payloads this many times
nrf.flush_tx() # clear the TX FIFO so we can use all 3 levels
# NOTE the write_only parameter does not initiate sending
buf_iter = 0 # iterator of payloads for the while loop
failures = 0 # keep track of manual retries
start_timer = time.monotonic_ns() # start timer
while buf_iter < size: # cycle through all the payloads
while buf_iter < size and nrf.write(buf[buf_iter], write_only=1):
# NOTE write() returns False if TX FIFO is already full
buf_iter += 1 # increment iterator of payloads
ce.value = True # start tranmission (after 10 microseconds)
while not nrf.fifo(True, True): # updates irq_df flag
if nrf.irq_df:
# reception failed; we need to reset the irq_rf flag
ce.value = False # fall back to Standby-I mode
failures += 1 # increment manual retries
if failures > 99 and buf_iter < 7 and c < 2:
# we need to prevent an infinite loop
print(
"Make sure slave() node is listening."
" Quiting master_fifo()"
)
buf_iter = size + 1 # be sure to exit the while loop
nrf.flush_tx() # discard all payloads in TX FIFO
break
nrf.clear_status_flags() # clear the irq_df flag
ce.value = True # start re-transmitting
ce.value = False
end_timer = time.monotonic_ns() # end timer
print(
"Transmission took {} us with {} failures detected.".format(
(end_timer - start_timer) / 1000, failures
),
end=" " if failures < 100 else "\n",
)
if 1 <= failures < 100:
print(
"\n\nHINT: Try playing with the 'ard' and 'arc' attributes to"
" reduce the number of\nfailures detected. Tests were better"
" with these attributes at higher values, but\nnotice the "
"transmission time differences."
)
elif not failures:
print("You Win!")
def slave(timeout=5):
"""Stops listening after a `timeout` with no response"""
nrf.listen = True # put radio into RX mode and power up
count = 0 # keep track of the number of received payloads
start_timer = time.monotonic() # start timer
while time.monotonic() < start_timer + timeout:
if nrf.available():
count += 1
# retreive the received packet's payload
buffer = nrf.read() # clears flags & empties RX FIFO
print("Received: {} - {}".format(buffer, count))
start_timer = time.monotonic() # reset timer on every RX payload
# recommended behavior is to keep in TX mode while idle
nrf.listen = False # put the nRF24L01 is in TX mode
Context Example¶
Changed in version 1.2.0: demonstrates switching between FakeBLE
object & RF24
object with the same nRF24L01
This is a test to show how to use The with statement
blocks to manage multiple different nRF24L01 configurations on 1 transceiver.
import board
import digitalio
from circuitpython_nrf24l01.rf24 import RF24
from circuitpython_nrf24l01.fake_ble import FakeBLE
# change these (digital output) pins accordingly
ce = digitalio.DigitalInOut(board.D4)
csn = digitalio.DigitalInOut(board.D5)
# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI() # init spi bus object
# initialize the nRF24L01 objects on the spi bus object
# the first object will have all the features enabled
nrf = RF24(spi, csn, ce)
# enable the option to use custom ACK payloads
nrf.ack = True
# set the static payload length to 8 bytes
nrf.payload_length = 8
# RF power amplifier is set to -18 dbm
nrf.pa_level = -18
# the second object has most features disabled/altered
ble = FakeBLE(spi, csn, ce)
# the IRQ pin is configured to only go active on "data fail"
# NOTE BLE operations prevent the IRQ pin going active on "data fail" events
ble.interrupt_config(data_recv=False, data_sent=False)
# using a channel 2
ble.channel = 2
# RF power amplifier is set to -12 dbm
ble.pa_level = -12
print("\nsettings configured by the nrf object")
with nrf:
# only the first character gets written because it is on a pipe_number > 1
nrf.open_rx_pipe(5, b"1Node") # NOTE we do this inside the "with" block
# display current settings of the nrf object
nrf.print_details(True) # True dumps pipe info
print("\nsettings configured by the ble object")
with ble as nerf: # the "as nerf" part is optional
nerf.print_details(1)
# if you examine the outputs from print_details() you'll see:
# pipe 5 is opened using the nrf object, but closed using the ble object.
# pipe 0 is closed using the nrf object, but opened using the ble object.
# also notice the different addresses bound to the RX pipes
# this is because the "with" statements load the existing settings
# for the RF24 object specified after the word "with".
# NOTE it is not advised to manipulate seperate RF24 objects outside of the
# "with" block; you will encounter bugs about configurations when doing so.
# Be sure to use 1 "with" block per RF24 object when instantiating multiple
# RF24 objects in your program.
# NOTE exiting a "with" block will always power down the nRF24L01
# NOTE upon instantiation, this library closes all RX pipes &
# extracts the TX/RX addresses from the nRF24L01 registers
Manual ACK Example¶
New in version 2.0.0: Previously, this example was strictly made for TMRh20’s RF24 library example titled “GettingStarted_HandlingData.ino”. With the latest addition of new examples to the TMRh20 RF24 library, this example was renamed from “nrf24l01_2arduino_handling_data.py” and adapted for both this library and TMRh20’s RF24 library.
This is a test to show how to use the library for acknowledgement (ACK) responses without using the automatic ACK packets (like the ACK Payloads Example does). Beware, that this technique is not faster and can be more prone to communication failure. However, This technique has the advantage of using more updated information in the responding payload as information in ACK payloads are always outdated by 1 transmission.
import time
import board
import digitalio
# if running this on a ATSAMD21 M0 based board
# from circuitpython_nrf24l01.rf24_lite import RF24
from circuitpython_nrf24l01.rf24 import RF24
# change these (digital output) pins accordingly
ce = digitalio.DigitalInOut(board.D4)
csn = digitalio.DigitalInOut(board.D5)
# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI() # init spi bus object
# initialize the nRF24L01 on the spi bus object
nrf = RF24(spi, csn, ce)
# set the Power Amplifier level to -12 dBm since this test example is
# usually run with nRF24L01 transceivers in close proximity
nrf.pa_level = -12
# addresses needs to be in a buffer protocol object (bytearray)
address = [b"1Node", b"2Node"]
# to use different addresses on a pair of radios, we need a variable to
# uniquely identify which address this radio will use to transmit
# 0 uses address[0] to transmit, 1 uses address[1] to transmit
radio_number = bool(
int(input("Which radio is this? Enter '0' or '1'. Defaults to '0' ") or 0)
)
# set TX address of RX node into the TX pipe
nrf.open_tx_pipe(address[radio_number]) # always uses pipe 0
# set RX address of TX node into an RX pipe
nrf.open_rx_pipe(1, address[not radio_number]) # using pipe 1
# nrf.open_rx_pipe(2, address[radio_number]) # for getting responses on pipe 2
# using the python keyword global is bad practice. Instead we'll use a 1 item
# list to store our integer number for the payloads' counter
counter = [0]
# uncomment the following 3 lines for compatibility with TMRh20 library
# nrf.allow_ask_no_ack = False
# nrf.dynamic_payloads = False
# nrf.payload_length = 8
def master(count=5): # count = 5 will only transmit 5 packets
"""Transmits an arbitrary unsigned long value every second"""
nrf.listen = False # ensures the nRF24L01 is in TX mode
while count:
# construct a payload to send
# add b"\0" as a c-string NULL terminating char
buffer = b"Hello \0" + bytes([counter[0]])
start_timer = time.monotonic_ns() # start timer
result = nrf.send(buffer) # save the response (ACK payload)
if not result:
print("send() failed or timed out")
else: # sent successful; listen for a response
nrf.listen = True # get radio ready to receive a response
timeout = time.monotonic_ns() + 200000000 # set sentinal for timeout
while not nrf.available() and time.monotonic_ns() < timeout:
# this loop hangs for 200 ms or until response is received
pass
nrf.listen = False # put the radio back in TX mode
end_timer = time.monotonic_ns() # stop timer
print(
"Transmission successful! Time to transmit: "
"{} us. Sent: {}{}".format(
int((end_timer - start_timer) / 1000),
buffer[:6].decode("utf-8"),
counter[0],
),
end=" ",
)
if nrf.pipe is None: # is there a payload?
# nrf.pipe is also updated using `nrf.listen = False`
print("Received no response.")
else:
length = nrf.any() # reset with read()
pipe_number = nrf.pipe # reset with read()
received = nrf.read() # grab the response
# save new counter from response
counter[0] = received[7:8][0]
print(
"Receieved {} bytes with pipe {}: {}{}".format(
length,
pipe_number,
bytes(received[:6]).decode("utf-8"), # convert to str
counter[0],
)
)
count -= 1
# make example readable in REPL by slowing down transmissions
time.sleep(1)
def slave(timeout=6):
"""Polls the radio and prints the received value. This method expires
after 6 seconds of no received transmission"""
nrf.listen = True # put radio into RX mode and power up
start_timer = time.monotonic() # used as a timeout
while (time.monotonic() - start_timer) < timeout:
if nrf.available():
length = nrf.any() # grab payload length info
pipe = nrf.pipe # grab pipe number info
received = nrf.read(length) # clears info from any() and nrf.pipe
# increment counter before sending it back in responding payload
counter[0] = received[7:8][0] + 1
nrf.listen = False # put the radio in TX mode
result = False
ack_timeout = time.monotonic_ns() + 200000000
while not result and time.monotonic_ns() < ack_timeout:
# try to send reply for 200 milliseconds (at most)
result = nrf.send(b"World \0" + bytes([counter[0]]))
nrf.listen = True # put the radio back in RX mode
print(
"Received {} on pipe {}: {}{} Sent:".format(
length,
pipe,
bytes(received[:6]).decode("utf-8"), # convert to str
received[7:8][0],
),
end=" ",
)
if not result:
print("Response failed or timed out")
else:
print("World", counter[0])
start_timer = time.monotonic() # reset timeout
# recommended behavior is to keep in TX mode when in idle
nrf.listen = False # put the nRF24L01 in TX mode + Standby-I power state
OTA compatibility¶
Fake BLE Example¶
New in version 1.2.0.
This is a test to show how to use the nRF24L01 as a BLE advertising beacon using the
FakeBLE
class.
import time
import board
import digitalio
from circuitpython_nrf24l01.fake_ble import (
chunk,
FakeBLE,
UrlServiceData,
BatteryServiceData,
TemperatureServiceData,
)
# change these (digital output) pins accordingly
ce = digitalio.DigitalInOut(board.D4)
csn = digitalio.DigitalInOut(board.D5)
# using board.SPI() automatically selects the MCU's
# available SPI pins, board.SCK, board.MOSI, board.MISO
spi = board.SPI() # init spi bus object
# initialize the nRF24L01 on the spi bus object as a BLE compliant radio
nrf = FakeBLE(spi, csn, ce)
# the name parameter is going to be its broadcasted BLE name
# this can be changed at any time using the `name` attribute
# nrf.name = b"foobar"
# you can optionally set the arbitrary MAC address to be used as the
# BLE device's MAC address. Otherwise this is randomly generated upon
# instantiation of the FakeBLE object.
# nrf.mac = b"\x19\x12\x14\x26\x09\xE0"
# set the Power Amplifier level to -12 dBm since this test example is
# usually run with nRF24L01 transceiver in close proximity to the
# BLE scanning application
nrf.pa_level = -12
def _prompt(remaining):
if remaining % 5 == 0 or remaining < 5:
if remaining - 1:
print(remaining, "advertisments left to go!")
else:
print(remaining, "advertisment left to go!")
# create an object for manipulating the battery level data
battery_service = BatteryServiceData()
# battery level data is 1 unsigned byte representing a percentage
battery_service.data = 85
def master(count=50):
"""Sends out the device information twice a second."""
# using the "with" statement is highly recommended if the nRF24L01 is
# to be used for more than a BLE configuration
with nrf as ble:
ble.name = b"nRF24L01"
# include the radio's pa_level attribute in the payload
ble.show_pa_level = True
print(
"available bytes in next payload:",
ble.len_available(chunk(battery_service.buffer)),
) # using chunk() gives an accurate estimate of available bytes
for i in range(count): # advertise data this many times
if ble.len_available(chunk(battery_service.buffer)) >= 0:
_prompt(count - i) # something to show that it isn't frozen
# broadcast the device name, MAC address, &
# battery charge info; 0x16 means service data
ble.advertise(battery_service.buffer, data_type=0x16)
# channel hoping is recommended per BLE specs
ble.hop_channel()
time.sleep(0.5) # wait till next broadcast
# nrf.show_pa_level & nrf.name both are set to false when
# exiting a with statement block
# create an object for manipulating temperature measurements
temperature_service = TemperatureServiceData()
# temperature's float data has up to 2 decimal places of percision
temperature_service.data = 42.0
def send_temp(count=50):
"""Sends out a fake temperature twice a second."""
with nrf as ble:
ble.name = b"nRF24L01"
print(
"available bytes in next payload:",
ble.len_available(chunk(temperature_service.buffer)),
)
for i in range(count):
if ble.len_available(chunk(temperature_service.buffer)) >= 0:
_prompt(count - i)
# broadcast a temperature measurement; 0x16 means service data
ble.advertise(temperature_service.buffer, data_type=0x16)
ble.hop_channel()
time.sleep(0.2)
# use the Eddystone protocol from Google to broadcast a URL as
# service data. We'll need an object to manipulate that also
url_service = UrlServiceData()
# the data attribute converts a URL string into a simplified
# bytes object using byte codes defined by the Eddystone protocol.
url_service.data = "http://www.google.com"
# Eddystone protocol requires an estimated TX PA level at 1 meter
# lower this estimate since we lowered the actual `ble.pa_level`
url_service.pa_level_at_1_meter = -45 # defaults to -25 dBm
def send_url(count=50):
"""Sends out a URL twice a second."""
with nrf as ble:
print(
"available bytes in next payload:",
ble.len_available(chunk(url_service.buffer)),
)
# NOTE we did NOT set a device name in this with block
for i in range(count):
# URLs easily exceed the nRF24L01's max payload length
if ble.len_available(chunk(url_service.buffer)) >= 0:
_prompt(count - i)
ble.advertise(url_service.buffer, 0x16)
ble.hop_channel()
time.sleep(0.2)
TMRh20’s Arduino library¶
All examples are designed to work with TMRh20’s RF24 library examples. This Circuitpython library uses dynamic payloads enabled by default. TMRh20’s library uses static payload lengths by default.
To make this circuitpython library compatible with TMRh20’s RF24 library:
set
dynamic_payloads
toFalse
.set
allow_ask_no_ack
toFalse
.set
payload_length
to the value that is passed to TMRh20’sRF24::setPayloadSize()
. 32 is the default (& maximum) payload length/size for both libraries.Warning
Certain C++ datatypes allocate a different amount of memory depending on the board being used in the Arduino IDE. For example,
uint8_t
isn’t always allocated to 1 byte of memory for certain boards. Make sure you understand the amount of memory that different datatypes occupy in C++. This will help you comprehend how to configurepayload_length
.
For completness, TMRh20’s RF24 library uses a default value of 15 for the ard
attribute,
but this Circuitpython library uses a default value of 3.
circuitpython_nrf24l01 |
TMRh20 RF24 |
---|---|
nrf24l01_simple_test1 |
gettingStarted |
nrf24l01_ack_payload_test |
acknowledgementPayloads |
nrf24l01_manual_ack_test1 |
manualAcknowledgements |
nrf24l01_multiceiver_test1 |
multiceiverDemo |
nrf24l01_stream_test1 |
streamingData |
nrf24l01_interrupt_test |
interruptConfigure |
nrf24l01_context_test |
feature is not available |
nrf24l01_fake_ble_test |
feature is available via floe’s BTLE library |
- 1(1,2,3,4)
Some of the Circuitpython examples (that are compatible with TMRh20’s examples) contain 2 or 3 lines of code that are commented out for easy modification. These lines look like this in the examples’ source code:
# uncomment the following 3 lines for compatibility with TMRh20 library # nrf.allow_ask_no_ack = False # nrf.dynamic_payloads = False # nrf.payload_length = 4