Example #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 send_data(self, xbee_port, command, return_length):
        '''
        xbee_port : Socket port of xbee used
        command : command to be sent to rover
        return_length : Expected length of return message

        NOTE : This function will send the command and close.
               Calling script has to put gap between 
        '''
        device = WiFiDevice(port=self.PORT, baud_rate=self.BAUD)
        device.open()
        device.send_ip_data_broadcast(xbee_port, command)
        device.close()
Example #4
0
    def send_data(self,xbee_port,command,return_length):
        '''
        xbee_port : Socket port of xbee used
        command : command to be sent to rover
        return_length : Expected length of return message

        NOTE : This function will send the command, open the serial 
               port and wait for receive_timeout seconds for a reply
               from rover. It will receive the data and return it to 
               calling script
        '''
        device = WiFiDevice(port=self.PORT,baud_rate=self.BAUD)
        device.open()
        device.send_ip_data_broadcast(xbee_port,command)
        device.close()

        return_data = self.receive_data(return_length)
        return return_data
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()
#  bidirectional script with sending test data first

import serial
from time import sleep
from digi.xbee.devices import WiFiDevice

PORT = '/dev/ttyUSB0'
BAUD = 9600

device = WiFiDevice(PORT, BAUD)
print('XBee Device Initialized')
device.open()
print('XBee Device Opened')
device.send_ip_data_broadcast(9750, "Hello TCR2")
print('Initial Packet Sent')
device.close()
print('Device Closed')

sleep(1)

try:
    while True:
        ser = serial.Serial(PORT, BAUD)
        print('Serial Port Initialized')
        # data = ser.read_until(terminator=b'TCR1 ')
        data = ser.read(25)
        print('Data Read')
        ser.close()
        print('Serial Port Closed')
        data = data[14:-1]
        data = data.decode()