1

I am an absolute newbie when coming to using these modules. I am trying to setup a basic UDP connection between my XBee S6B (UDP Client) to my computer (UDP Server). I have added my codes below. The XBee program (UDP client) is a small modification of the xbee python library on git made by Digi.

Note: Both devices (the xbee and computer are connected to the same wifi router).

UDP Server (computer)

import socket 

HOST = "127.0.0.1"
PORT = 20001

sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
sock.bind((HOST,PORT))

while True:
    recv = sock.recv(1024)
    print(f"message: {recv[0]}\tIP: {recv[1]}")

UDP Client (XBee)

from digi.xbee import xsocket
from digi.xbee.devices import CellularDevice
from digi.xbee.models.protocol import IPProtocol

# TODO: Replace with the serial port where your local module is connected to.
PORT = "COM22"
# TODO: Replace with the baud rate of your local module.
BAUD_RATE = 115200
LISTEN_PORT = hex(5007)
ECHO_SERVER_ADDRESS = "192.168.0.131"
ECHO_SERVER_PORT = 20001
# TODO: Optionally, replace with the text to be sent to the server.
ECHO_SERVER_REQUEST = "May the force be with you"


def main():
    print(" +-----------------------------------------------------+")
    print(" | XBee Python Library Socket UDP Server Client Sample |")
    print(" +-----------------------------------------------------+\n")

    device = CellularDevice(PORT, BAUD_RATE)

    try:
        device.open()

        with xsocket.socket(device, IPProtocol.UDP) as sock:
            print("- Starting UDP server at port %s" % LISTEN_PORT)
            sock.bind(("0.0.0.0", LISTEN_PORT))
            print("- Sending '%s' to the echo server" % ECHO_SERVER_REQUEST)
            sock.sendto(ECHO_SERVER_REQUEST.encode("utf-8"), (ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT))
            print("- Waiting for incoming data")
            answer, address = sock.recvfrom(4096)
            if answer is not None:
                print("- Data received from %s:%s - '%s'" % (address[0], address[1], answer.decode("utf-8")))
    finally:
        if device is not None and device.is_open():
            device.close()


if __name__ == '__main__':
    main()

I am having difficulty understanding how to fix the errors I am facing. I know I have to change something on XCTU but I am unsure what. I have already set AP = 1 which means its in API mode.

  File "C:\Users\DELL\AppData\Local\Programs\Python\Python310\lib\site-packages\digi\xbee\devices.py", line 449, in _check_at_cmd_response_is_valid
    raise ATCommandException(message=response.status.description,
digi.xbee.exception.ATCommandException: Invalid command

Any suggestion on this would be highly appreciated!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.