示例#1
0
def main():
    print(" +-----------------------------------------+")
    print(" | XBee Python Library Send IP Data Sample |")
    print(" +-----------------------------------------+\n")

    device = WiFiDevice(PORT, BAUD_RATE)

    try:
        device.open()

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

        print("Sending data to %s:%d >> %s..." %
              (DEST_IP_ADDRESS, DEST_PORT, DATA_TO_SEND))

        device.send_ip_data(IPv4Address(DEST_IP_ADDRESS), DEST_PORT, PROTOCOL,
                            DATA_TO_SEND)

        print("Success")

    finally:
        if device is not None and device.is_open():
            device.close()
def main():
    print(" +--------------------------------------------+")
    print(" | XBee Python Library Receive IP Data Sample |")
    print(" +--------------------------------------------+\n")

    device = WiFiDevice(PORT, BAUD_RATE)

    try:
        device.open()

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

        def ip_data_received_callback(ip_message):
            print("From %s > %s" %
                  (ip_message.ip_addr, ip_message.data.decode("utf8")))

        device.add_ip_data_received_callback(ip_data_received_callback)

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

    finally:
        if device is not None and device.is_open():
            device.close()
def main():
    print(" +----------------------------------------------------+")
    print(" | XBee Python Library Connect to Access Point Sample |")
    print(" +----------------------------------------------------+\n")

    device = WiFiDevice(PORT, BAUD_RATE)

    try:
        device.open()

        if device.is_connected():
            device.disconnect()

        device.set_ip_addressing_mode(IPAddressingMode.DHCP)

        def modem_status_receive_callback(modem_status):
            print("Modem status: %s" % modem_status.description)
            if modem_status == ModemStatus.JOINED_NETWORK:
                print(">> Successfully connected to '%s'" % SSID)
            elif modem_status == ModemStatus.STATUS_DISASSOCIATED:
                print(">> Disconnected from the access point")

        device.add_modem_status_received_callback(
            modem_status_receive_callback)

        if not device.connect_by_ssid(SSID, PASSWORD):
            print(">> Error: could not connect to '%s'\n" % SSID)

        print("")
        print("  - IP addressing mode: %s" %
              device.get_ip_addressing_mode().description)
        print("  - IP address:         %s" % device.get_ip_addr().exploded)
        print("  - IP address mask:    %s" %
              device.get_mask_address().exploded)
        print("  - Gateway IP address: %s" %
              device.get_gateway_address().exploded)
        print("  - DNS address:        %s" % device.get_dns_address().exploded)
        print("")

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