def from_hex_string(cls, address): """ Class constructor. Instantiates a new :`.XBee16BitAddress` object from the provided hex string. Args: address (String): String containing the address. Must be made by hex. digits without blanks. Minimum 1 character, maximum 4 (16-bit). Raises: ValueError: if `address` has less than 1 character. ValueError: if `address` contains non-hexadecimal characters. """ if not address: raise ValueError("Address must contain at least 1 digit") if not cls.__REGEXP.match(address): raise ValueError("Address must follow this pattern: " + cls.PATTERN) return cls(utils.hex_string_to_bytes(address))
from digi.xbee.devices import XBeeDevice from digi.xbee.util import utils # TODO: Replace with the serial port where your local module is connected to. PORT = "COM1" # TODO: Replace with the baud rate of your local module. BAUD_RATE = 9600 PARAM_NODE_ID = "NI" PARAM_PAN_ID = "ID" PARAM_DEST_ADDRESS_H = "DH" PARAM_DEST_ADDRESS_L = "DL" PARAM_VALUE_NODE_ID = "Yoda" PARAM_VALUE_PAN_ID = utils.hex_string_to_bytes("1234") PARAM_VALUE_DEST_ADDRESS_H = utils.hex_string_to_bytes("00") PARAM_VALUE_DEST_ADDRESS_L = utils.hex_string_to_bytes("FFFF") def main(): print(" +-----------------------------------------------+") print(" | XBee Python Library Set/Get parameters Sample |") print(" +-----------------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() # Set parameters.
class Device: PORT = '' BAUD_RATE = '' # Grupo de parametros que podem ser setados, nota-se que não são necessariamente os mesmo que podem ser obtidos do xbee.------ PARAM_NODE_ID = "NI" PARAM_PAN_ID = "ID" PARAM_DEST_ADDRESS_H = "DH" PARAM_DEST_ADDRESS_L = "DL" # Valores exemplo PARAM_VALUE_NODE_ID = "Yoda" PARAM_VALUE_PAN_ID = utils.hex_string_to_bytes("1234") PARAM_VALUE_DEST_ADDRESS_H = utils.hex_string_to_bytes("00") PARAM_VALUE_DEST_ADDRESS_L = utils.hex_string_to_bytes("FFFF") # ------------- local_xbee = None remote_xbee = None SixFourBitAddress = "" def __init__(self): super(Device, self).__init__() # Precisa ser chamado toda vez que um xbee remoto for criado. O endereço deve ser previamente conhecido. Pode ser que haja uma maneira melhor. def set_64bitaddres(self, adress): self.SixFourBitAddress = adress def set_port(self, porta): self.PORT = porta def get_port(self): return self.PORT def set_baudrate(self, baud): self.BAUD_RATE = baud def get_baudrate(self): return self.BAUD_RATE def create_nodes(self, numero): self.local_xbee = XBeeDevice(PORT, BAUND_RATE) for x in range(0, numero - 2): self.remote_xbee[x] = RemoteXBeeDevice( local_xbee, XBee64BitAddress.from_hex_string(SixFourBitAddress)) def open_connection(self): self.local_xbee.open() # retorna uma lista com os parâmetros def get_Device_Information(self, xbee): addr_64 = xbee.get_64bit_addr() node_id = xbee.get_node_id() hardware_version = xbee.get_hardware_version() firmware_version = xbee.get_firmware_version() protocol = xbee.get_protocol() operating_mode = xbee.get_operating_mode() dest_addr = xbee.get_dest_address() pan_id = xbee.get_pan_id() power_lvl = xbee.get_power_level() lista_result = [ addr_64, node_id, hardware_version, firmware_version, protocol, operating_mode, dest_addr, pan_id, power_lvl ] return lista_result # Recebe um dicionário com parâmetros def set_Device_Information(self, xbee, dicionario_paramentros): for key in dicionario_parametros: if key == 'PARAM_VALUE_NODE_ID': self.PARAM_VALUE_NODE_ID = dicionario_parametros.get( 'PARAM_VALUE_NODE_ID') if key == 'PARAM_VALUE_PAN_ID': self.PARAM_VALUE_PAN_ID = dicionario_parametros.get( 'PARAM_VALUE_PAN_ID') if key == 'PARAM_VALUE_DEST_ADDRESS_H': self.PARAM_VALUE_DEST_ADDRESS_H = dicionario_parametros.get( 'PARAM_VALUE_DEST_ADDRESS_H') if key == 'PARAM_VALUE_DEST_ADDRESS_L': self.PARAM_VALUE_DEST_ADDRESS_L = dicionario_parametros.get( 'PARAM_VALUE_DEST_ADDRESS_L') xbee.set_parameter(self.PARAM_NODE_ID, bytearray(self.PARAM_VALUE_NODE_ID, 'utf8')) xbee.set_parameter(self.PARAM_PAN_ID, self.PARAM_VALUE_PAN_ID) xbee.set_parameter(self.PARAM_DEST_ADDRESS_H, self.PARAM_VALUE_DEST_ADDRESS_H) xbee.set_parameter(self.PARAM_DEST_ADDRESS_L, self.PARAM_VALUE_DEST_ADDRESS_L) def close_connection(self): self.local_xbee.close() def reset_device(self, xbee): self.xbee.reset() def discover_network(self): xnet = self.local_xbee.get_network() xnet.set_discovery_options({ DiscoveryOptions.APPEND_DD, DiscoveryOptions.DISCOVERY_MYSELF, DiscoveryOptions.APEND_RSSI }) xnet.set_discovery_timeout(15) xnet.start_discovery_process() while xnet.is_discovery_running(): time.sleep(0.5) devices = xnet.get_devices() iterator = iter(devices) lista_nos while iterator.next() is not None: lista_nos.append(get_Device_Information(iterator.next())) return lista_nos def send_synchronous_data(self, remote_xbee, string): self.local_xbee.send_data(remote_xbee, string) # timeout está em segundos. def set_timeout_synchronous_data(self, timeout): self.local_xbee.set_sync_ops_timeout(timeout) def get_timeout_synchronous_data(self): return self.local_xbee.get_sync_ops_timeout() def send_broadcast_data(self, string): self.local_xbee.send_data_broadcast(string) # Definição do callback para recepção de mensagem. def my_data_received_callback(self, xbee_message, remote_xbee): address = xbee_message.remote_xbee.get_64bit_addr() data = xbee_message.data.decode("utf8") print("Received data from %s: %s" % (address, data)) # recebe mensagens por <timer> segundos def receive_data(self, timer): for y in range(0, timer - 1): self.local_xbee.add_data_received_callback( my_data_received_callback) self.local_xbee.del_data_received_callback(my_data_recived_callback) # Precisa de testes para ver como é a saída dos dados e como isso será passado para interface. # Há uma maneira alternativa de se fazer, ver documentação. def log(self): # enable log dev_logger = enable_logger(digi.xbee.devices.__name__, logging.INFO)
def main(NodeAddress): print(" +-----------------------------------------------+") print(" | Write Local XBee parameters |") print(" +-----------------------------------------------+\n") local_device = XBeeDevice(PORT, BAUD_RATE) try: local_device.open() # Get Hardware Models with extended DIO (P5 to P9) Hardware_Extended = read_sys_config.ReadHardwareVersionWhithP5ToP9PinsFromFile( ) HV = utils.hex_to_string(local_device.get_parameter("HV")) # Set filne name source of the params read_node_config_file.set_path_to_file(NodeAddress) # Networking local_device.set_parameter( "ID", utils.hex_string_to_bytes( read_node_config_file.ReadPanIDFromFile(NodeAddress))) local_device.set_parameter( "SC", utils.hex_string_to_bytes( read_node_config_file.ReadScanChannelsFromFile(NodeAddress))) local_device.set_parameter( "SD", utils.hex_string_to_bytes( read_node_config_file.ReadScanDurationFromFile(NodeAddress))) local_device.set_parameter( "ZS", utils.hex_string_to_bytes( read_node_config_file.ReadZigBeeStackProfileFromFile( NodeAddress))) local_device.set_parameter( "NJ", utils.hex_string_to_bytes( read_node_config_file.ReadNodeJoinTimeFromFile(NodeAddress))) local_device.set_parameter( "NW", utils.hex_string_to_bytes( read_node_config_file.ReadNetworkWatchdogTimeoutFromFile( NodeAddress))) local_device.set_parameter( "JV", utils.hex_string_to_bytes( read_node_config_file.ReadChannelVerificationFromFile( NodeAddress))) local_device.set_parameter( "JN", utils.hex_string_to_bytes( read_node_config_file.ReadJoinNotificationFromFile( NodeAddress))) local_device.set_parameter( "CE", utils.hex_string_to_bytes( read_node_config_file.ReadCoordinatorEnableFromFile( NodeAddress))) local_device.set_parameter( "DO", utils.hex_string_to_bytes( read_node_config_file.ReadDeviceOptionsFromFile(NodeAddress))) local_device.set_parameter( "DC", utils.hex_string_to_bytes( read_node_config_file.ReadDeviceControlsFromFile(NodeAddress))) # Addressing local_device.set_parameter( "DH", utils.hex_string_to_bytes( read_node_config_file.ReadDestinationAddressHighFromFile( NodeAddress))) local_device.set_parameter( "DL", utils.hex_string_to_bytes( read_node_config_file.ReadDestinationAddressLowFromFile( NodeAddress))) local_device.set_parameter( "NI", bytearray( read_node_config_file.ReadNodeIdentifierFromFile(NodeAddress), 'utf8')) local_device.set_parameter( "NH", utils.hex_string_to_bytes( read_node_config_file.ReadMaximumHopsFromFile(NodeAddress))) local_device.set_parameter( "BH", utils.hex_string_to_bytes( read_node_config_file.ReadBroadcastRadiusFromFile( NodeAddress))) local_device.set_parameter( "AR", utils.hex_string_to_bytes( read_node_config_file.ReadManyToOneRouteBroadcastTimeFromFile( NodeAddress))) local_device.set_parameter( "DD", utils.hex_string_to_bytes( read_node_config_file.ReadDeviceTypeIdentifierFromFile( NodeAddress))) local_device.set_parameter( "NT", utils.hex_string_to_bytes( read_node_config_file.ReadNodeDiscoveryBackoffFromFile( NodeAddress))) local_device.set_parameter( "NO", utils.hex_string_to_bytes( read_node_config_file.ReadNodeDiscoveryOptionsFromFile( NodeAddress))) local_device.set_parameter( "CR", utils.hex_string_to_bytes( read_node_config_file.ReadPanConflictThresholdFromFile( NodeAddress))) # ZigBee Addressing local_device.set_parameter( "SE", utils.hex_string_to_bytes( read_node_config_file.ReadZigBeeSourceEndPointFromFile( NodeAddress))) local_device.set_parameter( "DE", utils.hex_string_to_bytes( read_node_config_file.ReadZigBeeDestinationEndpointFromFile( NodeAddress))) local_device.set_parameter( "CI", utils.hex_string_to_bytes( read_node_config_file.ReadZigBeeClusterIDFromFile( NodeAddress))) local_device.set_parameter( "TO", utils.hex_string_to_bytes( read_node_config_file.ReadTransmitOptionsFromFile( NodeAddress))) # RF Interfacing local_device.set_parameter( "PL", utils.hex_string_to_bytes( read_node_config_file.ReadTxPowerLevelFromFile(NodeAddress))) local_device.set_parameter( "PM", utils.hex_string_to_bytes( read_node_config_file.ReadPowerModeFromFile(NodeAddress))) # Security local_device.set_parameter( "EE", utils.hex_string_to_bytes( read_node_config_file.ReadEncryptionEnableFromFile( NodeAddress))) local_device.set_parameter( "EO", utils.hex_string_to_bytes( read_node_config_file.ReadEncryptionOptionsFromFile( NodeAddress))) local_device.set_parameter( "KY", utils.hex_string_to_bytes( read_node_config_file.ReadEncryptionKeyFromFile(NodeAddress))) local_device.set_parameter( "NK", utils.hex_string_to_bytes( read_node_config_file.ReadNetworkEncryptionKeyFromFile( NodeAddress))) # Serial Interfacing local_device.set_parameter( "BD", utils.hex_string_to_bytes( read_node_config_file.ReadBaudRateFromFile(NodeAddress))) local_device.set_parameter( "NB", utils.hex_string_to_bytes( read_node_config_file.ReadParityFromFile(NodeAddress))) local_device.set_parameter( "SB", utils.hex_string_to_bytes( read_node_config_file.ReadStopBitsFromFile(NodeAddress))) local_device.set_parameter( "RO", utils.hex_string_to_bytes( read_node_config_file.ReadPacketizationTimeoutFromFile( NodeAddress))) local_device.set_parameter( "D6", utils.hex_string_to_bytes( read_node_config_file.ReadDIO6ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "D7", utils.hex_string_to_bytes( read_node_config_file.ReadDIO7ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "AP", utils.hex_string_to_bytes( read_node_config_file.ReadApiEnableFromFile(NodeAddress))) local_device.set_parameter( "AO", utils.hex_string_to_bytes( read_node_config_file.ReadApiOutputModeFromFile(NodeAddress))) # AT Command Options local_device.set_parameter( "CT", utils.hex_string_to_bytes( read_node_config_file.ReadATCommandModeTimeoutFromFile( NodeAddress))) local_device.set_parameter( "GT", utils.hex_string_to_bytes( read_node_config_file.ReadGuardTimesFromFile(NodeAddress))) local_device.set_parameter( "CC", utils.hex_string_to_bytes( read_node_config_file.ReadCommandSequenceCharacterFromFile( NodeAddress))) # Sleep Modes local_device.set_parameter( "SP", utils.hex_string_to_bytes( read_node_config_file.ReadCyclicSleepPeriodFromFile( NodeAddress))) local_device.set_parameter( "SN", utils.hex_string_to_bytes( read_node_config_file.ReadNumberOfCyclicSleepPeriodsFromFile( NodeAddress))) local_device.set_parameter( "SM", utils.hex_string_to_bytes( read_node_config_file.ReadSleepModeFromFile(NodeAddress))) local_device.set_parameter( "ST", utils.hex_string_to_bytes( read_node_config_file.ReadTimeBeforeSleepFromFile( NodeAddress))) local_device.set_parameter( "SO", utils.hex_string_to_bytes( read_node_config_file.ReadSleepOptionsFromFile(NodeAddress))) local_device.set_parameter( "WH", utils.hex_string_to_bytes( read_node_config_file.ReadWakeHostFromFile(NodeAddress))) local_device.set_parameter( "PO", utils.hex_string_to_bytes( read_node_config_file.ReadPollRateFromFile(NodeAddress))) # I/O Settings local_device.set_parameter( "D0", utils.hex_string_to_bytes( read_node_config_file.ReadDIO0AD0ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "D1", utils.hex_string_to_bytes( read_node_config_file.ReadDIO1AD1ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "D2", utils.hex_string_to_bytes( read_node_config_file.ReadDIO2AD2ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "D3", utils.hex_string_to_bytes( read_node_config_file.ReadDIO3AD3ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "D4", utils.hex_string_to_bytes( read_node_config_file.ReadDIO4ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "D5", utils.hex_string_to_bytes( read_node_config_file.ReadDIO5ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "D8", utils.hex_string_to_bytes( read_node_config_file.ReadDIO8ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "D9", utils.hex_string_to_bytes( read_node_config_file.ReadDIO9ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "P0", utils.hex_string_to_bytes( read_node_config_file.ReadDIO10ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "P1", utils.hex_string_to_bytes( read_node_config_file.ReadDIO11ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "P2", utils.hex_string_to_bytes( read_node_config_file.ReadDIO12ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "P3", utils.hex_string_to_bytes( read_node_config_file.ReadDIO13ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "P4", utils.hex_string_to_bytes( read_node_config_file.ReadDIO14ConfigurationFromFile( NodeAddress))) if HV == Hardware_Extended: # Not all hardware have this inputs local_device.set_parameter( "P5", utils.hex_string_to_bytes( read_node_config_file.ReadDIO15ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "P6", utils.hex_string_to_bytes( read_node_config_file.ReadDIO16ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "P7", utils.hex_string_to_bytes( read_node_config_file.ReadDIO17ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "P8", utils.hex_string_to_bytes( read_node_config_file.ReadDIO18ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "P9", utils.hex_string_to_bytes( read_node_config_file.ReadDIO19ConfigurationFromFile( NodeAddress))) local_device.set_parameter( "PR", utils.hex_string_to_bytes( read_node_config_file.ReadPullUpResistorEnableFromFile( NodeAddress))) local_device.set_parameter( "PD", utils.hex_string_to_bytes( read_node_config_file.ReadPullUpDownDirectionFromFile( NodeAddress))) local_device.set_parameter( "LT", utils.hex_string_to_bytes( read_node_config_file.ReadAssociatedLedBlinkTimeFromFile( NodeAddress))) local_device.set_parameter( "RP", utils.hex_string_to_bytes( read_node_config_file.ReadRssiPwmTimerFromFile(NodeAddress))) # I/O Sampling local_device.set_parameter( "IR", utils.hex_string_to_bytes( read_node_config_file.ReadIOSamplingRateFromFile(NodeAddress))) local_device.set_parameter( "IC", utils.hex_string_to_bytes( read_node_config_file.ReadDigitalIOChangeDetectionFromFile( NodeAddress))) local_device.set_parameter( "V+", utils.hex_string_to_bytes( read_node_config_file.ReadSupplyVoltageHihgThresholdFromFile( NodeAddress))) # Make params permanent local_device.write_changes() # make changes permanet #print parameters print(" Success!! All Parameters Were Written ") log = " Success!! All Parameters Were Written \n\n" except: log = " Sorry, an error has happened during writting operation\n" if local_device.is_open(): local_device.close() pass finally: if local_device is not None and local_device.is_open(): local_device.close() else: log = " No local device found\n" return log
from tkinter import messagebox from digi.xbee.devices import XBeeDevice from serial.tools import list_ports from digi.xbee.util import utils import time import sys import functools import math # Gateway parameters PARAM_NODE_ID = "NI" PARAM_PAN_ID = "ID" PARAM_VALUE_NODE_ID = "GATEWAY" # Global Variables PARAM_VALUE_PAN_ID = utils.hex_string_to_bytes("10") prevSelectedIndex = -1 devices = [] counter = -1 grid_width = 0 grid_height = 0 node_list = None node_frame = None # *** Application Class *** # The class of the main app class Application(Frame): def __init__(self, master=None): super().__init__(master) self.pack()
def apply_change_sm(self, sm): self.local_device.set_parameter('SM', hex_string_to_bytes(str(sm))) self.local_device.apply_changes() self.local_device.write_changes() self.new_sm = self.local_device.get_parameter('SM')
def apply_change_jv(self, jv): self.local_device.set_parameter('JV', hex_string_to_bytes(str(jv))) self.local_device.apply_changes() self.local_device.write_changes() self.new_jv = self.local_device.get_parameter('JV')
def apply_change_ce(self, ce): self.local_device.set_parameter('CE', hex_string_to_bytes(str(ce))) self.local_device.apply_changes() self.local_device.write_changes() self.new_ce = self.local_device.get_parameter('CE')
def apply_change_id(self, id): self.local_device.set_pan_id(hex_string_to_bytes(str(id))) self.local_device.apply_changes() self.local_device.write_changes() self.new_id = self.local_device.get_parameter('ID')
from digi.xbee.util import utils from digi.xbee.models.address import XBee64BitAddress # TODO: Replace with the serial port where your local module is connected to. PORT = "COM6" # TODO: Replace with the baud rate of your local module. BAUD_RATE = 9600 PARAM_NODE_ID = "NI" PARAM_PAN_ID = "ID" PARAM_DEST_ADDRESS_H = "DH" PARAM_DEST_ADDRESS_L = "DL" PARAM_SLEEP_PER = "SP" PARAM_VALUE_NODE_ID = "Yoda" PARAM_VALUE_PAN_ID = utils.hex_string_to_bytes("2015") PARAM_VALUE_DEST_ADDRESS_H = utils.hex_string_to_bytes("00") PARAM_VALUE_DEST_ADDRESS_L = utils.hex_string_to_bytes("FFFF") PARAM_VALUE_REMOTE_NODE_ADDR = XBee64BitAddress.from_hex_string( "0013A200415BFC59") PARAM_VALUE_REMOTE_NODE_ID = "Luke" PARAM_VALUE_REMOTE_NODE_SP = utils.hex_string_to_bytes("1FF") def main(): print(" +-----------------------------------------------+") print(" | XBee Python Library Set/Get parameters Sample |") print(" +-----------------------------------------------+\n") local_device = XBeeDevice(PORT, BAUD_RATE)