How to discover/pair ZHA compliant Zigbee devices with Xbee python library

I have Xbee3USB which is setup as Coordinator (AP: 2, CE: 1, AO: 3/E, ZS: 2 with XCTU tool). In XCTU network mode, if “Scan” is pressed and ZHA compliant endpoint devices(i.e IKEA Tradfri light, Plug, Remote Control) are in paring mode then all the end devices are discovered and connected.

But how to do it with xbee python library(https://xbplib.readthedocs.io/en/latest/)? I have setup the environment in Windows and Python is the latest (3.11).

Any guideline how to discover zigbee endpoints and control the remote devices?

The following code discovers only the Coordinator MAC address connected to the USB port:

# Instantiate a local XBee object.
xbee = XBeeDevice("COM1", 9600)

xbee.open()

# Define the device discovered callback.
def callback(remote):
    print(remote)

# Get the XBee network object from the local XBee.
xnet = xbee.get_network()

# Add the device discovered callback.
xnet.add_device_discovered_callback(callback)

xnet.set_deep_discovery_options(deep_mode=NeighborDiscoveryMode.FLOOD,
                                del_not_discovered_nodes_in_last_scan=False)

# Configure the discovery timeout, in SECONDS.
xnet.set_deep_discovery_timeouts(node_timeout=25, time_bw_requests=10,
                                time_bw_scans=20)

# Start the discovery process.
xnet.start_discovery_process(deep=True, n_deep_scans=1)

while xnet.is_discovery_running():
    time.sleep(0.5)

if xbee is not None and xbee.is_open():
    xbee.close()

I have also tried to run:
xbee.execute_command(“ND”) and xbee.execute_command(“AS”). But no success.

You are going to need to do a mixture of https://xbplib.readthedocs.io/en/latest/user_doc/discovering_the_xbee_network.html?highlight=Zigbee%20device%20object%20discovery#configure-the-standard-discovery-process and https://xbplib.readthedocs.io/en/latest/_modules/digi/xbee/models/zdo.html?highlight=Zigbee%20device%20object%20discovery

1 Like

Thanks for your help again. With the following steps, I am able to discover the devices.
Step1:

  • With XCTU - I needed changes only (AP:2, ZS:2, CE:1) from default settings. I kept AO as the default value: 0.
  • Also, I used the same discovery timing settings as in XCTU network working mode (25s timeout, 3s between Req and 5 sec between Scan). It was 30s timeout there. But the xbee python module complains if it more than 25s. I have not studied yet in the code. But it is OK.
  • Increased number of scanning cycles for some extended time.
    Step2: Run the python code and put the device in pairing mode

import datetime
import random
import string
import time
import sys
from digi.xbee.devices import XBeeDevice
from digi.xbee.devices import ZigBeeDevice
from digi.xbee.exception import TimeoutException
from digi.xbee.util import utils
from digi.xbee.models.mode import NeighborDiscoveryMode
def main():
# Instantiate a local XBee object.
xbee = ZigBeeDevice(“COM12”, 9600)
# Define the device discovered callback.
def dcallback(remote):
print(remote)
try:
xbee.open()
# Get the XBee network object from the local XBee.
xnet = xbee.get_network()
xnet.clear()
# Add the device discovered callback.
xnet.add_device_discovered_callback(dcallback) xnet.set_deep_discovery_options(deep_mode=NeighborDiscoveryMode.CASCADE, del_not_discovered_nodes_in_last_scan=False)
# Configure the discovery timeout, in SECONDS.
xnet.set_deep_discovery_timeouts(node_timeout=25, time_bw_requests=3, time_bw_scans=5)
# Start the discovery process.
xnet.start_discovery_process(deep=True, n_deep_scans=5)
while xnet.is_discovery_running():
time.sleep(0.5)
nodes = xnet.get_devices()
print(nodes)
finally:
if xbee is not None and xbee.is_open():
xbee.close()
if name == ‘main’:
main()

Result: MAC addresses of discovered Zigbee devices are printed.