Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1cd42eb

Browse files
author
Catalin Ioana
committed
[pymesh] added Border Router example
1 parent 0fc65e8 commit 1cd42eb

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

pymesh/pymesh_frozen/main_BR.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import time
2+
import pycom
3+
4+
# 2 = test pybytes OTA feature
5+
__VERSION__ = 3
6+
7+
try:
8+
from pymesh_config import PymeshConfig
9+
except:
10+
from _pymesh_config import PymeshConfig
11+
12+
try:
13+
from pymesh import Pymesh
14+
except:
15+
from _pymesh import Pymesh
16+
17+
# LoRa mac that will be self-defined as Border Routers
18+
MAC_BR = {2,4}
19+
20+
print("Scripts version ", __VERSION__)
21+
22+
if 'pybytes' not in globals():
23+
pybytes = None
24+
25+
def new_message_cb(rcv_ip, rcv_port, rcv_data):
26+
''' callback triggered when a new packet arrived '''
27+
print('Incoming %d bytes from %s (port %d):' %
28+
(len(rcv_data), rcv_ip, rcv_port))
29+
print(rcv_data)
30+
31+
# user code to be inserted, to send packet to the designated Mesh-external interface
32+
for _ in range(3):
33+
pycom.rgbled(0x888888)
34+
time.sleep(.2)
35+
pycom.rgbled(0)
36+
time.sleep(.1)
37+
return
38+
39+
def new_br_message_cb(rcv_ip, rcv_port, rcv_data, dest_ip, dest_port):
40+
''' callback triggered when a new packet arrived for the current Border Router,
41+
having destination an IP which is external from Mesh '''
42+
print('Incoming %d bytes from %s (port %d), to external IPv6 %s (port %d)' %
43+
(len(rcv_data), rcv_ip, rcv_port, dest_ip, dest_port))
44+
print(rcv_data)
45+
46+
for _ in range(2):
47+
pycom.rgbled(0x0)
48+
time.sleep(.1)
49+
# pycom.rgbled(0x001010)
50+
pycom.rgbled(0x663300)
51+
# time.sleep(.2)
52+
53+
if pybytes is not None and pybytes.isconnected():
54+
pkt = 'BR %d B from %s (%d), to %s ( %d): %s'%(len(rcv_data), rcv_ip, rcv_port, dest_ip, dest_port, str(rcv_data))
55+
pybytes.send_signal(1, pkt)
56+
57+
return
58+
59+
pycom.heartbeat(False)
60+
61+
# read config file, or set default values
62+
pymesh_config = PymeshConfig.read_config()
63+
64+
#initialize Pymesh
65+
pymesh = Pymesh(pymesh_config, new_message_cb)
66+
67+
# mac = pymesh.mac()
68+
# if mac > 10:
69+
# pymesh.end_device(True)
70+
# elif mac == 5:
71+
# pymesh.leader_priority(255)
72+
73+
while not pymesh.is_connected():
74+
print(pymesh.status_str())
75+
time.sleep(3)
76+
77+
# send message to the Node having MAC address 5
78+
pymesh.send_mess(2, "Hello World")
79+
80+
81+
print("done Pymesh init, forever loop, exit/stop with Ctrl+C multiple times")
82+
# set BR with callback
83+
if pybytes is not None and pybytes.isconnected():
84+
pybytes.send_signal(1, "RESTART")
85+
86+
pyb_port = pymesh.mac() & 0xFFFF
87+
pyb_ip = '1:2:3::' + hex(pyb_port)[2:]
88+
pkt_start = "Hello, from " + str(pymesh.mac()) + ", time "
89+
90+
br_enabled = False
91+
92+
while True:
93+
# add current node as Border Router, with a priority and a message handler callback
94+
95+
free_mem = pycom.get_free_heap()
96+
97+
if pymesh.mac() in MAC_BR:
98+
if pybytes is not None and pybytes.isconnected():
99+
if not br_enabled:
100+
br_enabled = True
101+
print("Set as BR")
102+
pymesh.br_set(PymeshConfig.BR_PRIORITY_NORM, new_br_message_cb)
103+
104+
pybytes.send_signal(1, str(pymesh.mac()) +" : " + str(time.time()) + "s, "+ str(free_mem))
105+
print("Send to Pyb,", free_mem)
106+
else: # not connected anymore to pybytes
107+
if br_enabled:
108+
br_enabled = False
109+
print("Remove as BR")
110+
pymesh.br_remove()
111+
else: # not MAC_BR
112+
pkt = pkt_start + str(time.time()) + ", mem " + str(free_mem)
113+
pymesh.send_mess_external(pyb_ip, pyb_port, pkt)
114+
print("Sending to BR: ", pkt)
115+
116+
time.sleep(20)

0 commit comments

Comments
 (0)