splitting things out for easier management of functions

barrier for the garage door stuff
com for xbee communications, send/receive etc
main is where the loop lives
spec is the dope library to decode the payload
master
Nathan 3 years ago
parent faf7e343db
commit 886f3c8d9f

@ -0,0 +1,19 @@
import com
import xbee
from machine import Pin
class barrier:
moving = 0
position = 0
ad0 = Pin("D0", Pin.IN, Pin.PULL_UP)
ad1 = Pin("D1", Pin.IN, Pin.PULL_UP)
ad2 = Pin("D2", Pin.IN, Pin.PULL_UP)
ad4 = Pin("D4", Pin.OUT)
def status(seq,payload):
pass
def command(seq, payload):
pass
def watch(self):
pass

@ -0,0 +1,17 @@
import time
import xbee
def receive():
return xbee.receive()
def fancy_transmit(payload, source_ep, dest_ep, cluster,profile):
send = 0
while send==0:
try:
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)

@ -6,123 +6,94 @@ import xbee
import spec import spec
import ubinascii import ubinascii
from machine import I2C from machine import I2C
import barrier
from machine import Pin from machine import Pin
import gen import gen
import com
import struct import struct
# print(" +-------------------------------------+")
# print(" +-------------------------------------+\n")
# print("Waiting for data...\n")
# default to everything off and 0 at power on.
# in the future, we can try to reclaim previous state.
# Hopefully this lamp won't turn off very often
ad0 = Pin("D0", Pin.IN, Pin.PULL_UP) ad0 = Pin("D0", Pin.IN, Pin.PULL_UP)
ad1 = Pin("D1", Pin.IN, Pin.PULL_UP) ad1 = Pin("D1", Pin.IN, Pin.PULL_UP)
ad2 = Pin("D2", Pin.IN, Pin.PULL_UP) ad2 = Pin("D2", Pin.IN, Pin.PULL_UP)
ad4 = Pin("D4", Pin.OUT) ad4 = Pin("D4", Pin.OUT)
def status_cb(status): def status_cb(status):
print("received status: {:02x}".format(status)) print("m stat: {:02x}".format(status))
xbee.modem_status.callback(status_cb) xbee.modem_status.callback(status_cb)
# arduino_addr = 0x48 # arduino_addr = 0x48
# senddata = 0 # senddata = 0
time.sleep(3)
x = xbee.XBee()
#xbee.atcmd('NT', 0xFF)
#tp = xbee.atcmd('TP')1A
xbee.atcmd('KY',b'\x5A\x69\x67\x42\x65\x65\x41\x6C\x6C\x69\x61\x6E\x63\x65\x30\x39') xbee.atcmd('KY',b'\x5A\x69\x67\x42\x65\x65\x41\x6C\x6C\x69\x61\x6E\x63\x65\x30\x39')
print(xbee.atcmd('MY'))
print("transmitting")
srcaddr = int(xbee.atcmd('MY')) srcaddr = int(xbee.atcmd('MY'))
print(xbee.atcmd('AI'))
xbee.atcmd('CN') xbee.atcmd('CN')
time.sleep(3) srcarry = srcaddr.to_bytes(2, "big")
srcarry = srcaddr.to_bytes(2,"big")
initial_payload=bytes([171, srcarry[1], srcarry[0], 141, 194, 209, 65, 0, 162, 19, 0, 142]) 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])
print(initial_payload)
#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
lame = 0
#while lame < 3:
# xbee.receive()
# lame += 1
#while send==0:
# try:
# if xbee.transmit(xbee.ADDR_COORDINATOR, leave_load, source_ep=0, dest_ep=0, cluster=52, profile=0, tx_options=0) is None:
# send = 1
# print("leaving")
# except OSError as e:
# print("leaving transmit error")
# send=1
send=0
time.sleep(1) time.sleep(1)
def fancy_transmit(payload, source_ep, dest_ep, cluster,profile):
send = 0
while send==0:
try:
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)
fancy_transmit( payload=initial_payload, source_ep=0, dest_ep=0, cluster=19,profile=0 ) 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
timestamp = time.ticks_ms() timestamp = time.ticks_ms()
while 1 != 0: while 1 != 0:
blorp = xbee.receive() packet = com.receive()
if blorp is not None: if packet is not None:
print(blorp) print(packet)
if blorp['cluster'] == 6: #genOnOffCluster in HA Profile if packet['cluster'] == 259: #barrier cluster
if blorp['profile'] == 260: #HA profile cluster_name, seq, CommandType, command_name, disable_default_response, kwargs = spec.decode_zcl(
cluster_name, seq, CommandType, command_name, disable_default_response, kwargs = spec.decode_zcl(blorp['cluster'], blorp['payload']) packet['cluster'], packet['payload'])
if "attributes" in kwargs:
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(CommandType)
print(command_name) print(command_name)
print(kwargs) print(kwargs)
if 'command' in kwargs: if "attributes" in kwargs:
if kwargs['commands'][0] == 0: payload = bytes([12, 30, 16, seq, 1])
ad4.value(1) payload = payload + bytes([0, 0, 16, ad4.value()])
time.sleep(1) # payload= attr_bytes
ad4.value(0) print(payload)
if kwargs['commands'][1] == 0: 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":
ad4.value(1) ad4.value(1)
time.sleep(1) if command_name == "off":
ad4.value(0) ad4.value(0)
if kwargs['commands'][2] == 0: if command_name == "toggle":
ad4.value(1) ad4.value(1)
time.sleep(1) time.sleep(1)
ad4.value(0) ad4.value(0)
if blorp['cluster']==5: #active endpoint request if packet['cluster']==5: #active endpoint request
print(bytes(blorp['payload'])) print(bytes(packet['payload']))
b = bytearray(blorp['payload']) b = bytearray(packet['payload'])
print(b[0]) print(b[0])
payload=bytes([b[0], 00, b[1], b[2], 1, 8]) payload=bytes([b[0], 00, b[1], b[2], 1, 8])
fancy_transmit(payload=payload,source_ep=0,dest_ep=0,cluster=32773, profile=0) com.fancy_transmit(payload=payload,source_ep=0,dest_ep=0,cluster=32773, profile=0)
print("sent-endpoint-response") print("sent-endpoint-response")
if blorp['cluster']==4: #simple descriptor request if packet['cluster']==4: #simple descriptor request
print(bytes(blorp['payload'])) print(bytes(packet['payload']))
b = bytearray(blorp['payload']) b = bytearray(packet['payload'])
print(b[0]) print(b[0])
payload = bytes([b[0], 00, b[1], b[2], 14, 8, 4, 1, 2, 0, 6, 3, 0, 0, 3, 0, 6, 0, 0]) payload = bytes([b[0], 00, b[1], b[2], 14, 8, 4, 1, 2, 0, 6, 3, 0, 0, 3, 0, 6, 0, 0])
fancy_transmit(payload=payload, source_ep=0, dest_ep=0, cluster=32772, profile=0) com.fancy_transmit(payload=payload, source_ep=0, dest_ep=0, cluster=32772, profile=0)
print("simple descriptor response") print("simple descriptor response")
if blorp['cluster'] == 0: #network address request if packet['cluster'] == 0: #network address request
if blorp['profile'] == 260: if packet['profile'] == 260:
#resp = bytearray(blorp['payload']) #resp = bytearray(packet['payload'])
cluster_name, seq, CommandType, command_name, disable_default_response, kwargs = spec.decode_zcl(blorp['cluster'], blorp['payload']) cluster_name, seq, CommandType, command_name, disable_default_response, kwargs = spec.decode_zcl(packet['cluster'], packet['payload'])
print(command_name) print(command_name)
print(kwargs) print(kwargs)
if 'attributes' in kwargs: if 'attributes' in kwargs:
@ -133,33 +104,33 @@ while 1 != 0:
payload = payload+attr_bytes payload = payload+attr_bytes
#payload= attr_bytes #payload= attr_bytes
print(payload) print(payload)
fancy_transmit(payload=payload, source_ep=blorp['dest_ep'], dest_ep=blorp['source_ep'], cluster=blorp['cluster'], profile=blorp['profile']) com.fancy_transmit(payload=payload, source_ep=packet['dest_ep'], dest_ep=packet['source_ep'], cluster=packet['cluster'], profile=packet['profile'])
print("attribute_read_response") print("attribute_read_response")
#spec.decode_zcl(blorp['cluster'], blorp['payload']) #spec.decode_zcl(packet['cluster'], packet['payload'])
print(bytes(blorp['payload'])) print(bytes(packet['payload']))
b = bytearray(blorp['payload']) b = bytearray(packet['payload'])
for x in b: for x in b:
print(x) print(x)
if blorp['cluster'] == 2: #node descriptor request if packet['cluster'] == 2: #node descriptor request
print(bytes(blorp['payload'])) print(bytes(packet['payload']))
b = bytearray(blorp['payload']) b = bytearray(packet['payload'])
print(b[0]) print(b[0])
payload = bytes([b[0], 00, b[1], b[2], 4, 143, 120, 8, 80, 160, 0, 1, 44, 160, 0, 0]) payload = bytes([b[0], 00, b[1], b[2], 4, 143, 120, 8, 80, 160, 0, 1, 44, 160, 0, 0])
fancy_transmit(payload=payload, source_ep=0, dest_ep=0, cluster=32772, profile=blorp['profile']) com.fancy_transmit(payload=payload, source_ep=0, dest_ep=0, cluster=32772, profile=packet['profile'])
print("node descriptor response") print("node descriptor response")
if blorp['cluster'] == 32770: #node descriptor response if packet['cluster'] == 32770: #node descriptor response
print(bytes(blorp['payload'])) print(bytes(packet['payload']))
b = bytearray(blorp['payload']) b = bytearray(packet['payload'])
print("Node descriptor response integer payload discard") print("Node descriptor response integer payload discard")
#for key, value in blorp.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, 10]) zcl_head = bytes([12, 30, 16, 171, 10])
payload = zcl_head + bytes([0,0,10,0]) #for now only return off for state report payload = zcl_head + bytes([0,0,10,0]) #for now only return off for state report
fancy_transmit(payload=payload, source_ep=8, dest_ep=1, cluster=6, profile=260) com.fancy_transmit(payload=payload, source_ep=8, dest_ep=1, cluster=6, profile=260)
barrier.watch()
#print(xbee.receive()) #print(xbee.receive())
#print("temperature") #print("temperature")

