List/discover nodes

In previous generations, the zigbee Python module was used to list the available XBee nodes of the network:

  1. Invoke the getnodelist() method to perform a discovery of XBee nodes. It returns the list of discovered nodes.

In the new XBee gateways, the digidevice.xbee Python module is used to list and discover the available XBee nodes of a network. The node list and node discovery are two separated operations.

List nodes

To list the nodes of the network:

  1. List the XBee of the gateway with the get_device() function.
  2. Once you have the local instance, get the XBee network using the get_network() method.
  3. List the available nodes with get_devices().
New API
from digidevice import xbee

device = xbee.get_device()
try:
    device.open()
    xbee_network = device.get_network()
    node_list = xbee_network.get_devices()
finally:
    if device.is_open():
        device.close()
    

Discover nodes

To perform an XBee network discover:

  1. Use the XBee network instance to register a network discovery callback.
  2. Invoke the start_discovery() method.
New API
import time

from digi.xbee.models.status import NetworkDiscoveryStatus
from digidevice import xbee

device = xbee.get_device()
try:
    device.open()
    xbee_network = device.get_network()
    xbee_network.set_discovery_timeout(15)  # 15 seconds.

    # Callback for discovered devices.
    def callback_device_discovered(remote):
        print("Device discovered: %s" % remote)

    # Callback for discovery finished.
    def callback_discovery_finished(status, desc=None):
        if status == NetworkDiscoveryStatus.SUCCESS:
            print("Discovery process finished successfully")
        else:
            info = status.description
            if desc:
                info = "%s (%s)" % (desc, status.description)
            print("There was an error discovering devices: %s" % info)

    xbee_network.add_device_discovered_callback(callback_device_discovered)
    xbee_network.add_discovery_process_finished_callback(callback_discovery_finished)
    xbee_network.start_discovery_process()
    while xbee_network.is_discovery_running():
        time.sleep(0.1)
    node_list =  xbee_network.get_devices()
finally:
    if device.is_open():
        device.close()