def main():

    print(" +--------------------------------+")
    print(" | Discover Specific Devices Test |")
    print(" +--------------------------------+\n")

    device = XBeeDevice(PORT, BAUD_RATE)

    try:
        device.open()

        network = device.get_network()

        # Discover a valid remote device.
        remote = network.discover_device(REMOTE_NODE_ID)
        assert (remote is not None)
        assert (remote.get_node_id() == REMOTE_NODE_ID)

        # Discover an invalid remote device.
        remote = network.discover_device("!inv4lid_1d!")
        assert (remote is None)

        # Discover myself.
        network.set_discovery_options({DiscoveryOptions.DISCOVER_MYSELF})
        if device.get_protocol() == XBeeProtocol.RAW_802_15_4:
            assert (network.get_discovery_options()[0] == 1)
        else:
            assert (network.get_discovery_options()[0] == 2)
        remote = network.discover_device(device.get_node_id())
        assert (remote == device)

        network.clear()

        # Discover the remote device and myself.
        devices_list = network.discover_devices(
            [REMOTE_NODE_ID, device.get_node_id()])
        assert (len(devices_list) == 2)
        assert (network.get_device_by_node_id(device.get_node_id()) == device)

        # Restore the discovery options.
        network.set_discovery_options({})
        assert (network.get_discovery_options()[0] == 0)

        print("Test finished successfully")

    finally:
        if device is not None and device.is_open():
            device.close()
def main():

    print(" +---------------------------------+")
    print(" | Get and Set Params Local/Remote |")
    print(" +---------------------------------+\n")

    local_xbee = XBeeDevice(PORT, BAUD_RATE)

    try:
        local_xbee.open()
        remote_xbee = RemoteXBeeDevice(local_xbee,
                                       x64bit_addr=REMOTE_DEVICE_ADDRESS)

        local_xbee.read_device_info()
        print("Read device info of local device successfully")
        remote_xbee.read_device_info()
        print("Read device info of remote device successfully")

        print("\nLocal:")
        print(local_xbee.get_node_id())
        print(local_xbee.get_hardware_version())
        print(hex_to_string(local_xbee.get_firmware_version()))
        print(local_xbee.get_protocol())
        print("\nRemote:")
        print(remote_xbee.get_node_id())
        print(remote_xbee.get_hardware_version())
        print(hex_to_string(remote_xbee.get_firmware_version()))
        print(remote_xbee.get_protocol())

        ni = ''.join(
            random.choice(string.ascii_letters)
            for i in range(random.randint(1, 20)))
        local_xbee.set_parameter("NI", bytearray(ni, "utf8"))
        param = local_xbee.get_parameter("NI")
        assert (param.decode() == ni)

        ni = ''.join(
            random.choice(string.ascii_letters)
            for i in range(random.randint(1, 20)))
        remote_xbee.set_parameter("NI", bytearray(ni, "utf8"))
        param = remote_xbee.get_parameter("NI")
        assert (param.decode() == ni)

        print("\nTest finished successfully")

    finally:
        if local_xbee is not None and local_xbee.is_open():
            local_xbee.close()
Exemplo n.º 3
0
from digi.xbee.devices import XBeeDevice
device = XBeeDevice("/dev/ttyUSB0", 9600)
device.open()

print(device.read_device_info())

protocol = device.get_protocol()
print(protocol)


print("get_64bit_addr {}".format(device.get_64bit_addr()))
print("get_16bit_addr {}".format(device.get_16bit_addr()) )