@ -521,6 +521,7 @@ CLUSTERS_BY_NAME = {
}, { }, {
'name_support': (0x0000, 'uint8',), 'name_support': (0x0000, 'uint8',),
},), },),
'scenes': (0x0005, { 'scenes': (0x0005, {
}, { }, {
}, { }, {
@ -539,29 +540,11 @@ CLUSTERS_BY_NAME = {
'on_time': (0x4001, 'uint16',), 'on_time': (0x4001, 'uint16',),
'off_wait_time': (0x4002, 'uint16',), 'off_wait_time': (0x4002, 'uint16',),
},), },),
'barrierControl'
'onoff_configuration': (0x0007, { 'onoff_configuration': (0x0007, {
}, { }, {
}, { }, {
},), },),
'level_control': (0x0008, {
'move_to_level': (0x00, ('level:uint8', 'time:uint16',),),
'move': (0x01, ('mode:enum8:up,down', 'rate:uint8',),),
'step': (0x02, ('mode:enum8:up,down', 'size:uint8', 'time:uint16',),),
'stop': (0x03, (),),
'move_to_level_on_off': (0x04, ('level:uint8', 'time:uint16',),),
'move_on_off': (0x05, ('mode:enum8:up,down', 'rate:uint8',),),
'step_on_off': (0x06, ('mode:enum8:up,down', 'size:uint8', 'time:uint16',),),
'stop_on_off': (0x07, (),),
}, {
}, {
'current_level': (0x0000, 'uint8',),
'remaining_time': (0x0001, 'uint16',),
'on_off_transition_time': (0x0010, 'uint16',),
'on_level': (0x0011, 'uint8',),
'on_transition_time': (0x0012, 'uint16',),
'off_transition_time': (0x0013, 'uint16',),
'default_move_rate': (0x0014, 'uint16',),
},),
'poll_control': (0x0020, { 'poll_control': (0x0020, {
}, { }, {
}, { }, {

Loading…
Cancel
Save