Get started with MicroPython MicroPython networking and communication examples
Digi XBee® 3 Zigbee® RF Module
36
node_list = list(xbee.discover())
print("Remote node found, transmitting data")
for node in node_list:
dest_addr = node['sender_nwk'] # using 16 bit addressing
dest_node_id = node['node_id']
payload_data = "Hello, " + dest_node_id + "!"
print("Sending \"{}\" to {}".format(payload_data, hex(dest_addr)))
xbee.transmit(dest_addr, payload_data)
# Start the receive loop
print("Receiving data...")
print("Hit CTRL+C to cancel")
while True:
p = xbee.receive()
if p:
format_packet(p)
else:
time.sleep(0.25)
3. Press Ctrl+E to enter paste mode.
4. At the MicroPython >>> prompt, right-click and select the Paste option. Once you paste the
code, it executes immediately.
Example code on the router module
The following example code joins the Zigbee network from the previous example, and continuously
prints out any incoming data. This device also sends its temperature data every 5 seconds to the
coordinator address.
1. Access the MicroPython environment.
2. Copy the following sample code:
print("Joining network as a router...")
xbee.atcmd("NI", "Router")
network_settings = {"CE": 0, "ID": 0x3332, "EE": 0}
for command, value in network_settings.items():
xbee.atcmd(command, value)
xbee.atcmd("AC") # Apply changes
time.sleep(1)
while network_status() != 0:
time.sleep(0.1)
print("Connected to Network\n")
last_sent = time.ticks_ms()
interval = 5000 # How often to send a message
# Start the transmit/receive loop
print("Sending temp data every {} seconds".format(interval/1000))
while True:
p = xbee.receive()
if p:
format_packet(p)
else:
# Transmit temperature if ready