@ -6,123 +6,94 @@ import xbee
import spec
import ubinascii
from machine import I2C
import barrier
from machine import Pin
import gen
import com
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 )
ad1 = Pin ( " D1 " , Pin . IN , Pin . PULL_UP )
ad2 = Pin ( " D2 " , Pin . IN , Pin . PULL_UP )
ad4 = Pin ( " D4 " , Pin . OUT )
def status_cb ( status ) :
print ( " received status : {:02x} " . format ( status ) )
print ( " m stat: {:02x} " . format ( status ) )
xbee . modem_status . callback ( status_cb )
# arduino_addr = 0x48
# 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 ' )
print ( xbee . atcmd ( ' MY ' ) )
print ( " transmitting " )
srcaddr = int ( xbee . atcmd ( ' MY ' ) )
print ( xbee . atcmd ( ' AI ' ) )
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 ] )
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)
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 )
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 " )
diff = 3600000
first_report = False
timestamp = time . ticks_ms ( )
while 1 != 0 :
blorp = xbee . receive ( )
if blorp is not None :
print ( blorp )
if blorp [ ' cluster ' ] == 6 : #genOnOffCluster in HA Profile
if blorp [ ' profile ' ] == 260 : #HA profile
cluster_name , seq , CommandType , command_name , disable_default_response , kwargs = spec . decode_zcl ( blorp [ ' cluster ' ] , blorp [ ' payload ' ] )
packet = com . receive ( )
if packet is not None :
print ( packet )
if packet [ ' cluster ' ] == 259 : #barrier cluster
cluster_name , seq , CommandType , command_name , disable_default_response , kwargs = spec . decode_zcl (
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 ( command_name )
print ( kwargs )
if ' command ' in kwargs :
if kwargs [ ' commands ' ] [ 0 ] == 0 :
ad4 . value ( 1 )
time . sleep ( 1 )
ad4 . value ( 0 )
if kwargs [ ' commands ' ] [ 1 ] == 0 :
if " attributes " in kwargs :
payload = bytes ( [ 12 , 30 , 16 , seq , 1 ] )
payload = payload + bytes ( [ 0 , 0 , 16 , ad4 . value ( ) ] )
# payload= attr_bytes
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 " :
ad4 . value ( 1 )
time . sleep ( 1 )
if command_name == " off " :
ad4 . value ( 0 )
if kwargs [ ' commands ' ] [ 2 ] == 0 :
if command_name == " toggle " :
ad4 . value ( 1 )
time . sleep ( 1 )
ad4 . value ( 0 )
if blor p[ ' cluster ' ] == 5 : #active endpoint request
print ( bytes ( blor p[ ' payload ' ] ) )
b = bytearray ( blor p[ ' payload ' ] )
if packet [ ' cluster ' ] == 5 : #active endpoint request
print ( bytes ( packet [ ' payload ' ] ) )
b = bytearray ( packet [ ' payload ' ] )
print ( b [ 0 ] )
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 " )
if blor p[ ' cluster ' ] == 4 : #simple descriptor request
print ( bytes ( blor p[ ' payload ' ] ) )
b = bytearray ( blor p[ ' payload ' ] )
if packet [ ' cluster ' ] == 4 : #simple descriptor request
print ( bytes ( packet [ ' payload ' ] ) )
b = bytearray ( packet [ ' payload ' ] )
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 ] )
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 " )
if blor p[ ' cluster ' ] == 0 : #network address request
if blor p[ ' profile ' ] == 260 :
#resp = bytearray( blor p['payload'])
cluster_name , seq , CommandType , command_name , disable_default_response , kwargs = spec . decode_zcl ( blor p[ ' cluster ' ] , blor p[ ' payload ' ] )
if packet [ ' cluster ' ] == 0 : #network address request
if packet [ ' profile ' ] == 260 :
#resp = bytearray( packet ['payload'])
cluster_name , seq , CommandType , command_name , disable_default_response , kwargs = spec . decode_zcl ( packet [ ' cluster ' ] , packet [ ' payload ' ] )
print ( command_name )
print ( kwargs )
if ' attributes ' in kwargs :
@ -133,33 +104,33 @@ while 1 != 0:
payload = payload + attr_bytes
#payload= attr_bytes
print ( payload )
fancy_transmit( payload = payload , source_ep = blor p[ ' dest_ep ' ] , dest_ep = blor p[ ' source_ep ' ] , cluster = blor p[ ' cluster ' ] , profile = blor p[ ' 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 " )
#spec.decode_zcl( blor p['cluster'], blor p['payload'])
print ( bytes ( blor p[ ' payload ' ] ) )
b = bytearray ( blor p[ ' payload ' ] )
#spec.decode_zcl( packet ['cluster'], packet ['payload'])
print ( bytes ( packet [ ' payload ' ] ) )
b = bytearray ( packet [ ' payload ' ] )
for x in b :
print ( x )
if blor p[ ' cluster ' ] == 2 : #node descriptor request
print ( bytes ( blor p[ ' payload ' ] ) )
b = bytearray ( blor p[ ' payload ' ] )
if packet [ ' cluster ' ] == 2 : #node descriptor request
print ( bytes ( packet [ ' payload ' ] ) )
b = bytearray ( packet [ ' payload ' ] )
print ( b [ 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 = blor p[ ' profile ' ] )
com. fancy_transmit( payload = payload , source_ep = 0 , dest_ep = 0 , cluster = 32772 , profile = packet [ ' profile ' ] )
print ( " node descriptor response " )
if blor p[ ' cluster ' ] == 32770 : #node descriptor response
print ( bytes ( blor p[ ' payload ' ] ) )
b = bytearray ( blor p[ ' payload ' ] )
if packet [ ' cluster ' ] == 32770 : #node descriptor response
print ( bytes ( packet [ ' payload ' ] ) )
b = bytearray ( packet [ ' payload ' ] )
print ( " Node descriptor response integer payload discard " )
#for key, value in blor p.items():
#for key, value in packet .items():
#1 print (key, ' : ', value)
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 , 10 ] )
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("temperature")