RF24Mesh API¶
Added in version 2.1.0.
See Also
Documentation for:
Shared Networking API (API common to
RF24MeshandRF24Network)RF24Network API (
RF24Meshinherits from the same mixin class thatRF24Networkinherits from)
RF24MeshNoMaster class¶
-
class circuitpython_nrf24l01.rf24_mesh.RF24MeshNoMaster(spi: SPI, csn_pin: DigitalInOut, ce_pin: DigitalInOut, node_id: int, spi_frequency: int =
10000000)[source]¶ A descendant of the same mixin class that
RF24Networkinherits from. This class adds easy Mesh networking capability (non-master nodes only).This class exists to save memory for nodes that don’t behave like mesh network master nodes. It is the python equivalent to TMRh20’s
MESH_NO_MASTERmacro in the C++ RF24Mesh library. All the API is the same asRF24Meshclass.See Also
For all parameters’ descriptions, see the
RF24class’ constructor documentation.
- RF24MeshNoMaster.release_address() bool[source]¶
Forces an address lease to expire from the master.
Hint
This should be called from a mesh network node that is disconnecting from the network. This is also recommended for mesh network nodes that are entering a powered down (or sleep) mode.
RF24Mesh class¶
-
class circuitpython_nrf24l01.rf24_mesh.RF24Mesh(spi: SPI, csn_pin: DigitalInOut, ce_pin: DigitalInOut, node_id: int, spi_frequency: int =
10000000)[source]¶ Bases:
RF24MeshNoMasterA descendant of the base class
RF24MeshNoMasterthat adds algorithms needed for Mesh network master nodes.See Also
For all parameters’ descriptions, see the
RF24class’ constructor documentation.
Basic API¶
- RF24Mesh.send(to_node: int, message_type: int | str, message: bytes | bytearray) bool¶
Send a message to a mesh
node_id.This function will use
lookup_address()to fetch the necessary Logical Address to set the frame’s header’sto_nodeattribute.Hint
If you already know the destination node’s Logical Address, then you can use
write()for quicker operation.- Parameters:
- to_node: int¶
The unique mesh network
node_idof the frame’s destination. Defaults to0(which is reserved for the master node).- message_type: int | str¶
The
intthat describes the frame header’smessage_type.- message: bytes | bytearray¶
The frame’s
messageto be transmitted.Note
Be mindful of the message’s size as this cannot exceed
MAX_FRAG_SIZE(24 bytes) iffragmentationis disabled. Iffragmentationis enabled (it is by default), then the message’s size must be less thanmax_message_length.
- Returns:
Trueif theframehas been transmitted. This does not necessarily describe if the message has been received at its target destination.Falseif theframehas not been transmitted.
Tip
To ensure a message has been delivered to its target destination, set the
message_typeparameter to anintin range [65, 127]. This will invoke aNETWORK_ACKresponse message.
- property RF24Mesh.node_id : int¶
The unique ID number (1 byte long) of the mesh network node.
This is not to be confused with the network node’s
node_address. This attribute is meant to distinguish different mesh network nodes that may, at separate instances, use the samenode_address. It is up to the developer to make sure each mesh network node uses a different ID number.Warning
Changing this attributes value after instantiation will automatically call
release_address()which disconnects the node from the mesh network. Notice thenode_addressis set toNETWORK_DEFAULT_ADDRwhen consciously not connected to the mesh network.Tip
When a mesh node becomes disconnected from the mesh network, use
renew_address()to fetch (from the master node) an assigned logical address to be used as the mesh node’snode_address.
-
RF24Mesh.renew_address(timeout: float | int =
7.5)[source]¶ Connect to the mesh network and request a new
node_address.- Parameters:
- timeout: float | int =
7.5¶ The amount of time (in seconds) to continue trying to connect and get an assigned Logical Address. Defaults to 7.5 seconds.
- timeout: float | int =
Note
This function automatically sets the
node_addressaccordingly.- Returns:
If successful: The
node_addressthat was set to the newly assigned Logical Address.If unsuccessful:
None, and thenode_addressattribute will be set toNETWORK_DEFAULT_ADDR(0o4444in octal or2340in decimal).
Advanced API¶
-
RF24Mesh.lookup_node_id(address: int | None =
None) int[source]¶ Convert a node’s Logical Address into its corresponding unique ID number.
- Parameters:
- address: int | None =
None¶ The Logical Address for which a unique
node_idis assigned from network master node.
- address: int | None =
- Returns:
The unique
node_idassigned to the specifiedaddress.Error codes include
-2means the specifiedaddresshas not been assigned a uniquenode_idfrom the master node or the requesting network node’snode_addressis equal toNETWORK_DEFAULT_ADDR.-1means the address lookup operation failed due to no network connection or the master node has not assigned a uniquenode_idfor the specifiedaddress.
-
RF24Mesh.lookup_address(node_id: int | None =
None) int[source]¶ Convert a node’s unique ID number into its corresponding Logical Address.
- Parameters:
- node_id: int | None =
None¶ The unique
node_idfor which a Logical Address is assigned from network master node.
- node_id: int | None =
- Returns:
The Logical Address assigned to the specified
node_id.Error codes include
-2means the specifiednode_idhas not been assigned a Logical Address from the master node or the requesting network node’snode_addressis equal toNETWORK_DEFAULT_ADDR.-1means the address lookup operation failed due to no network connection or the master node has not assigned a Logical Address for the specifiednode_id.
- RF24Mesh.write(to_node: int, message_type: int | str, message: bytes | bytearray) bool¶
Send a message to a network
node_address.- Parameters:
- to_node: int¶
The network node’s Logical Address. of the frame’s destination. This must be the destination’s network
node_addresswhich is not be confused with a mesh node’snode_id.- message_type: int | str¶
The
intthat describes the frame header’smessage_type.Note
Be mindful of the message’s size as this cannot exceed
MAX_FRAG_SIZE(24 bytes) iffragmentationis disabled. Iffragmentationis enabled (it is by default), then the message’s size must be less thanmax_message_length.- message: bytes | bytearray¶
The frame’s
messageto be transmitted.
- Returns:
Trueif theframehas been transmitted. This does not necessarily describe if the message has been received at its target destination.Falseif theframehas not been transmitted.
Tip
To ensure a message has been delivered to its target destination, set the
message_typeparameter to anintin range [65, 127]. This will invoke aNETWORK_ACKresponse message.
-
RF24Mesh.check_connection(attempts: int =
3, ping_master: bool =False) bool[source]¶ Check for network connectivity (not for use on master node).
- Parameters:
- attempts: int =
3¶ The number of attempts to test for active connection to the mesh network.
- ping_master: bool =
False¶ If this parameter is set to
True, then this function will verify the connectivity by usinglookup_address()to transact with the master node. Setting this parameter toFalsewill simply ping the node’s parent.Warning
Setting this parameter to
Truecan result in performance cost when used in a large mesh network. The disadvantages in such a situation are:increased load on master node
increased network congestion
unreliable connectivity information when a parent or grandparent of the current node briefly loses connection.
- attempts: int =
- Returns:
Changed in version 2.2.0: Added
attemptsandping_masterparameters; changed return value for master nodesPreviously, this function would return
Falsewhen called from a master node. This was changed to returnTrueto help avoid erroneous user code callingrenew_address()on a master node.
-
RF24Mesh.release_address(address: int =
0) bool[source]¶ Release an assigned address from any corresponding mesh node’s ID.
Important
This function is meant for use on master nodes. If the
addressparameter is not specified, thenRF24MeshNoMaster.release_address()is called.- Parameters:
- Returns:
Added in version 2.2.0: Allows master nodes to forcibly release an assigned address.
This function is essentially an overload of
RF24MeshNoMaster.release_address()specifically designed for use on a master node.
- RF24Mesh.block_less_callback : Callable[[], Any] | None¶
This variable can be assigned a function to perform during long operations.
Note
Requesting a new address (via
renew_address()) can take a while since it sequentially attempts to get re-assigned to the first available Logical Address on the highest possible network level.The assigned function will be called during
renew_address(),lookup_address()andlookup_node_id().The callback function assigned should take no positional parameters and it’s returned data (if any) is ignored. For example:
In user/app code space¶arbitrary_global_counter = [0] def callback_func(kw_arg: int = 1): arbitrary_global_counter[0] += kw_arg # let `mesh_node` be the instantiated RF24Mesh object mesh_node.block_less_callback = callback_func
- RF24Mesh.dhcp_dict : Dict[int, int]¶
A
dictthat enables master nodes to act as a DNS.This
dictstores the assigned Logical Addresses to the connected mesh node’snode_id.The keys in this
dictare the uniquenode_idof a mesh network node.The values in this
dict(corresponding to each key) are thenode_addressassigned to thenode_id.
-
RF24Mesh.save_dhcp(filename: str =
'dhcplist.json', as_bin: bool =False)[source]¶ Save the
dhcp_dictto a JSON file (meant for master nodes only).Warning
This function will likely throw a
OSErroron boards running CircuitPython firmware because the file system is by default read-only.Calling this function on a Linux device (like the Raspberry Pi) will save the
dhcp_dictto a JSON file located in the program’s working directory.- Parameters:
Changed in version 2.1.1: Added
as_binparameter to make use of binary text files.
-
RF24Mesh.load_dhcp(filename: str =
'dhcplist.json', as_bin: bool =False)[source]¶ Load the
dhcp_dictfrom a JSON file (meant for master nodes only).- Parameters:
Warning
This function will raise an
OSErrorexception if no file exists.Changed in version 2.1.1: Added
as_binparameter to make use of binary text files.
-
RF24Mesh.set_address(node_id: int, node_address: int, search_by_address: bool =
False)[source]¶ Set/change a
node_idandnode_addresspair in thedhcp_dict.This function is only meant to be called on the mesh network’s master node. Use this function to manually assign a
node_idto aRF24Network.node_address.