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()
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()
# 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()