Pymesh
Pymesh - LoRa Mesh
This class provides Pymesh - LoRa Mesh protocol compliant for the LoRa network processor in the LoPy and FiPy. Below is an example demonstrating Pymesh initialisation and basic usage:
Additional Examples
For various other complete Pymesh examples, check Tutorials & Examples section (LoRa/Pymesh).
Constructors
class network.LoRa.Mesh()
Create and configure the Mesh object.
Methods
mesh.deinit()
De-initialise Pymesh task. Any further Pymesh commands will return no answer. To use again Pymesh the LoRa.Mesh()
constructor has to be called.
mesh.state()
Get node state inside Pymesh, which can be one of the following:
mesh.single()
Returns True
if this node is the only Leader or Router in the current Mesh network.
mesh.ipaddr()
Returns all the IPv6 unicast addresses assigned on Pymesh interface.
In the previous pymesh.ipaddr()
answer, these are the individual IPv6:
fe80:0:0:0:301:101:101:104
- link-local IPv6used to discover neighbors, configure links.
not routable.
always has a prefix of
fe80::/16
.
fdde:ad00:beef:0:86c3:6130:98cc:6633
- mesh-local identifierindependent of network topology.
does not change as the topology changes.
should be used by applications.
always has a prefix
fd00::/8
.
fdde:ad00:beef:0:0:ff:fe00:cc00
- routing locator (RLOC)identifies a mesh interface, based on its location in the network topology.
Changes as the topology changes.
Generally not used by applications.
fdde:ad00:beef:0:0:ff:fe00:fc00
- Leader IPv6.
mesh.rloc()
Returns the routing locator (RLOC) IPv6 address.
mesh.neighbors()
Returns a list with tuples containing information about neighbors, ie. all other nodes that have a direct radio connection to the calling node.
For each neighbor the following properties are returned:
mac - LoRa MAC address.
role - Child(2) or Router(3), implicitly Leader is shown as normal Router.
rssi - the RSSI of the radio link (Received Signal Strength Indication) expressed in db.
age - number of seconds since last data packet was received.
mesh.routers()
Returns a list with tuples containing information about all routers from Pymesh. Routers are Pymesh nodes that relay/route packets inside Pymesh.
For each Router the following properties are returned:
mac - LoRa MAC address.
id - the Pymesh internal ID, each Router has its own random unique ID.
path_cost - the cost of the path from current node to this router.
age - number of seconds since last keep-alive packet was received.
mesh.leader()
Returns information about Leader of the current Pymesh. can be called from any connected node.
The following details are returned:
part_id - partition id, the Pymesh internal network address.
mac - the LoRa MAC address of the Leader.
rloc16 - the Leader RLOC16.
mesh.rx_cb(handler, argument)
Specify the callback handler executed ( with parameter argument
) when a new packet was received, on any opened socket (in case multiple sockets are opened). At this moment, in the callback handler, the right socket has to be checked for incoming data.
Please check the following callback example.
mesh.border_router([ipv6_net_address, preference_level])
Gets or sets as Border Router the current node, by specifying the external IPv6 network address (prefix), which should be used for any IPv6 packet to be sent outside of Pymesh. The parameters details are:
ipv6_net_address
has to be a valid IPv6 network address containing IP and mask, for ex
"2001:cafe:cafe:cafe::/64"
.all the nodes from the Pymesh will receive a random additional IPv6 unicast address, from this network address.
a IPv6 packet which has as destination this prefix, will be routed to this node.
the UDP datagram has a custom header: MAGIC byte (
0xBB
), then 16 bytes of IPv6 destination and 2 bytes port number.to actually catch this IPv6 packet, a socket has to be created on a port.
please check the Pymesh example, to see how a UDP packet for external network is being handled.
preference_level - should be a value (-1: low, 0: normal, 1: high)
in case multiple Border Routers are being declared with the same prefix and the same path cost, the one with the highest preference is used.
mesh.border_router_del(ipv6_net_address)
Removes a Border Router entry, by specifying the external IPv6 network address (prefix), which should be used for any IPv6 packet to be sent outside of Pymesh. The parameter is the same as for mesh.border_router(ipv6_net_address, preference_level)
.
This will remove all IPv6 unicast from all Mesh nodes, which previously set an IPv6 with BR prefix.
mesh.cli()
Working with Pymesh LoRa Sockets
Pymesh supports only UDP sockets (not-acknowledged). They are created in the following way:
Multiple sockets (maximum 3) can be created, being bind on a certain IPv6 unicast and port number.
Pymesh sockets is created, if the Mesh was enabled before (lora.Mesh()
was called).
LoRa sockets support the following standard methods from the socket module:
socket.close()
Closes the socket.
Usage:
socket.bind(port_number)
socket.bind((ipv6_string, port_number))
Binds (links) an socket with an UDP port number (values 1024 to 63535), with or without an IPv6 interface. By default, if just port_number
is used, then it binds the socket with all IPv6 unicast addresses; it's equivalent with "::"
as for the ipv6_string
.
Usage:
socket.sendto(bytes,(ip, port))
Sends bytes
buffer to ip
, on the designated UDP port
. Returns the number of bytes sent.
Usage:
socket.recvfrom(bufsize)
This method is useful to know the destination port number of the message received. Returns a pair of the form: (data_bytes, (ipv6_string, port_number))
Usage:
Last updated