device.close()
Exemplo n.º 4
0
def shutdown_cleanly(xbee: XBeeDevice) -> Error:
    """
    Helper function to attempt to cleanly shut down the XBee.

    Note that only XBee 3 Cellular devices require a clean shutdown; this function
    behaves as a NOP for XBee 3 Zigbee/802.15.4/DigiMesh devices.

    You should invoke this function prior to issuing the ATFR command or removing power to the XBee.
    Otherwise, you risk corrupting/bricking the XBee's cell modem chip.
    For more info, see the "Clean shutdown" page in the XBee3 Cellular User Guide:
    https://www.digi.com/resources/documentation/Digidocs/90002258/#Reference/r_clean_shutdown.htm

    This function assumes that the XBee is currently in API Mode Without Escapes.

    The clean shutdown is performed by issuing the XBee 3 Cellular ATSD (Shutdown) command to the XBee module.
    Note that on XBee 3 Zigbee and 802.15.4 devices, the ATSD command does something completely different
    (SD = Scan Duration).

    Note that ATSD command does NOT put the XBee 3 Cellular device to sleep. Rather, it simply
    puts the XBee module (particularly its cell modem chip) into a state in which it is safe to power off.
    The XBee will continue to respond to API Frames messages even after it is in this shutdown state.

    Note that the XBee 3 Cellular ATSD command takes a long time to respond, usually somewhere between
    6 and 12 seconds, but it could be longer, so give it a nice long timeout, say 30 seconds, since
    that is what the user guide recommends for alternate airplane mode shutdown approach.

    We don't retry the shutdown command in a loop because a failure is highly unlikely.
    If the shutdown command fails, we report the error, but we don't do anything else--
    chances are you're still going to turn off the power no matter what, but at least
    we tried to shut down so gracefully.

    If the XBee 3 Cellular ATSD fails, we do attempt an alternate, fallback shutdown approach.

    Note that the shutdown command (ATSD) was not introduced until the August 2019 release of
    the XBee3 Cellular firmware, so older versions of firmware will need to rely upon this
    alternate approach.

    One approach mentioned on the [Clean shutdown page of the User Guide](
    https://www.digi.com/resources/documentation/Digidocs/90002258/#Reference/r_clean_shutdown.htm)
    is to de-assert the DTR_n/SLEEP_REQUEST pin and wait until the SLEEP_STATUS pin goes low.

    However, if your XBee carrier board hardware was not designed with this in mind, you won't
    be able to monitor this. Instead, we just wait for a specific reasonable amount of time, say 30 seconds.
    After all, this is a fallback shutdown approach, and this is better than nothing.
    However, there is another, bigger issue with the DTR_n/SLEEP_REQUEST approach:
    this pin might not be configured as a sleep request input! In other words, the XBee register "D8"
    might instead have it configured as a general-purposes digital I/O pin, in which case strobing
    this pin won't work. Since I don't want to make any assumptions about how the XBee is configured,
    I'm going to avoid the pin-based approach.

    So instead, we will use the third shutdown approach described in the User Guide:
    send the ATAM (Airplane Mode) command to put the device into airplane mode. Then we wait
    30 seconds as prescribed in the User Guide to give it plenty of time to shut down.

    Note that the ATAM (Airplane Mode) command gets applied immediately;
    no need to subsequently send ATAC (Apply Configuration) to apply the changes.
    """

    proto = xbee.get_protocol()
    if proto not in (XBeeProtocol.CELLULAR, XBeeProtocol.CELLULAR_NBIOT):
        log("%s: Device is not an XBee 3 Cellular; no need for clean shutdown. Bypassing clean shutdown."
            % func())
        return Success

    xbee_atsd_timeout_sec = 30
    xbee_time_to_wait_in_airplane_mode_sec = 30

    log("%s: Adjusting the xbee-python command timeout value..." % func())
    try:
        default_timeout_sec = xbee.get_sync_ops_timeout(
        )  # Store default value so that we can put it back.
        xbee.set_sync_ops_timeout(
            xbee_atsd_timeout_sec
        )  # ATSD takes longer than most commands so give it extra time.
    except Exception as ex:
        return Error(
            "%s: Failed to adjust the xbee-python command timeout value. Reason: "
            % (func(), ex))

    log("%s: Attempting to cleanly shut down the XBee. Sending shutdown command (ATSD)..."
        % func())
    try:
        xbee.execute_command("SD")
        log("%s: XBee shutdown command (ATSD) succeeded. Now safe to remove power from the module."
            % func())
        return Success
    except Exception as ex:
        log("%s: XBee shutdown command (ATSD) failed. Reason: %s. Will attempt fallback approach."
            % (func(), ex))
    finally:
        xbee.set_sync_ops_timeout(default_timeout_sec)

    log("%s: Attempting fallback approach to cleanly shut down. Sending ATAM command and waiting for %s seconds."
        % (func(), xbee_time_to_wait_in_airplane_mode_sec))
    try:
        xbee.set_parameter("AM", b"\x01")
    except Exception as ex:
        return Error("%s: XBee ATAM command failed. Reason: %s" % (func(), ex))

    time.sleep(xbee_time_to_wait_in_airplane_mode_sec)
    log("%s: Done waiting for XBee to enter airplane mode. You may now remove power from the module."
        % func())
    return Success