From d2639e1b8bafaf72380439fcf010a1cfa959f4d7 Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 7 Jan 2022 23:16:39 -0500 Subject: [PATCH] updating, sending values with write command no response moving formatting around to reduce memory footprint handling eonotcon error by restarting network --- barrier.py | 56 +++++++++++++++++++++++++++++++----------------------- com.py | 22 ++++++++++++++++++--- main.py | 39 +++++++++++++++++++------------------ spec.py | 24 +++++++++++------------ 4 files changed, 83 insertions(+), 58 deletions(-) diff --git a/barrier.py b/barrier.py index 5ee4728..33b1362 100644 --- a/barrier.py +++ b/barrier.py @@ -4,17 +4,23 @@ from machine import Pin class Barrier: - moving = 0 - barrier_position = 0 + moving = b'\x00' + barrier_position = b'\x00' ad0 = Pin("D0", Pin.IN, Pin.PULL_UP) #door ad1 = Pin("D1", Pin.IN, Pin.PULL_UP) #motor ad4 = Pin("D4", Pin.OUT, value=0) - door = ad0.value() - motor = ad1.value() - update = False + door = ad0.value().to_bytes(2,"big") + motor = ad1.value().to_bytes(2, "big") + update = True 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): pass @@ -22,26 +28,28 @@ class Barrier: def watch(self): current_door = self.door current_motor = self.motor - self.door = self.ad0.value() - self.motor = self.ad1.value() + self.door = self.ad0.value().to_bytes(2, "big") + 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): self.update = True else: self.update = False - if self.door == 0: - if self.motor == 1: - self.moving = 0 - self.barrier_position = 100 - if self.motor == 0: - if self.door == 1: - self.moving = 0 - self.barrier_position = 0 - if self.motor == 0: - if self.door == 0: - if self.barrier_position == 100: - self.barrier_position = 50 - self.moving = 1 - if self.barrier_position == 0: - self.barrier_position = 50 - self.moving = 2 + if self.door == b'\x00': + if self.motor == b'\x01': + self.moving = b'\x00' + self.barrier_position = b'\x64' + if self.motor == b'\x00': + if self.door == b'\x01': + self.moving = b'\x00' + self.barrier_position = b'\x00' + if self.motor == b'\x00': + if self.door == b'\x00': + if self.barrier_position == b'\x64': + self.barrier_position = b'\x32' + self.moving = b'\x01' + if self.barrier_position == b'\x00': + self.barrier_position = b'\x32' + self.moving = b'\x02' return self.update diff --git a/com.py b/com.py index e3b2b5f..fc835a3 100644 --- a/com.py +++ b/com.py @@ -3,6 +3,13 @@ import xbee def 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): 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, tx_options=0) is None: send = 1 - #time.sleep(1) except OSError as e: time.sleep(1) - print(payload) - print(e) \ No newline at end of file + # print("connection status: {stat}".format(stat=str(xbee.atcmd('AI')))) + 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) \ No newline at end of file diff --git a/main.py b/main.py index 02dfde2..45174c1 100644 --- a/main.py +++ b/main.py @@ -22,20 +22,17 @@ def status_cb(status): xbee.modem_status.callback(status_cb) # arduino_addr = 0x48 # senddata = 0 -xbee.atcmd('KY',b'\x5A\x69\x67\x42\x65\x65\x41\x6C\x6C\x69\x61\x6E\x63\x65\x30\x39') -srcaddr = int(xbee.atcmd('MY')) -xbee.atcmd('CN') -srcarry = srcaddr.to_bytes(2, "big") +key = b'\x5A\x69\x67\x42\x65\x65\x41\x6C\x6C\x69\x61\x6E\x63\x65\x30\x39' +xbee.atcmd('KY', key) -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) -send=0 +#send=0 +com.announce() time.sleep(1) -com.fancy_transmit( payload=initial_payload, source_ep=0, dest_ep=0, cluster=19,profile=0 ) print("receiving") diff = 3600000 first_report = False @@ -53,15 +50,14 @@ while 1 != 0: barrier.status(seq,payload) if CommandType is not None: barrier.command(payload) - barrier.status(seq, payload) pass if packet['cluster'] == 6: #genOnOffCluster in 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']) - print(packet['payload']) - print(CommandType) - print(command_name) - print(kwargs) + # print(packet['payload']) + # print(CommandType) + # print(command_name) + # print(kwargs) if "attributes" in kwargs: if kwargs['attributes'][0] == 0: payload = bytes([12, 30, 16, seq, 1]) #zcl_header @@ -74,7 +70,7 @@ while 1 != 0: payload = bytes([12, 30, 16, seq, 1]) payload = payload + bytes([0, 0, 16, ad4.value()]) # payload= attr_bytes - print(payload) + #print(payload) com.fancy_transmit(payload=payload, source_ep=packet['dest_ep'], dest_ep=packet['source_ep'], cluster=packet['cluster'], profile=packet['profile']) if command_name == "on": @@ -134,15 +130,20 @@ while 1 != 0: print("Node descriptor response integer payload discard") #for key, value in packet.items(): #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() first_report = True - zcl_head = bytes([12, 30, 16, 171, 5]) # zcl_header - payload = bytes([0, 0, 0, 16, ad4.value()]) - payload = zcl_head + payload - com.fancy_transmit(payload=payload, source_ep=8, dest_ep=1, cluster=6, profile=260) + #payload = bytes([12, 30, 16, 171, 5,0, 0, 0, 16, ad4.value()]) # zcl_header + #payload = bytes([]) + #payload = zcl_head# + payload + 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(): 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()) diff --git a/spec.py b/spec.py index 0f4ad25..555043c 100644 --- a/spec.py +++ b/spec.py @@ -447,16 +447,16 @@ DATATYPES_BY_NAME = { # return cluster, data -PROFILE_COMMANDS_BY_NAME = { +_PROFILE_COMMANDS_BY_NAME = { # ZCL Spec -- "2.5 General Command Frames" 'read_attributes': (0x00, ('attributes:*uint16',),), 'read_attributes_response': (0x01, ('attributes:%read_attr_status',),), 'write_attributes': (0x02, ('a:*write_attr',),), 'write_attributes_undivided': (0x03, ('a:*write_attr',),), - 'write_attributes_response': (0x04, ('*a:write_attr_status',),), - 'write_attributes_no_response': (0x05, ('a:*write_attr',),), - 'configure_reporting': (0x06, ('configs:*attr_reporting_config',),), - 'configure_reporting_response': (0x07, ('results:%attr_reporting_status',),), + #'write_attributes_response': (0x04, ('*a:write_attr_status',),), + #'write_attributes_no_response': (0x05, ('a:*write_attr',),), + #'configure_reporting': (0x06, ('configs:*attr_reporting_config',),), + #'configure_reporting_response': (0x07, ('results:%attr_reporting_status',),), # 'read_reporting_configuration': (0x08, (),), # 'read_reporting_configuration_response': (0x09, (),), 'report_attributes': (0x0a, ('attributes:%reported_attribute',),), @@ -470,11 +470,11 @@ PROFILE_COMMANDS_BY_NAME = { 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 'basic': (0x0000, { 'reset': (0x00, (),), @@ -607,7 +607,7 @@ CLUSTERS_BY_NAME = { 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] = ( cluster_name, { @@ -682,18 +682,18 @@ def return_attributes(cluster, data): 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)) - cluster, _rx_commands, _tx_commands, _attributes = CLUSTERS_BY_NAME[cluster_name] + cluster, _rx_commands, _tx_commands, _attributes = _CLUSTERS_BY_NAME[cluster_name] return cluster 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)) - 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: raise ValueError('Unknown command "{}"'.format(command_name))