Exemplo n.º 1
0
    def test_cellular_functionality():
        print("Experimenting with python-xbee library - cellular functions")

        from digi.xbee.devices import CellularDevice
        from digi.xbee.models.protocol import IPProtocol
        from ipaddress import IPv4Address

        device = CellularDevice("COM7", 115200)
        #import pdb; pdb.set_trace()

        device.open()
        device.send_ip_data(
            IPv4Address("52.43.121.77"), 11001, IPProtocol.TCP,
            "Hello XBee World!")  # Send a message to Digi's echo server.
        ip_message = device.read_ip_data()
        print(
            ip_message.data.decode("utf8"
                                   ) if ip_message is not None else "ERROR")

        xbee_message = device.read_data()
        remote_device = xbee_message.remote_device
        data = xbee_message.data
        is_broadcast = xbee_message_is_broadcast
        timestamp = xbee_message.timestamp

        device.close()
Exemplo n.º 2
0
def main():
    print(" +----------------------------------------------+")
    print(" | XBee Python Library Socket TCP Server Sample |")
    print(" +----------------------------------------------+\n")

    device = CellularDevice(PORT, BAUD_RATE)

    try:
        device.open()

        with xsocket.socket(device) as sock:
            print("- Starting TCP server at port %s" % LISTEN_PORT)
            sock.bind(("0.0.0.0", LISTEN_PORT))
            sock.listen()
            print("- Waiting for client")
            client, client_addr = sock.accept()
            print("- Client '%s:%s' connected" %
                  (client_addr[0], client_addr[1]))
            print("- Waiting for incoming data")
            answer = None
            while not answer and client.is_connected:
                answer = client.recv(4096)

            if answer is not None:
                print("- Data received: %s" % answer.decode("utf-8"))
                print("- Sending data back to the client")
                client.sendall(answer)
            else:
                print("- Could not receive data from the client")
            print("- Closing client socket")
            client.close()

    finally:
        if device is not None and device.is_open():
            device.close()
Exemplo n.º 3
0
def main():
    print(" +-------------------------------------+")
    print(" | XBee Python Library Send SMS Sample |")
    print(" +-------------------------------------+\n")

    device = CellularDevice(PORT, BAUD_RATE)

    try:
        device.open()

        print("Sending SMS to %s >> %s..." % (PHONE, SMS_TEXT))

        device.send_sms(PHONE, SMS_TEXT)

        print("Success")

    finally:
        if device is not None and device.is_open():
            device.close()
Exemplo n.º 4
0
def main():
    print(" +----------------------------------------+")
    print(" | XBee Python Library Receive SMS Sample |")
    print(" +----------------------------------------+\n")

    device = CellularDevice(PORT, BAUD_RATE)

    try:
        device.open()

        def sms_received_callback(sms_message):
            print("From %s >> '%s'" %
                  (sms_message.phone_number, sms_message.data))

        device.add_sms_callback(sms_received_callback)

        print("Waiting for SMS...\n")
        input()

    finally:
        if device is not None and device.is_open():
            device.close()
Exemplo n.º 5
0
    def test_get_parameter():
        print("Experimenting with python-xbee library - query parameters")

        from digi.xbee.devices import CellularDevice, XBeeDevice

        # Enable logging in the python-xbee library.
        import logging, digi.xbee.devices, digi.xbee.reader
        logging.getLogger(digi.xbee.devices.__name__).setLevel(logging.DEBUG)
        logging.getLogger(digi.xbee.reader.__name__).setLevel(logging.DEBUG)

        xb = CellularDevice(
            "COM7", 115200)  # Alternatively: XBeeDevice("COM7", 115200)
        xb.open()

        p = xb.get_parameter("AP")
        print("GOT: %s" % p)
        xb.close()
def main():
    print(" +----------------------------------------------+")
    print(" | XBee Python Library Socket TCP Client Sample |")
    print(" +----------------------------------------------+\n")

    device = CellularDevice(PORT, BAUD_RATE)

    try:
        device.open()

        with xsocket.socket(device) as sock:
            print("- Connecting to '%s'" % DEST_ADDRESS)
            sock.connect((DEST_ADDRESS, DEST_PORT))
            print("- Sending request text to the server")
            sock.send(REQUEST_TEXT.encode("utf-8"))
            print("- Waiting for the answer")
            answer = sock.recv(4096)
            print("- Data received:")
            print(answer.decode("utf-8"))

    finally:
        if device is not None and device.is_open():
            device.close()
