Search code examples
pythonxbee

How to get list of xbee devices in network using digi xbee Python


I have a xbee code running which has a scheduler scheduled to run every 1min. This scheduler queries the network and gets the xbee devices which are online. Below is the code:

def search_devices_in_nw():
    log_debug.error("Discovery process starting....")
    
    net = device.get_network()
    net.start_discovery_process(deep=True, n_deep_scans=1)
    while net.is_discovery_running():
        time.sleep(0.5)

    active_nodes = net.get_devices()
    print(active_nodes)


schedule.every(1).minutes.do(search_devices_in_nw)
while device.is_open():
    schedule.run_pending()

I have two devices in my xbee network and running this code, gives 2 mac address which is correct. But if I put one of the xbee device offline, it still gives result as 2 mac address online which is incorrect.

If I stop my code and restart it, then it shows 1 mac address online. I am not sure why the code is not working fine. Can anyone please help me in this. Please help. Thanks

Documentation page: https://xbplib.readthedocs.io/en/latest/user_doc/discovering_the_xbee_network.html#discovernetwork


Solution

  • Per the documentation, "All discovered XBee nodes are stored in the XBeeNetwork instance."

    But you can also clear the list of nodes with the clear() method on the XBeeNetwork object before initiating a new discovery:

    [...]
    
    # Instantiate a local XBee object.
    xbee = XBeeDevice(...)
    
    [...]
    
    # Get the XBee network object from the local XBee.
    xnet = xbee.get_network()
    
    # Discover XBee devices in the network and add them to the list of nodes.
    [...]
    
    # Clear the list of nodes.
    xnet.clear()
    
    [...]