示例#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()
示例#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
#  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()
示例#6
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()
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()