2 from xbee import ZigBee
10 from threading import Thread, Lock
13 # Communication Parameters
14 ZIGBEE_SERIAL_PORT = "/dev/cu.usbserial-DN01DJIP"
15 ZIGBEE_SERIAL_BAUD = 115200
16 UDP_RECEIVE_PORT = 5005
19 ZIGBEE_DEVICE_ADDRESS = "0000000000000000"
20 didzigbeeAddressLSBs = False
21 didzigbeeAddressMSBs = False
24 def parseCommandLineArgs(argv):
25 global ZIGBEE_SERIAL_PORT
26 global ZIGBEE_SERIAL_BAUD
28 opts, args = getopt.getopt(
29 argv, "hp:b:u:", ["port=", "baud=", "udpport="])
31 except getopt.GetoptError:
32 print 'test.py -p <serial_port> -b <baud_rate> -u <udp_port>'
37 print 'test.py -i <inputfile> -o <outputfile>'
40 elif opt in ("-p", "--port"):
41 ZIGBEE_SERIAL_PORT = arg
43 elif opt in ("-b", "--baud"):
45 ZIGBEE_SERIAL_BAUD = int(arg)
47 print "Buad rate must be an integer"
50 # elif opt in ("-u", "--udpport"):
54 # print "Udp port must be an integer"
58 def printMessageData(data):
62 print "{0:02x}".format(ord(e)),
64 print "({})".format(data[d]),
68 def convertMessageToString(data):
72 retString += d + ' : '
75 retString += "{0:02x}".format(ord(e))
78 retString += "({})".format(data[d])
86 """A generator to divide a sequence into chunks of n units."""
92 def hexListToChar(hexList):
95 retString += chr(int(h, 16))
101 def zigbeeMessageCallbackHandler(data):
102 global ZIGBEE_DEVICE_ADDRESS
103 global didzigbeeAddressLSBs
104 global didzigbeeAddressMSBs
106 print "================================================================================"
107 printMessageData(data)
108 print "================================================================================"
112 if __name__ == "__main__":
114 # zigbeeClientCallbackDict["000d6f0003ebf2ee"] = [("127.0.0.1", 5556)]
116 # parse the command line arguments
117 parseCommandLineArgs(sys.argv[1:])
119 # create serial object used for communication to the zigbee radio
120 serialConnection = serial.Serial(ZIGBEE_SERIAL_PORT, ZIGBEE_SERIAL_BAUD)
122 # create a zigbee object that handles all zigbee communication
123 # we use this to do all communication to and from the radio
124 # when data comes from the radio it will get a bit of unpacking
125 # and then a call to the callback specified will be done with the
127 zigbeeConnection = ZigBee(
128 serialConnection, callback=zigbeeMessageCallbackHandler)
132 print "Starting main loop..."
134 # zigbeeConnection.send('tx_explicit',
136 # dest_addr_long='\x00\x0d\x6f\x00\x03\xeb\xf2\xee',
137 # dest_addr='\xff\xfd',
138 # src_endpoint='\x00',
139 # dest_endpoint='\x00',
140 # cluster='\x00\x00',
141 # profile='\x00\x00',
142 # data='\xb4' + '\xee\xf2\xeb\x03\x00\x6f\x0d\x00' + '\x00'
145 # zigbeeConnection.send('tx_explicit',
147 # dest_addr_long='\x00\x0d\x6f\x00\x03\xeb\xf2\xee',
148 # dest_addr='\xff\xfd',
149 # src_endpoint='\x00',
150 # dest_endpoint='\x00',
151 # cluster='\x00\x21',
152 # profile='\x00\x00',
153 # data='\xb4' + '\xee\xf2\xeb\x03\x00\x6f\x0d\x00' + '\x01' + '\x05\x04' +
154 # '\x03' + '\xda\x9a\xd9\x40\x00\xa2\x13\x00' + '\x00'
157 # zigbeeConnection.send('tx_explicit',
159 # dest_addr_long='\x00\x0d\x6f\x00\x03\xeb\xf2\xee',
160 # dest_addr='\x65\x04',
161 # src_endpoint='\x00',
162 # dest_endpoint='\x01',
163 # cluster='\x04\x05',
164 # profile='\x01\x04',
165 # data='\x00' + '\xa1' + '\x06' + '\x00' + '\x00\x00' +
166 # '\x21' + '\x01\x00' + '\x3c\x00' + '\x00\x00'
174 # zigbeeConnection.send('tx_explicit',
176 # dest_addr_long='\x00\x0d\x6f\x00\x03\xeb\xf2\xee',
177 # dest_addr='\x9a\xb7',
178 # src_endpoint='\x00',
179 # dest_endpoint='\x00',
180 # cluster='\x00\x00',
181 # profile='\x00\x00',
182 # data='\xb4' + '\xee\xf2\xeb\x03\x00\x6f\x0d\x00' + '\x00'
186 zigbeeConnection.send('tx_explicit',
188 dest_addr_long='\x00\x0d\x6f\x00\x03\xeb\xf2\xee',
189 dest_addr='\x9a\xb7',
191 dest_endpoint='\x00',
194 data='\xb4' + '\xee\xf2\xeb\x03\x00\x6f\x0d\x00' + '\x01' + '\x05\x04' +
195 '\x03' + '\xda\x9a\xd9\x40\x00\xa2\x13\x00' + '\x00'
200 except KeyboardInterrupt:
203 traceback.print_exc()
206 zigbeeConnection.halt()
207 serialConnection.close()