updating, sending values with write command no response

moving formatting around to reduce memory footprint
handling eonotcon error by restarting network
master
Nathan 3 years ago
parent e70de5f231
commit d2639e1b8b

@ -4,17 +4,23 @@ from machine import Pin
class Barrier: class Barrier:
moving = 0 moving = b'\x00'
barrier_position = 0 barrier_position = b'\x00'
ad0 = Pin("D0", Pin.IN, Pin.PULL_UP) #door ad0 = Pin("D0", Pin.IN, Pin.PULL_UP) #door
ad1 = Pin("D1", Pin.IN, Pin.PULL_UP) #motor ad1 = Pin("D1", Pin.IN, Pin.PULL_UP) #motor
ad4 = Pin("D4", Pin.OUT, value=0) ad4 = Pin("D4", Pin.OUT, value=0)
door = ad0.value() door = ad0.value().to_bytes(2,"big")
motor = ad1.value() motor = ad1.value().to_bytes(2, "big")
update = False update = True
def status(self, seq, payload): def status(self, seq, payload):
pass #moving state is x0001 enum8(0x30)
#Barrierposition is 0x000A uint8(0x20)
# 2 octets attribute identifier
# 1 octet attribute data type
# 1 octet attribute value
return bytes([1,0,32,self.moving,10,0,20,self.barrier_position])
def command(self, seq, payload): def command(self, seq, payload):
pass pass
@ -22,26 +28,28 @@ class Barrier:
def watch(self): def watch(self):
current_door = self.door current_door = self.door
current_motor = self.motor current_motor = self.motor
self.door = self.ad0.value() self.door = self.ad0.value().to_bytes(2, "big")
self.motor = self.ad1.value() self.motor = self.ad1.value().to_bytes(2, "big")
#print("door: "+str(self.door))
#print("motor:" +str(self.motor))
if (current_door != self.door) or (current_motor != self.motor): if (current_door != self.door) or (current_motor != self.motor):
self.update = True self.update = True
else: else:
self.update = False self.update = False
if self.door == 0: if self.door == b'\x00':
if self.motor == 1: if self.motor == b'\x01':
self.moving = 0 self.moving = b'\x00'
self.barrier_position = 100 self.barrier_position = b'\x64'
if self.motor == 0: if self.motor == b'\x00':
if self.door == 1: if self.door == b'\x01':
self.moving = 0 self.moving = b'\x00'
self.barrier_position = 0 self.barrier_position = b'\x00'
if self.motor == 0: if self.motor == b'\x00':
if self.door == 0: if self.door == b'\x00':
if self.barrier_position == 100: if self.barrier_position == b'\x64':
self.barrier_position = 50 self.barrier_position = b'\x32'
self.moving = 1 self.moving = b'\x01'
if self.barrier_position == 0: if self.barrier_position == b'\x00':
self.barrier_position = 50 self.barrier_position = b'\x32'
self.moving = 2 self.moving = b'\x02'
return self.update return self.update

@ -3,6 +3,13 @@ import xbee
def receive(): def receive():
return xbee.receive() return xbee.receive()
def announce():
srcaddr = xbee.atcmd('MY')
xbee.atcmd('CN')
srcarry = srcaddr.to_bytes(2, "big")
initial_payload=bytes([171, srcarry[1], srcarry[0], 141, 194, 209, 65, 0, 162, 19, 0, 142])
xbee.transmit(xbee.ADDR_COORDINATOR,initial_payload, source_ep=0, dest_ep=0, cluster=19,profile=0, tx_options=0)
def fancy_transmit(payload, source_ep, dest_ep, cluster,profile): def fancy_transmit(payload, source_ep, dest_ep, cluster,profile):
send = 0 send = 0
while send == 0: while send == 0:
@ -10,8 +17,17 @@ def fancy_transmit(payload, source_ep, dest_ep, cluster,profile):
if xbee.transmit(xbee.ADDR_COORDINATOR, payload, source_ep=source_ep, dest_ep=dest_ep, cluster=cluster, profile=profile, if xbee.transmit(xbee.ADDR_COORDINATOR, payload, source_ep=source_ep, dest_ep=dest_ep, cluster=cluster, profile=profile,
tx_options=0) is None: tx_options=0) is None:
send = 1 send = 1
#time.sleep(1)
except OSError as e: except OSError as e:
time.sleep(1) time.sleep(1)
print(payload) # print("connection status: {stat}".format(stat=str(xbee.atcmd('AI'))))
print(e) if xbee.atcmd('AI') == 'b\x23':
print("we burnin")
xbee.atcmd('NR')
xbee.atcmd('CN')
announce()
i = 0
while i<4:
receive()
i+=1
#print(payload)
#print(e)

