
Get started with MicroPython
MicroPython examples
Digi XBee® 3 802.15.4 RF Module User Guide
44
raise Exception("Network discovery did not find any remote devices")
for node in node_list:
dest_addr = node['sender_nwk']
# 'sender_eui64' can also be used
dest_node_id = node['node_id']
payload_data = "Hello, " + dest_n "!"
try:
print("Sending \"{}\" to {}".format(payload_data, hex(dest_addr)))
xbee.transmit(dest_addr, payload_data)
except Exception as err:
print(err)
print("complete")
Receiving data
Use the
receive()
function from the
xbee
module to receive messages. When MicroPython is active on
a device (
AP
is set to 4), all incoming messages are saved to a receive queue within MicroPython. This
receive queue is limited in size and only has room for 4 messages. To ensure that data is not lost, it is
important to continuously iterate through the receive queue and process any of the packets within.
If the receive queue is full and another message is sent to the device, it will not acknowledge the
packet and the sender generates a failure status of 0x24 (Address not found).
The
receive()
function returns one of the following:
n
None: No message (the receive queue is empty).
n
Message dictionary consisting of:
l
sender_nwk
: 16-bit network address of the sending node.
l
sender_eui64
: 64-bit address (as a "bytes object") of the sending node.
l
source_ep
: source endpoint as an integer.
l
dest_ep
: destination endpoint as an integer.
l
cluster
: cluster id as an integer.
l
profile
: profile id as an integer.
l
broadcast
: True or False depending on whether the frame was broadcast or unicast.
l
payload
: "Bytes object" of the payload. This is a bytes object instead of a string, because
the payload can contain binary data.
Example: continuously receive data
In this example, the
format_packet()
helper formats the contents of the dictionary and
format_eui64
()
formats the bytes object holding the EUI-64. The
while
loop shows how to poll for packets
continually to ensure that the receive buffer does not become full.
def format_eui64(addr):
return ':'.join('%02x' % b for b in addr)
def format_packet(p):
type = 'Broadcast' if p['broadcast'] else 'Unicast'
print("%s message from EUI-64 %s (network 0x%04X)" % (type,
format_eui64(p['sender_eui64']), p['sender_nwk']))
print("
from EP 0x%02X to EP 0x%02X, Cluster 0x%04X, Profile 0x%04X:" %
(p['source_ep'], p['dest_ep'], p['cluster'], p['profile']))
print(p['payload'])