pythonbeagleboneblackxbee

How to send an API frame to a remote XBee using Digi XBee Python module?


I am moving code from Python XBee library to Digi's Python XBee library and I am unable to locate syntax in the docs on how to send a API frame to a remote XBee. I am using S1 and S1 Pro devices where the "local" device is attached to a Beaglebone and the "remote" devices are standalone out in the wild.

I have the basic framework down:

from digi.xbee.devices import XBeeDevice, RemoteXBeeDevice, XBee64BitAddress

PORT = '/dev/ttyO1'
BAUD = 9600
API_FRAME = 'long-hex-string-here'

local_xbee = XBeeDevice(PORT, BAUD)
local_xbee.open()

# Instantiate a remote XBee device object.
remote_xbee = RemoteXBeeDevice(local_xbee, XBee64BitAddress.from_hex_string("0013A20040DD7DCD"))

# Transmit frame to remote_xbee - unsure of correct method and syntax
# perhaps XBeePacket.create_packet(hex string here)
# local_xbee.send_data(remote_xbee, "api frame here?")  ??

local_xbee.close()

but I am unable to locate syntax for how to transmit my constructed API frame. Based upon the Introduction section in the docs I presume that is the correct approach. I am not interested in broadcasting to all devices on the network but rather unicast communication.


Solution

  • I have some older model of the source when I could get it to work.

    I have some WiFi Xbee modules that I used w/ some converter boards (base boards). I used it to attach the communication of the Xbee from one computer to another, e.g. random desktop to BeagleBone Black via USB instead of UART. So, I would use the source, listed below, to attach my USB dongle for the Xbee communication from the BBB to the other field module.

    Their I/O stuff can be found here: https://github.com/digidotcom/xbee-python/tree/master/examples/io.

    Also...changing just some of their lines in their source w/ the USB dongle WiFi adapter boards proved valuable in signaling LEDs and other sensors.

    Oh and you will need what they are now calling Carrier Boards. It is the adapter board I just typed out. So, if you have already got a Carrier Board, use lsusb as the command in Linux to find your USB "name."

    So, for instance, if lsusb brings up /dev/ttyUSB0, then that is the port identification.

    And you can use that section, from lsusb, to then change your xbee modules in the xtcu software from Digi.

    from digi.xbee.devices import XBeeDevice
    from digi.xbee.io import IOLine, IOMode
    import time
    import threading
    
    # TODO: Replace with the serial port where your local module is connected to.
    PORT = "/dev/ttyUSB0"
    # TODO: Replace with the baud rate of your local module.
    BAUD_RATE = 9600
    
    REMOTE_NODE_ID = "Xbee_B"
    
    IOLINE_IN = IOLine.DIO2_AD2
    IOLINE_OUT = IOLine.DIO4_AD4
    
    def main():
        print(" +-----------------------------------------------+")
        print(" | XBee Python Library Get/Set Remote DIO Sample |")
        print(" +-----------------------------------------------+\n")
    
        stop = False
        th = None
    
        local_device = XBeeDevice(PORT, BAUD_RATE)
    
        try:
            local_device.open()
            print("local device: ", local_device.get_node_id())
            # Obtain the remote XBee device from the XBee network.
            xbee_network = local_device.get_network()
            remote_device = xbee_network.discover_device(REMOTE_NODE_ID)
            if local_device is None:
                print("Could not find the remote device")
                exit(2)
    
            def io_detection_callback():
                while not stop:
                    # Read the digital value from the input line.
                    io_value = remote_device.get_dio_value(IOLINE_IN)
                    print("%s: %s" % (IOLINE_IN, io_value))
    
                    # Set the previous value to the local output line.
                    local_device.set_dio_value(IOLINE_OUT, io_value)
    
                    time.sleep(2)
    
            th = threading.Thread(target=io_detection_callback)
    
            remote_device.set_io_configuration(IOLINE_IN, IOMode.DIGITAL_IN)
    
            local_device.set_io_configuration(IOLINE_OUT, IOMode.DIGITAL_OUT_HIGH)
    
            time.sleep(1)
            th.start()
    
            input()
    
        finally:
            stop = True
            if th is not None and th.is_alive():
                th.join()
            if local_device is not None and local_device.is_open():
                local_device.close()
    
    
    if __name__ == '__main__':
        main()
    

    So, see the PORT = "/dev/ttyUSB0" section of the source?

    This is where I attached my Xbee module to the Carrier Board and then attached the Carrier Board to the BBB by way of USB.

    Um, this may not answer a question but give more insight as to how to handle Digi Devices/Modules.

    I also think that if you want to venture in this direction of UART communication w/ Xbee and the BeagleBone Black, it may be more complicated. I will keep searching my text.

    P.S. This book goes over some methods to connect, Experiment 10 and Experiment 16, your "BBB" to a UART, Xbee, and how to communicate. It is a bit too in depth to get all of the communication ideas from this book but this is it:

    The Hands-on XBEE Lab Manual, Experiments that Teach you XBEE Wireless Communications – Jonathan A Titus