def xbee_0(): #Master device - polls remote device. device = XBeeDevice('COM4', 230400) remote_addr = '' # Insert remote address remote_device = RemoteXBeeDevice( device, XBee64BitAddress.from_hex_string(remote_addr)) device.open() # Represents the largest single 'packet' that can be transmitted. #msg = b'0' * 249 msg = "Alive?" pickled = pickle.dumps(msg) while True: try: rcv = device.read_data(1) if rcv is None: continue print('COM4 (Master) recieved:\n\tDevice: {0:}\n\t Value: {1:}\n'. format(remote_addr, pickle.loads(rcv.data))) except TimeoutException: device.send_data(remote_device, pickled) continue device.send_data(remote_device, pickled) device.close()
def main(): print(" +-----------------------------------------------+") print(" | XBee Python Library Set/Get parameters Sample |") print(" +-----------------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() # Set parameters. device.set_parameter(PARAM_NODE_ID, bytearray(PARAM_VALUE_NODE_ID, 'utf8')) device.set_parameter(PARAM_PAN_ID, PARAM_VALUE_PAN_ID) device.set_parameter(PARAM_DEST_ADDRESS_H, PARAM_VALUE_DEST_ADDRESS_H) device.set_parameter(PARAM_DEST_ADDRESS_L, PARAM_VALUE_DEST_ADDRESS_L) # Get parameters. print("Node ID: %s" % device.get_parameter(PARAM_NODE_ID).decode()) print("PAN ID: %s" % utils.hex_to_string(device.get_parameter(PARAM_PAN_ID))) print("Destination address high: %s" % utils.hex_to_string(device.get_parameter(PARAM_DEST_ADDRESS_H))) print("Destination address low: %s" % utils.hex_to_string(device.get_parameter(PARAM_DEST_ADDRESS_L))) print("") print("All parameters were set correctly!") finally: if device is not None and device.is_open(): device.close()
def main(): print(" +---------------------+") print(" | Read IO Sample Test |") print(" +---------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() device.set_io_configuration(IOLine.DIO1_AD1, IOMode.DIGITAL_IN) sample = device.read_io_sample() assert (sample.has_digital_value(IOLine.DIO1_AD1)) remote = RemoteXBeeDevice(device, x64bit_addr=REMOTE_DEVICE_ADDRESS) remote.set_io_configuration(IOLine.DIO1_AD1, IOMode.DIGITAL_IN) sample = remote.read_io_sample() assert (sample.has_digital_value(IOLine.DIO1_AD1)) print("Test finished successfully") finally: if device is not None and device.is_open(): device.close()
def main(): print(" +----------------------------------------------+") print(" | XBee Python Library Format Filesystem Sample |") print(" +----------------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() filesystem_manager = LocalXBeeFileSystemManager(device) print("Starting file system manager...", end=" ") filesystem_manager.connect() print("OK\n") get_fs_info(filesystem_manager) print("\nFormatting filesystem...", end=" ") filesystem_manager.format_filesystem() print("OK\n") get_fs_info(filesystem_manager) except (XBeeException, FileSystemException) as e: print("ERROR: %s" % str(e)) exit(1) finally: if filesystem_manager.is_connected: print("\nStopping file system manager...", end=" ") filesystem_manager.disconnect() print("OK") if device is not None and device.is_open(): device.close()
def main(): print(" +---------------------------------------------------+") print(" | XBee Python Library Remote Firmware Update Sample |") print(" +---------------------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() # Obtain the remote XBee device from the XBee network. xbee_network = device.get_network() remote_device = xbee_network.discover_device(REMOTE_NODE_ID) if remote_device is None: print("Could not find the remote device") exit(1) print("Starting firmware update process...") remote_device.update_firmware( XML_FIRMWARE_FILE, xbee_firmware_file=OTA_FIRMWARE_FILE, bootloader_firmware_file=OTB_FIRMWARE_FILE, progress_callback=progress_callback) print("Firmware updated successfully!") except (XBeeException, FirmwareUpdateException, OperationNotSupportedException) as e: print("ERROR: %s" % str(e)) exit(1) finally: if device is not None and device.is_open(): device.close()
class XBee: def __init__(self, port: str, baud_rate: int = 9600): self._device = XBeeDevice(port, baud_rate) def _normalize_data(self, data: typing.Union[bytearray, bytes]): if isinstance(data, bytes): data = bytearray(data) return data def open(self): self._device.open() self._device.add_data_received_callback(self._on_data_received) def close(self): self._device.close() def send_broadcast(self, data: typing.Union[bytearray, bytes]): data = self._normalize_data(data) self._device.send_data_broadcast(data) def send(self, remote_address: bytearray, data: typing.Union[bytearray, bytes]): data = self._normalize_data(data) remote_device = RemoteXBeeDevice( self._device, x64bit_addr=XBee64BitAddress(remote_address)) self._device.send_data(remote_device, data) def on_message_received(self, remote_address: bytearray, data: bytearray): pass def _on_data_received(self, message): self.on_message_received( message.remote_device.get_64bit_addr().address, message.data)
def main(): print(" +-------------------------------------------+") print(" | XBee Python Library List Directory Sample |") print(" +-------------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) filesystem_manager = LocalXBeeFileSystemManager(device) try: device.open() filesystem_manager = LocalXBeeFileSystemManager(device) print("Starting file system manager...", end=" ") filesystem_manager.connect() print("OK\n") current_directory = filesystem_manager.get_current_directory() print("Current directory: %s" % current_directory) path_to_list = PATH_TO_LIST if path_to_list is None: path_to_list = current_directory files = filesystem_manager.list_directory(path_to_list) print("Contents of '%s':" % path_to_list) for file in files: print(str(file)) except (XBeeException, FileSystemException) as e: print("ERROR: %s" % str(e)) exit(1) finally: if filesystem_manager.is_connected: print("\nStopping file system manager...", end=" ") filesystem_manager.disconnect() print("OK") if device is not None and device.is_open(): device.close()
def main(): print(" +-----------------------------------------+") print(" | XBee Python Library Receive Data Sample |") print(" +-----------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() def data_receive_callback(xbee_message): print("From %s >> %s" % (xbee_message.remote_device.get_64bit_addr(), xbee_message.data.decode())) message = xbee_message.data.decode() if message == "cutdown": return CUTDOWN_VALUE else: return SAFE_VALUE device.add_data_received_callback(data_receive_callback) print("Waiting for data...\n") input() finally: if device is not None and device.is_open(): device.close()
def main(): print(" +-------------------+") print(" | Send Data 16 Test |") print(" +-------------------+\n") local = XBeeDevice(PORT_LOCAL, BAUD_RATE_LOCAL) local_remote = XBeeDevice(PORT_REMOTE, BAUD_RATE_REMOTE) try: local.open() local_remote.open() remote = RemoteXBeeDevice(local, x16bit_addr=local_remote.get_16bit_addr()) local.send_data(remote, "Test message") time.sleep(1) message = local_remote.read_data() assert (message is not None) assert ( message.remote_device.get_16bit_addr() == local.get_16bit_addr()) print("Test finished successfully") finally: if local is not None and local.is_open(): local.close() if local_remote is not None and local_remote.is_open(): local_remote.close()
def main(): print("Libelium Waspmote data receiver\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() device.flush_queues() print("Waiting for data...\n") while True: xbee_message = device.read_data(300) if xbee_message is not None: # print("From %s >> %s" % (xbee_message.remote_device.get_64bit_addr(), # xbee_message.data)) if b"<=>" in xbee_message.data: data = xbee_message.data[5:] print(data.decode()) except KeyboardInterrupt: print("Exit") finally: if device is not None and device.is_open(): device.close()
def main(): print(" +--------------------------------------+") print(" | XBee Python Library Send Data Sample |") print(" +--------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() print("open device") # Obtain the remote XBee device from the XBee network. xbee_network = device.get_network() remote_device = xbee_network.discover_device(REMOTE_NODE_ID) if remote_device is None: print("Could not find the remote device. We will try 5 second") time.sleep(5) #exit(1) numero = 300 count = 0 #numero = int(input("Escriba un número positivo: ")) while (count < numero): DATA_TO_SEND = "Fecha: " + dt.datetime.now().strftime('%Y-%m-%d Hora: %H:%M:%S') + " Temperatura: 24.7 Humedad: 33% Presion 44mb Co2: 674.5" + "\n" time.sleep(2) count = count + 1 print("Sending data to %s >> %s..." % (remote_device.get_64bit_addr(), DATA_TO_SEND)) device.send_data(remote_device, DATA_TO_SEND) print("Success") finally: if device is not None and device.is_open(): device.close()
def main(): print(" +--------------------------------------+") print(" | Sender (Station) |") print(" +--------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() # Obtain the remote XBee device from the XBee network. xbee_network = device.get_network() remote_device = xbee_network.discover_device(REMOTE_NODE_ID) commActive = True if remote_device is None: print("Could not find the remote device") exit(1) while commActive: print(remote_device) action = input("action: ") device.send_data_async(remote_device, action) print("Success") if action == "exit": commActive = False finally: if device is not None and device.is_open(): device.close()
def main(): if os.path.exists("/tmp/data.sock"): os.remove("/tmp/data.sock") device = XBeeDevice(PORT, BAUD_RATE) socketServer = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) socketServer.bind(data) socketServer.listen(10) socketConnection, _ = socketServer.accept() try: device.open() def data_receive_callback(xbee_message): print("From %s >> %s" % (xbee_message.remote_device.get_16bit_addr(), xbee_message.data.decode())) list = xbee_message.data.decode() list = list.replace('\n', ' ').replace('\r', '').replace('\x00', '') list = list.split(',') msg = ','.join(list).encode('UTF-8') socketConnection.send(msg) device.add_data_received_callback(data_receive_callback) print("Waiting for data...\n") input() except Error: main() finally: if device is not None and device.is_open(): device.close() socketServer.close()
def main(): coord = XBeeDevice('COM7', 9600) #create Xbeedevice Coord at COM7 & baud rate 9600 try: coord.open() #calling method to open communication with Xbee device coord.flush_queues() xbee_network = coord.get_network() #getting a newtwork and assigning it to the xbee router2 = xbee_network.discover_device('R3') # find the remote device on the network at R1 if router2 is None: print("Could not find the remote device") else : print("Remote device found") while True: xbee_message = coord.read_data() if xbee_message is not None: timestamp = round(time.time()) data_raw = xbee_message.data.decode().split(",") data = [float(element) for element in data_raw] # Temperature print("At {0} Temperature is {1}".format(timestamp, data[0]), end=": ") if data[0] > 23.7: print("Unsafe") if router2 is not None: coord.send_data(router2, "T1") else: print("Safe") if router2 is not None: coord.send_data(router2, "T0") finally: if coord is not None and coord.is_open(): coord.close()
def main(): print(" +-----------------------------------------+") print(" | Wireless Sleep Monitoring |") print(" | \"No Strings Attached!\" |") print(" +-----------------------------------------+\n") inp = input("Press enter to start!\nPress enter a second time to stop!") #declaring device and grapher device = XBeeDevice(PORT, BAUD_RATE) try: #Opening device device.open() device.flush_queues() def data_receive_callback(xbee_message): print(xbee_message.data.decode()) device.add_data_received_callback(data_receive_callback) print("Waiting for data...\n") input() #For some reason python 3.8 causes delay here finally: if device is not None and device.is_open(): #Closing device stops callbacks device.close()
def main(): print(" +-----------------------+") print(" | Discover Network Test |") print(" +-----------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() network = device.get_network() devices_callback = [] def device_discovered_callback(remote): devices_callback.append(remote) network.add_device_discovered_callback(device_discovered_callback) network.start_discovery_process() while network.is_discovery_running(): time.sleep(0.1) assert(devices_callback == network.get_devices()) print("Test finished successfully") finally: if device is not None and device.is_open(): device.close()
def main(): print(" +---------------------+") print(" | PWM Duty Cycle Test |") print(" +---------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() device.set_io_configuration(IOLine.DIO10_PWM0, IOMode.PWM) device.set_io_configuration(IOLine.DIO11_PWM1, IOMode.PWM) device.set_pwm_duty_cycle(IOLine.DIO10_PWM0, 50) device.set_pwm_duty_cycle(IOLine.DIO11_PWM1, 100) dc1 = device.get_pwm_duty_cycle(IOLine.DIO10_PWM0) dc2 = device.get_pwm_duty_cycle(IOLine.DIO11_PWM1) assert (dc1 == 50) assert (dc2 == 100) print("Test finished successfully") finally: if device is not None and device.is_open(): device.close()
def main(): print(" +------------------------------------------------------+") print(" | XBee Python Library Apply XBee Profile Remote Sample |") print(" +------------------------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() # Obtain the remote XBee device from the XBee network. xbee_network = device.get_network() remote_device = xbee_network.discover_device(REMOTE_NODE_ID) if remote_device is None: print("Could not find the remote device") exit(1) print("Updating profile '%s'...\n" % PROFILE_PATH) remote_device.apply_profile(PROFILE_PATH, progress_callback=progress_callback) print("\nProfile updated successfully!") except Exception as e: print(str(e)) exit(1) finally: if device.is_open(): device.close()
def main(): print(" +--------------------------------------+") print(" | XBee Receive Data Node |") print(" +--------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) pub = rospy.Publisher('left_drive', Int16, queue_size=10) # rospy.init_node('teleop_comms', anonymous=True) # rate = rospy.Rate(1000) # 1000hz to be changed later if needed device.open() msg = 0 while not rospy.is_shutdown(): def data_receive_callback(xbee_message): #l_drive = xbee_message.data.decode() l_drive = xbee_message.data msg = hexint(l_drive) #print(msg) pub.publish(Int16(msg)) device.add_data_received_callback(data_receive_callback) print("Waiting for data...\n") #pub.publish(msg) rospy.spin() rate.sleep() # print("*******Closing Device************") device.close()
def main(): print(" +-------------------------------------------+") print(" | XBee Python Library Read Local ADC Sample |") print(" +-------------------------------------------+\n") th = None device = XBeeDevice(PORT, BAUD_RATE) try: device.open() device.set_io_configuration(IOLINE_IN, IOMode.ADC) stop = False def polling_adc(): while not stop: # Read the analog value from the input line. value = device.get_adc_value(IOLINE_IN) print("%s: %d" % (IOLINE_IN, value)) time.sleep(1) th = threading.Thread(target=polling_adc) th.start() input() finally: stop = True if th is not None and th.isAlive(): th.join() if device is not None and device.is_open(): device.close()
def main(): print(" +--------------------------------------+") print(" | XBee Python Library Send Data Sample |") print(" +--------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) data_to_send = "this be the data homie" try: device.open() # Obtain the remote XBee device from the XBee network. # xbee_network = device.get_network() # remote_device = xbee_network.discover_device(REMOTE_NODE_ID) # if remote_device is None: # print("Could not find the remote device") # exit(1) # print("Sending data to %s >> %s..." % (remote_device.get_64bit_addr(), DATA_TO_SEND)) # device.send_data(remote_device, DATA_TO_SEND) i = 0 while i < 5: i = i + 1 device.send_data_broadcast(data_to_send) print("Success on iteration: ", i) finally: if device is not None and device.is_open(): device.close()
def main(): print(" +-----------------------------------------------------+") print(" | XBee Python Library Manage Common parameters Sample |") print(" +-----------------------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() print("Cached parameters\n" + "-" * 50) print("64-bit address: %s" % device.get_64bit_addr()) print("16-bit address: %s" % device.get_16bit_addr()) print("Node Identifier: %s" % device.get_node_id()) print("Firmware version: %s" % utils.hex_to_string(device.get_firmware_version())) print("Hardware version: %s" % device.get_hardware_version().description) print("") # Configure and read non-cached parameters. device.set_pan_id(PARAM_VALUE_PAN_ID) device.set_dest_address(PARAM_DESTINATION_ADDR) device.set_power_level(PARAM_POWER_LEVEL) print("Non-Cached parameters\n" + "-" * 50) print("PAN ID: %s" % utils.hex_to_string(device.get_pan_id())) print("Dest address: %s" % device.get_dest_address()) print("Power level: %s" % device.get_power_level().description) finally: if device is not None and device.is_open(): device.close()
def main(): print(" +-------------------------------------------+") print(" | XBee Python Library List Directory Sample |") print(" +-------------------------------------------+\n") local_xbee = XBeeDevice(PORT, BAUD_RATE) fs_xbee = local_xbee try: local_xbee.open() if REMOTE_NODE_ID: # Obtain the remote XBee from the network. xbee_network = local_xbee.get_network() fs_xbee = xbee_network.discover_device(REMOTE_NODE_ID) if not fs_xbee: print("Could not find remote device '%s'" % REMOTE_NODE_ID) exit(1) filesystem_manager = fs_xbee.get_file_manager() path_to_list = PATH_TO_LIST if not path_to_list: path_to_list = "/flash" files = filesystem_manager.list_directory(path_to_list) print("Contents of '%s' (%s):\n" % (path_to_list, fs_xbee if fs_xbee.is_remote() else "local")) for file in files: print(file) except (XBeeException, FileSystemException) as e: print("ERROR: %s" % str(e)) exit(1) finally: if local_xbee and local_xbee.is_open(): local_xbee.close()
def main(): Number = 0 while (1): try: Port = "/dev/ttyUSB" + str(Number) device = XBeeDevice(Port, Baud_Rate) device.open() # Obtain the remote XBee device from the XBee network. xbee_network = device.get_network() remote_device = xbee_network.discover_device(REMOTE_NODE_ID) if remote_device is None: print("Could not find the remote device") exit(1) time_string = strftime('%H:%M') if time_string == "14:26": # Set up time device.send_data(remote_device, "1/80") sleep(1) device.send_data(remote_device, "2/50") sleep(10) if time_string == "14:27": device.send_data(remote_device, "1/30") sleep(1) device.send_data(remote_device, "2/20") sleep(10) if time_string == "14:28": device.send_data(remote_device, "1/0") sleep(1) device.send_data(remote_device, "2/0") sleep(10) except: Number = Number + 1 finally: if device is not None and device.is_open(): device.close()
def main(): print(" +--------------------------------------+") print(" | XBee Python Library Send Data Sample |") print(" +--------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) # 这部分可选,传输Timeout # Retrieving the configured timeout for synchronous operations. print("Current timeout: %d seconds" % device.get_sync_ops_timeout()) # 默认是4s # Configuring the new timeout (in seconds) for synchronous operations. device.set_sync_ops_timeout(NEW_TIMEOUT_FOR_SYNC_OPERATIONS) try: device.open() # Obtain the remote XBee device from the XBee network. xbee_network = device.get_network() remote_device = xbee_network.discover_device(REMOTE_NODE_ID) if remote_device is None: print("Could not find the remote device") exit(1) print("Sending data to %s >> %s..." % (remote_device.get_64bit_addr(), DATA_TO_SEND)) device.send_data(remote_device, DATA_TO_SEND) print("Success") finally: if device is not None and device.is_open(): device.close()
def main(): print(" +-------------------------------------------------+") print(" | XBee Python Library Send User Data Relay Sample |") print(" +-------------------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() for i in range(10): data = (DATA_TO_SEND % (i + 1)) print("Sending User Data Relay to %s >> '%s'... " % (DEST_INTERFACE.name, data), end="") device.send_user_data_relay(DEST_INTERFACE, data.encode("utf-8")) print("Success") time.sleep(1) finally: if device is not None and device.is_open(): device.close()
def main(): print(" +------------+") print(" | Reset Test |") print(" +------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() def modem_status_callback(status): if status == ModemStatus.COORDINATOR_STARTED: return assert (status in [ModemStatus.HARDWARE_RESET, ModemStatus.WATCHDOG_TIMER_RESET]) device.add_modem_status_received_callback(modem_status_callback) for i in range(10): device.reset() time.sleep(1) print("Test finished successfully") finally: if device is not None and device.is_open(): device.close()
def main(): print(" +--------------------------------------+") print(" | XBee Python Library Send Data Sample |") print(" +--------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() # Obtain the remote XBee device from the XBee network. xbee_network = device.get_network() remote_device = xbee_network.discover_device(REMOTE_NODE_ID) if remote_device is None: print("Could not find the remote device") exit(1) # send data to the remote node id while True: raw_val = device.get_adc_value(IOLine.DIO0_AD0) voltage = (raw_val/1024) *3.3 p = ((voltage - .33)/1.32) - 1 string = str(p) device.send_data(remote_device, string) #device will close if there is a error finally: if device is not None and device.is_open(): device.close()
def main(): #Define your xbee device with the defined port and baudrate device = XBeeDevice(PORT, BAUD_RATE) try: #open the xbee device device.open() #set the io pins on the xbee board itself #in this example, we are using the DIO_AD0 pin on the board #refrence the datasheet for other availible pins device.set_io_configuration(IOLine.DIO0_AD0, IOMode.ADC) # Obtain the remote XBee device from the XBee network. xbee_network = device.get_network() remote_device = xbee_network.discover_device(REMOTE_NODE_ID) if remote_device is None: print("Could not find the remote device") exit(1) # a simple while loop to read the adc sample, convert it and send to the main reciever while True: raw_val = device.get_adc_value(IOLine.DIO0_AD0) voltage = ((raw_val / 1024)) * 2.5 p = ((voltage - .3333) / 1.32000) - 1.00 string = str(p) print(string) time.sleep(0.05) device.send_data(remote_device, string) # close the device if any error occurs finally: if device is not None and device.is_open(): device.close()
def main(): print(" +--------------------------------------+") print(" | XBee Python Library Send Data Sample |") print(" +--------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() # Obtain the remote XBee device from the XBee network. xbee_network = device.get_network() remote_device = xbee_network.discover_device(REMOTE_NODE_ID) if remote_device is None: print("Could not find the remote device") exit(1) print("Sending data to %s >> %s..." % (remote_device.get_64bit_addr(), DATA_TO_SEND)) device.send_data(remote_device, DATA_TO_SEND) print("Success") finally: if device is not None and device.is_open(): device.close()
def main(): print(" +-------------------------------------------------+") print(" | Bote |") print(" +-------------------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() device.flush_queues() print("Waiting conversation...\n") #Variable to stop the conversation commActive = True while commActive : #Read data and chek if something has been received xbee_message = device.read_data() if xbee_message is not None: #Print the message and print("Received Message: " , xbee_message.data.decode()) print(xbee_message.data.decode()) #if it's different than exit continue listening if xbee_message.data.decode() == 'exit': commActive = False #If the device is not closed, close it. finally: if device is not None and device.is_open(): device.close()