These API's are currently only available in the latest RC builds.
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:
from network import LoRa# initialise LoRa# the LoRa parameters (frecq, sf, bandwidth) has to be the same for all# nodes in the same Pymeshlora =LoRa(mode=LoRa.LORA,region=LoRa.EU868,frequency=863000000,bandwidth=LoRa.BW_125KHZ,sf=7)print("Enable Pymesh")pymesh = lora.Mesh()# check node state inside Pymesh# PYMESH_ROLE_DISABLED = 0, ///< The Pymesh stack is disabled.# PYMESH_ROLE_DETACHED = 1, ///< Not currently participating in a Pymesh.# PYMESH_ROLE_CHILD = 2, ///< The Pymesh Child role.# PYMESH_ROLE_ROUTER = 3, ///< The Pymesh Router role.# PYMESH_ROLE_LEADER = 4, ///< The Pymesh Leader role.print("Pymesh node role: %d"%pymesh.state())print("IPv6 unicast addresses: %s"%pymesh.ipaddr())
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:
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.
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()
Sends a CLI command to the internal openthread engine; the list of CLI commands is here
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).
The Pymesh sockets supports only the following socket methods: close() , bind(), sendto(), and recvfrom().
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))
# handler responsible for receiving packets on UDP Pymesh socket
def receive_pack(sockets):
# listen for incoming packets on all sockets
while True:
is_new_data = False
for sock in sockets:
# check if data received on all sockets
rcv_data, rcv_addr = sock.recvfrom(128)
if len(rcv_data) > 0:
is_new_data = True
break # out of for sock
if not is_new_data:
break # out of while True
rcv_ip = rcv_addr[0]
rcv_port = rcv_addr[1]
print('Incomming %d bytes from %s (port %d)'%(len(rcv_data), rcv_ip, rcv_port))
#check if data is for the external of the Pymesh (for The Cloud)
if rcv_data[0] == BORDER_ROUTER_MAGIC_BYTE and len(rcv_data) >= struct.calcsize(BORDER_ROUTER_HEADER_FORMAT):
br_header = struct.unpack(BORDER_ROUTER_HEADER_FORMAT, rcv_data)
print("IP dest: %x:%x:%x:%x:%x:%x:%x:%x (port %d)"%(
br_header[1],br_header[2],br_header[3],br_header[4],
br_header[5],br_header[6],br_header[7],br_header[8], br_header[9]))
rcv_data = rcv_data[struct.calcsize(BORDER_ROUTER_HEADER_FORMAT):]
print(rcv_data)
# send some ACK
if not rcv_data.startswith("ACK"):
print("Sent ACK back")
sock.sendto('ACK', (rcv_ip, rcv_port))
# create the list of sockets
sockets = []
sockets.append(eid_socket)
sockets.append(br_socket)
# set RX callback
mesh.mesh.rx_cb(receive_pack, sockets)
# IPv6 addresses, before setting Border Router
>>> pymesh.ipaddr()
['fdde:ad00:beef:0:0:ff:fe00:fc00', 'fdde:ad00:beef:0:0:ff:fe00:cc00', 'fdde:ad00:beef:0:86c3:6130:98cc:6633', 'fe80:0:0:0:301:101:101:104']
# setting Border Router
>>> pymesh.border_router("2001:cafe:cafe:cafe::/64", 0)
0
# checking a new IPv6 address is assigned
>>> pymesh.ipaddr()
['2001:cafe:cafe:cafe:a5d2:6934:9acd:66b3', 'fdde:ad00:beef:0:0:ff:fe00:fc00', 'fdde:ad00:beef:0:0:ff:fe00:cc00', 'fdde:ad00:beef:0:86c3:6130:98cc:6633', 'fe80:0:0:0:301:101:101:104']
# list the BR entries
>>> mesh.mesh.border_router()
[(net='2001:dead:beef:cafe::/64', preference=0)]
>>>
# BR entry
>>> mesh.mesh.border_router()
[(net='2001:dead:beef:caff::/64', preference=1)]
# checking the BR IPv6 unicast address with BR prefix
>>> pymesh.ipaddr()
['2001:dead:beef:caff:2291:48a4:5229:94ca', 'fdde:ad00:beef:0:0:ff:fe00:6800', 'fdde:ad00:beef:0:4623:91c8:64b2:d9ec', 'fe80:0:0:0:200:0:0:8']
# remove the BR entry
>>> mesh.mesh.border_router_del('2001:dead:beef:caff::/64')
# verify, no more BR entry
>>> mesh.mesh.border_router()
[]
# verify, no more IPv6 with BR prefix
>>> mesh.ipaddr()
['fdde:ad00:beef:0:0:ff:fe00:6800', 'fdde:ad00:beef:0:4623:91c8:64b2:d9ec', 'fe80:0:0:0:200:0:0:8']
# get the Leader data set
>>> print(pymesh.cli('leaderdata'))
Partition ID: 828258
Weighting: 64
Data Version: 217
Stable Data Version: 108
Leader Router ID: 51
# sending a PING to another node from Pymesh
>>> pymesh.cli('ping fdde:ad00:beef:0:0:ff:fe00:d800')
'16 bytes from fdde:ad00:beef:0:0:ff:fe00:d800: icmp_seq=2 hlim=64 time=246ms\r\n'
import socket
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.close()
# binding socket with all IPv6 interfaces, like "::"
>>> s.bind(1234)
>>> mesh.ipaddr()
['fdde:ad00:beef:0:0:ff:fe00:6800', 'fdde:ad00:beef:0:4623:91c8:64b2:d9ec', 'fe80:0:0:0:200:0:0:8']
# binding the socket on a specific pair IPv6 and port number
>>> s.bind(('fdde:ad00:beef:0:4623:91c8:64b2:d9ec', 1235))