def __init__(self, ser=None, xbee=None): self.xbee = None if xbee: self.xbee = xbee elif ser: self.xbee = XBee(ser) self.handlers = [] self.names = set()
def __init__(self, serial_port, remote_addr): # Open serial port, construct XBee1, configure remote device, # store as hardware self.remote_addr = remote_addr ser = serial.Serial(serial_port) xbee = XBee(ser) super(XBeeAlarm, self).__init__(xbee) # Reset remote device self._reset()
from xbee.thread import XBee import time import serial PORT = '/dev/ttyUSB0' BAUD_RATE = 9600 # Open serial port ser = serial.Serial(PORT, BAUD_RATE) def message_received(data): print(data) #print(type(data)) print(data['source_addr']) print(type('source_addr')) # Create API object, which spawns a new thread xbee = XBee(ser, callback=message_received) # Do other stuff in the main thread while True: try: time.sleep(.1) except KeyboardInterrupt: break # halt() must be called before closing the serial # port in order to ensure proper thread shutdown xbee.halt() ser.close()
DevList = {} if not args.address: args.address = 'FFFF' regex = re.compile("status\': \'..(00)\'") ser = serial.Serial('/dev/' + args.port, args.baud.split(',')[0]) XBee = DigiMesh(ser) print "\nXBEE COM Configuration Utility v1.0\nRunning configuration steps...\n" ## Set local XBee PAN ID to 2513 ## print "Set Local Network ID (ATID=2513)" XBee.send('at', frame_id='1', command='ID', parameter=binascii.unhexlify('2513')) response = XBee.wait_read_frame() #print response if response['status'].encode("hex") == '00': print "OK" else: print "Fail" print response exit(1) time.sleep(0.5) ## Set Local XBee Node ID to XBEEPI ## print "Set Node Name to 'XBEEPI' (ATNI=XBEEPI)" XBee.send('at', frame_id='2', command='NI', parameter='XBEEPI')
dispatch = Dispatch(ser) # Register the packet handlers with the dispatch: # The string name allows one to distinguish between mutiple registrations # for a single callback function # The second argument is the function to call # The third argument is a function which determines whether to call its # associated callback when a packet arrives. It should return a boolean. dispatch.register("status", status_handler, lambda packet: packet['id'] == 'status') dispatch.register("io_data", io_sample_handler, lambda packet: packet['id'] == 'rx_io_data') # Create API object, which spawns a new thread # Point the asyncronous callback at Dispatch.dispatch() # This method will dispatch a single XBee data packet when called xbee = XBee(ser, callback=dispatch.dispatch) # Do other stuff in the main thread while True: try: time.sleep(.1) except KeyboardInterrupt: break # halt() must be called before closing the serial # port in order to ensure proper thread shutdown xbee.halt() ser.close()
""" led_adc_example.py By Paul Malmsten, 2010 [email protected] A simple example which sets up a remote device to read an analog value on ADC0 and a digital output on DIO1. It will then read voltage measurements and write an active-low result to the remote DIO1 pin. """ from xbee.thread import XBee import serial ser = serial.Serial('/dev/ttyUSB0', 9600) xbee = XBee(ser) ## Set up remote device #xbee.send('remote_at', #frame_id='A', #dest_addr_long='\x00\x00\x00\x00\x00\x00\x00\x00', #dest_addr='\x56\x78', #options='\x02', #command='D0', #parameter='\x02') #print xbee.wait_read_frame()['status'] #xbee.send('remote_at', #frame_id='B', #dest_addr_long='\x00\x00\x00\x00\x00\x00\x00\x00',
# The third argument is a function which determines whether to call its # associated callback when a packet arrives. It should return a boolean. dispatch.register( "status", status_handler, lambda packet: packet['id']=='status' ) dispatch.register( "io_data", io_sample_handler, lambda packet: packet['id']=='rx_io_data' ) # Create API object, which spawns a new thread # Point the asyncronous callback at Dispatch.dispatch() # This method will dispatch a single XBee data packet when called xbee = XBee(ser, callback=dispatch.dispatch) # Do other stuff in the main thread while True: try: time.sleep(.1) except KeyboardInterrupt: break # halt() must be called before closing the serial # port in order to ensure proper thread shutdown xbee.halt() ser.close()
def main(): """ Sends an API AT command to read the lower-order address bits from an XBee Series 1 and looks for a response """ try: # Open serial port ser = serial.Serial('/dev/tty.usbserial-DN06AABE', 57600) # Create XBee Series 1 object xbee = XBee(ser) # Send AT packet xbee.send('at', frame_id='A', command='DH') print('Waiting for the response') # Wait for response response = xbee.wait_read_frame() print(response) # Send AT packet xbee.send('at', frame_id='B', command='DL') # Wait for response response = xbee.wait_read_frame() print(response) # Send AT packet xbee.send('at', frame_id='C', command='MY') # Wait for response response = xbee.wait_read_frame() print(response) # Send AT packet xbee.send('at', frame_id='D', command='CE') # Wait for response response = xbee.wait_read_frame() print(response) except KeyboardInterrupt: pass finally: ser.close()
receive_samples.py By Paul Malmsten, 2010 [email protected] This example continuously reads the serial port and processes IO data received from a remote XBee. """ from xbee.thread import XBee import serial PORT = '/dev/ttyUSB0' BAUD_RATE = 9600 # Open serial port ser = serial.Serial(PORT, BAUD_RATE) # Create API object xbee = XBee(ser) # Continuously read and print packets while True: try: response = xbee.wait_read_frame() print response except KeyboardInterrupt: break ser.close()
class Dispatch(object): def __init__(self, ser=None, xbee=None): self.xbee = None if xbee: self.xbee = xbee elif ser: self.xbee = XBee(ser) self.handlers = [] self.names = set() def register(self, name, callback, filter): """ register: string, function: string, data -> None, function: data -> boolean -> None Register will save the given name, callback, and filter function for use when a packet arrives. When one arrives, the filter function will be called to determine whether to call its associated callback function. If the filter method returns true, the callback method will be called with its associated name string and the packet which triggered the call. """ if name in self.names: raise ValueError("A callback has already been registered with \ the name '%s'" % name) self.handlers.append({ 'name': name, 'callback': callback, 'filter': filter }) self.names.add(name) def run(self, oneshot=False): """ run: boolean -> None run will read and dispatch any packet which arrives from the XBee device """ if not self.xbee: raise ValueError("Either a serial port or an XBee must be provided \ to __init__ to execute run()") while True: self.dispatch(self.xbee.wait_read_frame()) if oneshot: break def dispatch(self, packet): """ dispatch: XBee data dict -> None When called, dispatch checks the given packet against each registered callback method and calls each callback whose filter function returns true. """ for handler in self.handlers: if handler['filter'](packet): # Call the handler method with its associated # name and the packet which passed its filter check handler['callback'](handler['name'], packet)