Exemplo n.º 7
0
    def test_xbee_filesystem_commands():
        print(
            "Need to reverse-engineer syntax of the API Frames command with an AT Command that takes a parameter that is longer than a single byte."
        )

        from digi.xbee.devices import CellularDevice

        import logging, digi.xbee.devices, digi.xbee.reader
        logging.getLogger(digi.xbee.devices.__name__).setLevel(logging.DEBUG)

        xb = CellularDevice("COM7", 115200)
        xb.open()

        try:
            # I scoured the [xbee-python library docs](https://xbplib.readthedocs.io/en/latest/index.html)
            # to see if they have any support for the ATFS (file system) commands, but I couldn't find anything.
            # The closest I could find were these:
            #   p = xb.execute_command("FS HASH /flash/missing.txt")    # DOESN'T WORK
            #   p = xb.set_parameter("FS", b"HASH /flash/missing.txt")  # DOESN'T WORK
            # But they didn't work.
            #
            # Aha! Just found the following notes in the [XBee3 Cellular User Guide](https://www.digi.com/resources/documentation/Digidocs/90002258/#Containers/cont_at_cmd_file_system.htm):
            #  "To access the file system, Enter Command mode and use the following commands.
            #   All commands block the AT command processor until completed and only work from Command mode;
            #   they are not valid for API mode or MicroPython's xbee.atcmd() method."
            #
            # So that answers my question.
            #
            # So this experiment has changed my perspective a little bit. I'm now thinking
            # that we should use AT Command Mode to do a bunch of the configuration,
            # rather than the AT Command API Frame message. Good to know!
            #
            # For more information on the xbee.atcmd() MicroPython function: https://www.digi.com/resources/documentation/digidocs/90002219/#reference/r_function_atcmd.htm
            pass
        except Exception as ex:
            print("GOT: %s" % ex)
        #import pdb; pdb.set_trace()
        xb.close()
Exemplo n.º 8
0
    def test_tcp():
        '''
        Test sending a simple TCP message using the xbee-python library.
        '''
        print("Running TestPythonXBeeLibrary.test_tcp()...")

        print("Running TestPythonXBeeLibrary.test_aws_connection()...")

        # @todo enable logging

        from ipaddress import IPv4Address
        from digi.xbee.devices import CellularDevice
        from digi.xbee.models.protocol import IPProtocol

        xb = CellularDevice("COM7", 115200)

        xb.open()

        DEST_ADDR = "107.22.215.20"  # @todo implement DNS lookup for the aws endpoint! For now I've just hard-coded it.
        DEST_PORT = 80
        PROTOCOL = IPProtocol.TCP

        tx = b"GET / HTTP/1.1\r\nHost: api.ipify.org\r\nUser-Agent: curl/7.64.0\r\nAccept: */*\r\n\r\n"

        # The TCP request might take a little longer than the default so increase timeout.
        print("Old timeout: %s" % xb.get_sync_ops_timeout())
        xb.set_sync_ops_timeout(10)
        print("New timeout: %s" % xb.get_sync_ops_timeout())

        # Send the data.
        xb.send_ip_data(IPv4Address(DEST_ADDR), DEST_PORT, PROTOCOL, tx)
        print("TX: %s" % tx)

        # Receive response.
        rx = xb.read_ip_data()
        if rx is None:
            print("Response was not received from the server.")
        else:
            print("RX: %s" % rx.data.decode("utf8"))

        xb.close()
