mirror of
https://github.com/sascha-hemi/pycom-documentation.git
synced 2026-03-26 20:06:33 +01:00
tutorials/lora: updated Pymesh example
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# LoRa Mesh
|
||||
# Pymesh - LoRa Mesh
|
||||
|
||||
The LoRa Mesh is implemented using [OpenThread](https://openthread.io/guides/thread-primer).
|
||||
The Pymesh LoRa Mesh is implemented using [OpenThread](https://openthread.io/guides/thread-primer).
|
||||
|
||||
To understand the OpenThread terms and overall functionality, these guides are highly recommended:
|
||||
|
||||
@@ -8,7 +8,7 @@ To understand the OpenThread terms and overall functionality, these guides are h
|
||||
* [Node Roles and Types](https://openthread.io/guides/thread-primer/node-roles-and-types)
|
||||
* [IPv6 Addressing](https://openthread.io/guides/thread-primer/ipv6-addressing) \(especially, RLOC unicast address\)
|
||||
|
||||
Important, all the OpenThread CLI commands are accessible using `LoRa.cli("command")`, the complete list of commands is [here](https://github.com/openthread/openthread/blob/master/src/cli/README.md). Please note some commands, can't execute, as some functionalities are not implemented \(ex: Commissioning Role, Joiner Role, DNS\).
|
||||
Important, all the OpenThread CLI commands are accessible using `LoRa.cli("command")`, the complete list of commands is [here](https://github.com/openthread/openthread/blob/c482301ec73b80985445102e4d0a936346172ddb/src/cli/README.md). Please note some commands, can't execute, as some functionalities are not implemented \(ex: Commissioning Role, Joiner Role, DNS\).
|
||||
|
||||
This example shows how to:
|
||||
|
||||
@@ -40,14 +40,16 @@ import machine
|
||||
|
||||
from loramesh import Loramesh
|
||||
|
||||
pycom.wifi_on_boot(False)
|
||||
pycom.heartbeat(False)
|
||||
|
||||
lora = LoRa(mode=LoRa.LORA, region=LoRa.EU868, bandwidth=LoRa.BW_125KHZ, sf=7)
|
||||
MAC = str(ubinascii.hexlify(lora.mac()))[2:-1]
|
||||
print("LoRa MAC: %s"%MAC)
|
||||
|
||||
mesh = Loramesh(lora)
|
||||
|
||||
# waiting until it connected to Mesh network and
|
||||
# it has some valid neighbors
|
||||
# waiting until it connected to Mesh network
|
||||
while True:
|
||||
mesh.led_state()
|
||||
print("%d: State %s, single %s"%(time.time(), mesh.cli('state'), mesh.cli('singleton')))
|
||||
@@ -55,35 +57,21 @@ while True:
|
||||
if not mesh.is_connected():
|
||||
continue
|
||||
|
||||
neigbors = mesh.neighbors_ip()
|
||||
if len(neigbors) == 0:
|
||||
print('No neighbor')
|
||||
continue
|
||||
|
||||
print('Neighbors found: %s'%neigbors)
|
||||
print('Neighbors found: %s'%mesh.neighbors())
|
||||
break
|
||||
|
||||
# create UDP socket
|
||||
s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
|
||||
myport = 1234
|
||||
s.bind(myport)
|
||||
pack_num = 1
|
||||
msg = "Hello World! MAC: " + MAC + ", pack: "
|
||||
ip = mesh.ip()
|
||||
|
||||
while True:
|
||||
mesh.led_state()
|
||||
print("%d: State %s, single %s, IP %s"%(time.time(), mesh.cli('state'), mesh.cli('singleton'), mesh.ip()))
|
||||
|
||||
# check if topology changes, maybe RLOC IPv6 changed
|
||||
new_ip = mesh.ip()
|
||||
if ip != new_ip:
|
||||
print("IP changed from: %s to %s"%(ip, new_ip))
|
||||
ip = new_ip
|
||||
|
||||
# handler responisble for receiving packets on UDP Pymesh socket
|
||||
def receive_pack():
|
||||
# listen for incomming packets
|
||||
rcv_data, rcv_addr = s.recvfrom(128)
|
||||
if len(rcv_data)>0:
|
||||
while True:
|
||||
rcv_data, rcv_addr = s.recvfrom(128)
|
||||
if len(rcv_data) == 0:
|
||||
break
|
||||
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))
|
||||
@@ -95,11 +83,26 @@ while True:
|
||||
except Exception:
|
||||
pass
|
||||
mesh.blink(7, .3)
|
||||
continue
|
||||
|
||||
pack_num = 1
|
||||
msg = "Hello World! MAC: " + MAC + ", pack: "
|
||||
ip = mesh.ip()
|
||||
mesh.mesh.rx_cb(receive_pack)
|
||||
|
||||
# infinite main loop
|
||||
while True:
|
||||
mesh.led_state()
|
||||
print("%d: State %s, single %s, IP %s"%(time.time(), mesh.cli('state'), mesh.cli('singleton'), mesh.ip()))
|
||||
|
||||
# check if topology changes, maybe RLOC IPv6 changed
|
||||
new_ip = mesh.ip()
|
||||
if ip != new_ip:
|
||||
print("IP changed from: %s to %s"%(ip, new_ip))
|
||||
ip = new_ip
|
||||
|
||||
# update neighbors list
|
||||
neigbors = mesh.neighbors_ip()
|
||||
print("%d Neighbors %s"%(len(neigbors), neigbors))
|
||||
print("%d neighbors, IPv6 list: %s"%(len(neigbors), neigbors))
|
||||
|
||||
# send PING and UDP packets to all neighbors
|
||||
for neighbor in neigbors:
|
||||
@@ -121,5 +124,5 @@ while True:
|
||||
|
||||
# random sleep time
|
||||
time.sleep(30 + machine.rng()%30)
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user