@ -22,20 +22,17 @@ def status_cb(status):
xbee.modem_status.callback(status_cb) xbee.modem_status.callback(status_cb)
# arduino_addr = 0x48 # arduino_addr = 0x48
# senddata = 0 # senddata = 0
xbee.atcmd('KY',b'\x5A\x69\x67\x42\x65\x65\x41\x6C\x6C\x69\x61\x6E\x63\x65\x30\x39') key = b'\x5A\x69\x67\x42\x65\x65\x41\x6C\x6C\x69\x61\x6E\x63\x65\x30\x39'
srcaddr = int(xbee.atcmd('MY')) xbee.atcmd('KY', key)
xbee.atcmd('CN')
srcarry = srcaddr.to_bytes(2, "big")
initial_payload=bytes([171, srcarry[1], srcarry[0], 141, 194, 209, 65, 0, 162, 19, 0, 142]) #leave_load=bytes([171, srcarry[1], srcarry[0], 209, 65, 0, 162, 19, 0, 2])
leave_load=bytes([171, srcarry[1], srcarry[0], 209, 65, 0, 162, 19, 0, 2])
#xbee.transmit(xbee.ADDR_COORDINATOR, b'\xAB\xF7\x6A\x8D\xC2\xD1\x41\x00\xA2\x13\x00\x8E', source_ep=0, dest_ep=0, cluster=19, profile=0, tx_options=0) #xbee.transmit(xbee.ADDR_COORDINATOR, b'\xAB\xF7\x6A\x8D\xC2\xD1\x41\x00\xA2\x13\x00\x8E', source_ep=0, dest_ep=0, cluster=19, profile=0, tx_options=0)
#xbee.transmit(xbee.ADDR_COORDINATOR, b'\xAB\xF7\x6A\x8D\xC2\xD1\x41\x00\xA2\x13\x00\x8E', source_ep=0, dest_ep=0, cluster=19, profile=0, tx_options=0) #xbee.transmit(xbee.ADDR_COORDINATOR, b'\xAB\xF7\x6A\x8D\xC2\xD1\x41\x00\xA2\x13\x00\x8E', source_ep=0, dest_ep=0, cluster=19, profile=0, tx_options=0)
send=0 #send=0
com.announce()
time.sleep(1) time.sleep(1)
com.fancy_transmit( payload=initial_payload, source_ep=0, dest_ep=0, cluster=19,profile=0 )
print("receiving") print("receiving")
diff = 3600000 diff = 3600000
first_report = False first_report = False
@ -53,15 +50,14 @@ while 1 != 0:
barrier.status(seq,payload) barrier.status(seq,payload)
if CommandType is not None: if CommandType is not None:
barrier.command(payload) barrier.command(payload)
barrier.status(seq, payload)
pass pass
if packet['cluster'] == 6: #genOnOffCluster in HA Profile if packet['cluster'] == 6: #genOnOffCluster in HA Profile
if packet['profile'] == 260: #HA profile if packet['profile'] == 260: #HA profile
cluster_name, seq, CommandType, command_name, disable_default_response, kwargs = spec.decode_zcl(packet['cluster'], packet['payload']) cluster_name, seq, CommandType, command_name, disable_default_response, kwargs = spec.decode_zcl(packet['cluster'], packet['payload'])
print(packet['payload']) # print(packet['payload'])
print(CommandType) # print(CommandType)
print(command_name) # print(command_name)
print(kwargs) # print(kwargs)
if "attributes" in kwargs: if "attributes" in kwargs:
if kwargs['attributes'][0] == 0: if kwargs['attributes'][0] == 0:
payload = bytes([12, 30, 16, seq, 1]) #zcl_header payload = bytes([12, 30, 16, seq, 1]) #zcl_header
@ -74,7 +70,7 @@ while 1 != 0:
payload = bytes([12, 30, 16, seq, 1]) payload = bytes([12, 30, 16, seq, 1])
payload = payload + bytes([0, 0, 16, ad4.value()]) payload = payload + bytes([0, 0, 16, ad4.value()])
# payload= attr_bytes # payload= attr_bytes
print(payload) #print(payload)
com.fancy_transmit(payload=payload, source_ep=packet['dest_ep'], dest_ep=packet['source_ep'], com.fancy_transmit(payload=payload, source_ep=packet['dest_ep'], dest_ep=packet['source_ep'],
cluster=packet['cluster'], profile=packet['profile']) cluster=packet['cluster'], profile=packet['profile'])
if command_name == "on": if command_name == "on":
@ -134,15 +130,20 @@ while 1 != 0:
print("Node descriptor response integer payload discard") print("Node descriptor response integer payload discard")
#for key, value in packet.items(): #for key, value in packet.items():
#1 print (key, ' : ', value) #1 print (key, ' : ', value)
if (diff < time.ticks_diff(time.ticks_ms(), timestamp)) or ( not first_report ) : if (diff < time.ticks_diff(time.ticks_ms(), timestamp)) or (not first_report):
timestamp = time.ticks_ms() timestamp = time.ticks_ms()
first_report = True first_report = True
zcl_head = bytes([12, 30, 16, 171, 5]) # zcl_header #payload = bytes([12, 30, 16, 171, 5,0, 0, 0, 16, ad4.value()]) # zcl_header
payload = bytes([0, 0, 0, 16, ad4.value()]) #payload = bytes([])
payload = zcl_head + payload #payload = zcl_head# + payload
com.fancy_transmit(payload=payload, source_ep=8, dest_ep=1, cluster=6, profile=260) com.fancy_transmit(payload=bytes([12, 30, 16, 171, 5,0, 0, 0, 16, ad4.value()]), source_ep=8, dest_ep=1, cluster=6, profile=260)
com.fancy_transmit(payload=bytes([12, 30, 16, 171,5])+garage.status(), source_ep=8, dest_ep=1, cluster=259, profile=260)
if garage.watch(): if garage.watch():
zcl_head = bytes([12, 30, 16, 171, 5]) # zcl_header zcl_head = bytes([12, 30, 16, 171, 5]) # zcl_header
print("door: "+ str(garage.door))
print("motor: "+ str(garage.motor))
time.sleep(1)
garage.update = False
#print(xbee.receive()) #print(xbee.receive())