Exemplo n.º 9
0
    def test_aws_connection():
        '''
        Based on https://github.com/digidotcom/python-xbee/blob/master/examples/communication/ip/ConnectToEchoServerSample/ConnectToEchoServerSample.py
        which is documented on https://xbplib.readthedocs.io/en/latest/user_doc/communicating_with_xbee_devices.html
        '''

        print("Running TestPythonXBeeLibrary.test_aws_connection()...")

        # @todo enable logging

        from ipaddress import IPv4Address
        from digi.xbee.devices import CellularDevice
        from digi.xbee.models.protocol import IPProtocol

        xb = CellularDevice("COM7", 115200)

        try:
            xb.open()

            #####################

            #DEST_ADDR = "52.43.121.77"
            #DEST_PORT = 11001
            #PROTOCOL = IPProtocol.TCP
            #tx = "Hello XBee!"

            #####################

            aws_endpoint = b'ak87jc7d58d2m.iot.us-east-2.amazonaws.com'
            #aws_endpoint = b'ak87jc7d58d2m-ats.iot.us-east-2.amazonaws.com'
            thing_type = b'XBee3Cellular'
            thing_name = b'DanXBee'

            DEST_ADDR = "18.217.238.169"  # @todo Implement DNS lookup for the aws endpoint! For now I've just hard-coded DEST_ADDR.
            DEST_PORT = 8443
            PROTOCOL = IPProtocol.TCP_SSL

            tx = b"GET /things/%s/shadow HTTP/1.0\r\nHost: %s\r\n\r\n" % (
                thing_name, aws_endpoint)

            # Set up TLS v1.2 and paths to certs.
            xb.set_parameter("TL", b"\x03")
            xb.set_parameter(
                "$0",
                b"/flash/cert/aws.ca;/flash/cert/aws.crt;/flash/cert/aws.key")
            print("TL: %s" % xb.get_parameter("TL"))
            print("$0: %s" % xb.get_parameter("$0"))

            # The TLS requests might take a little longer than the default so increase timeout.
            print("Old timeout: %s" % xb.get_sync_ops_timeout())
            xb.set_sync_ops_timeout(10)
            print("New timeout: %s" % xb.get_sync_ops_timeout())

            #####################

            xb.send_ip_data(IPv4Address(DEST_ADDR), DEST_PORT, PROTOCOL, tx)
            print("TX: %s" % tx)

            rx = xb.read_ip_data()
            if rx is None:
                print("Echo response was not received from the server.")
            else:
                print("RX: %s" % rx.data.decode("utf8"))

        except RuntimeError as ex:  #Exception as ex:
            print("Got exception! %s" % ex)

        finally:
            print("Cleaning up.")
            if xb is not None and xb.is_open():
                xb.close()
def main():
    print(" +---------------------------------------------------+")
    print(" | XBee Python Library Connect to Echo Server Sample |")
    print(" +---------------------------------------------------+\n")

    device = CellularDevice(PORT, BAUD_RATE)

    try:
        device.open()

        if not device.is_connected():
            print(">> Error: the device is not connected to the network")
            return

        print("Sending text to %s:%d >> %s..." %
              (ECHO_SERVER, ECHO_SERVER_PORT, TEXT))

        device.send_ip_data(IPv4Address(ECHO_SERVER), ECHO_SERVER_PORT,
                            PROTOCOL, TEXT)

        print("Success")

        # Read the echoed data.
        ip_message = device.read_ip_data()
        if ip_message is None:
            print("Echo response was not received from the server.")
            return

        print("Echo response received from %s:%d >> '%s'" %
              (ip_message.ip_addr, ip_message.source_port,
               ip_message.data.decode("utf8")))

    finally:
        if device is not None and device.is_open():
            device.close()
Exemplo n.º 11
0
# visit https://pypi.org/project/rssi/ for ref in rssi
#  1- importing the libarary
#  2- choosing between Xbeedevice or wifidevice
#  3-  XBeeDevice is for sending meesseages and stuff
#  4-  WifiDevice is for sending Data
#  5-  CellularDevice for remote xbee
#  6- IPprotocol & IPv4Address is for server config
from digi.xbee.devices import XBeeDevice, WiFiDevice, CellularDevice, IPProtocol, IPv4Address
from rssi import RSSI_Scan, RSSI_Localizer  # imports rssi library
# using XBeeDevice
device = XBeeDevice("COM1",
                    9600)  # Com replaced with the port ,, 9600 is the baudrate
device.open()  #opens the conncetion with the device
device.send_data_broadcast("Hello World!")  # broadcast the message
device.colse()  # closes the connection
# using WiFiDevice
device2 = WiFiDevice("COM", 9600)  # same as above with modification
device2.open()
device2.send_ip_data_broadcast(9750, "hello world ")  #destination port , Data
device2.colse()
# sending data to remote
device3 = CellularDevice("COM1", 9600)
device3.open()
#sending  to server ip with port 11001 using TCP protocol
device3.send_ip_data(IPv4Address("52.43.121.77"), 11001, IPProtocol.TCP,
                     "Hello XBee World!")
#Read and print the response from the echo server. If response cannot be received, print ERROR.
ip_message = device3.read_ip_data()
print(ip_message.data.decode("utf8") if ip_message is not None else "ERROR")
device3.colse()