@ -447,16 +447,16 @@ DATATYPES_BY_NAME = {
# return cluster, data # return cluster, data
PROFILE_COMMANDS_BY_NAME = { _PROFILE_COMMANDS_BY_NAME = {
# ZCL Spec -- "2.5 General Command Frames" # ZCL Spec -- "2.5 General Command Frames"
'read_attributes': (0x00, ('attributes:*uint16',),), 'read_attributes': (0x00, ('attributes:*uint16',),),
'read_attributes_response': (0x01, ('attributes:%read_attr_status',),), 'read_attributes_response': (0x01, ('attributes:%read_attr_status',),),
'write_attributes': (0x02, ('a:*write_attr',),), 'write_attributes': (0x02, ('a:*write_attr',),),
'write_attributes_undivided': (0x03, ('a:*write_attr',),), 'write_attributes_undivided': (0x03, ('a:*write_attr',),),
'write_attributes_response': (0x04, ('*a:write_attr_status',),), #'write_attributes_response': (0x04, ('*a:write_attr_status',),),
'write_attributes_no_response': (0x05, ('a:*write_attr',),), #'write_attributes_no_response': (0x05, ('a:*write_attr',),),
'configure_reporting': (0x06, ('configs:*attr_reporting_config',),), #'configure_reporting': (0x06, ('configs:*attr_reporting_config',),),
'configure_reporting_response': (0x07, ('results:%attr_reporting_status',),), #'configure_reporting_response': (0x07, ('results:%attr_reporting_status',),),
# 'read_reporting_configuration': (0x08, (),), # 'read_reporting_configuration': (0x08, (),),
# 'read_reporting_configuration_response': (0x09, (),), # 'read_reporting_configuration_response': (0x09, (),),
'report_attributes': (0x0a, ('attributes:%reported_attribute',),), 'report_attributes': (0x0a, ('attributes:%reported_attribute',),),
@ -470,11 +470,11 @@ PROFILE_COMMANDS_BY_NAME = {
PROFILE_COMMANDS_BY_ID = { PROFILE_COMMANDS_BY_ID = {
command: (command_name, args) for command_name, (command, args) in PROFILE_COMMANDS_BY_NAME.items() command: (command_name, args) for command_name, (command, args) in _PROFILE_COMMANDS_BY_NAME.items()
} }
CLUSTERS_BY_NAME = { _CLUSTERS_BY_NAME = {
# ZCL Spec -- Chapter 3 -- General # ZCL Spec -- Chapter 3 -- General
'basic': (0x0000, { 'basic': (0x0000, {
'reset': (0x00, (),), 'reset': (0x00, (),),
@ -607,7 +607,7 @@ CLUSTERS_BY_NAME = {
CLUSTERS_BY_ID = {} CLUSTERS_BY_ID = {}
for cluster_name, (cluster, rx_commands, tx_commands, attributes,) in CLUSTERS_BY_NAME.items(): for cluster_name, (cluster, rx_commands, tx_commands, attributes,) in _CLUSTERS_BY_NAME.items():
CLUSTERS_BY_ID[cluster] = ( CLUSTERS_BY_ID[cluster] = (
cluster_name, cluster_name,
{ {
@ -682,18 +682,18 @@ def return_attributes(cluster, data):
def get_cluster_by_name(cluster_name): def get_cluster_by_name(cluster_name):
if cluster_name not in CLUSTERS_BY_NAME: if cluster_name not in _CLUSTERS_BY_NAME:
raise ValueError('Unknown cluster "{}"'.format(cluster_name)) raise ValueError('Unknown cluster "{}"'.format(cluster_name))
cluster, _rx_commands, _tx_commands, _attributes = CLUSTERS_BY_NAME[cluster_name] cluster, _rx_commands, _tx_commands, _attributes = _CLUSTERS_BY_NAME[cluster_name]
return cluster return cluster
def get_cluster_rx_command(cluster_name, command_name): def get_cluster_rx_command(cluster_name, command_name):
if cluster_name not in CLUSTERS_BY_NAME: if cluster_name not in _CLUSTERS_BY_NAME:
raise ValueError('Unknown cluster "{}"'.format(cluster_name)) raise ValueError('Unknown cluster "{}"'.format(cluster_name))
cluster, rx_commands, _tx_commands, _attributes = CLUSTERS_BY_NAME[cluster_name] cluster, rx_commands, _tx_commands, _attributes = _CLUSTERS_BY_NAME[cluster_name]
if command_name not in rx_commands: if command_name not in rx_commands:
raise ValueError('Unknown command "{}"'.format(command_name)) raise ValueError('Unknown command "{}"'.format(command_name))

Loading…
Cancel
Save